Skip to content

Commit ef0c655

Browse files
author
Guillaume Chau
committed
New apolloProvider.getStates() method
1 parent 7734d18 commit ef0c655

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/apollo-provider.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,28 @@ export class ApolloProvider {
128128
})
129129
}
130130

131-
exportStates (options) {
131+
getStates (options) {
132132
const finalOptions = Object.assign({}, {
133133
exportNamespace: '',
134-
globalName: '__APOLLO_STATE__',
135-
attachTo: 'window',
136134
}, options)
137-
138-
let js = `${finalOptions.attachTo}.${finalOptions.globalName} = {`
135+
const states = {}
139136
for (const key in this.clients) {
140137
const client = this.clients[key]
141138
const state = { [client.reduxRootKey || 'apollo']: client.getInitialState() }
142-
js += `['${finalOptions.exportNamespace}${key}']:${JSON.stringify(state)},`
139+
states[`${finalOptions.exportNamespace}${key}`] = state
143140
}
144-
js += `};`
141+
return states
142+
}
143+
144+
exportStates (options) {
145+
const finalOptions = Object.assign({}, {
146+
globalName: '__APOLLO_STATE__',
147+
attachTo: 'window',
148+
}, options)
149+
const states = this.getStates({
150+
exportNamespace: options.exportNamespace,
151+
})
152+
const js = `${finalOptions.attachTo}.${finalOptions.globalName} = ${JSON.stringify(states)};`
145153
return js
146154
}
147155
}

0 commit comments

Comments
 (0)