Created
August 9, 2017 16:56
-
-
Save Chun-Yang/25938dd69f3b189d79297135989565b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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