Skip to content

Instantly share code, notes, and snippets.

@Chun-Yang
Created August 9, 2017 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chun-Yang/25938dd69f3b189d79297135989565b2 to your computer and use it in GitHub Desktop.
Save Chun-Yang/25938dd69f3b189d79297135989565b2 to your computer and use it in GitHub Desktop.
import React from 'react';
import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import { ApolloClient, ApolloProvider, createNetworkInterface } from 'react-apollo';
import accessToken from './reducers/accessToken'
const SERVER_URL = process.env.REACT_APP_SERVER_URL
const networkInterface = createNetworkInterface({
uri: `${SERVER_URL}/graphql`,
})
networkInterface.use([{
applyMiddleware(req, next) {
if (!req.options.headers) {
req.options.headers = {}; // Create the header object if needed.
}
req.options.headers['authorization'] = localStorage.getItem('accessToken') || null;
next();
}
}])
const client = new ApolloClient({
networkInterface,
});
const initialState = {};
const store = createStore(
combineReducers({
apollo: client.reducer(),
accessToken,
}),
initialState,
compose(
applyMiddleware(client.middleware()),
// If you are using the devToolsExtension, you can add it here also
(typeof window.__REDUX_DEVTOOLS_EXTENSION__ !== 'undefined') ? window.__REDUX_DEVTOOLS_EXTENSION__() : f => f,
)
);
export default function storeHOF (Component) {
return () => {
return <ApolloProvider store={store} client={client}>
<Component/>
</ApolloProvider>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment