defer creation of apolloClient until it is first used

This commit is contained in:
ochicf 2018-08-28 18:24:30 +02:00
parent 485f7bad37
commit 374dc2510d

View file

@ -14,15 +14,12 @@ export const initContext = () => {
// init
const history = browserHistory;
const loginToken = global.localStorage['Meteor.loginToken'];
const apolloClient = createApolloClient();
addReducer({ apollo: apolloClient.reducer() });
addMiddleware(apolloClient.middleware());
let apolloClient;
// init context
context = {
history,
loginToken,
apolloClient,
addAction, // context.addAction same as addAction
getActions, // context.getActions same as getActions
addReducer, // context.addReducer same as addReducer
@ -31,6 +28,19 @@ export const initContext = () => {
getMiddlewares, // context.getMiddlewares same as getMiddlewares
};
// defer creation of apolloClient until it is first used
Object.defineProperty(context, 'apolloClient', {
enumerable: true,
get: () => {
if (!apolloClient) {
apolloClient = createApolloClient();
addReducer({ apollo: apolloClient.reducer() });
addMiddleware(apolloClient.middleware());
}
return apolloClient;
},
});
// init store
context.store = configureStore(context.getReducers, {}, (store) => {
let chain, newDispatch;