Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

with-apollo: Don't store Redux store and Apollo client in global namespace #909

Merged
merged 2 commits into from
Jan 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions examples/with-apollo/lib/initClient.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import ApolloClient, { createNetworkInterface } from 'apollo-client'

export const initClient = (headers) => {
const client = new ApolloClient({
let apolloClient = null

function createClient (headers) {
return new ApolloClient({
ssrMode: !process.browser,
headers,
dataIdFromObject: result => result.id || null,
Expand All @@ -12,11 +14,14 @@ export const initClient = (headers) => {
}
})
})
}

export const initClient = (headers) => {
if (!process.browser) {
return client
return createClient(headers)
}
if (!window.APOLLO_CLIENT) {
window.APOLLO_CLIENT = client
if (!apolloClient) {
apolloClient = createClient(headers)
}
return window.APOLLO_CLIENT
return apolloClient
}
8 changes: 5 additions & 3 deletions examples/with-apollo/lib/initStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { createStore } from 'redux'
import getReducer from './reducer'
import createMiddleware from './middleware'

let reduxStore = null

export const initStore = (client, initialState) => {
let store
if (!process.browser || !window.REDUX_STORE) {
if (!process.browser || !reduxStore) {
const middleware = createMiddleware(client.middleware())
store = createStore(getReducer(client), initialState, middleware)
if (!process.browser) {
return store
}
window.REDUX_STORE = store
reduxStore = store
}
return window.REDUX_STORE
return reduxStore
}
Loading