blob: c823e629e833dd175a647db4ed1e4624ac2283fe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import withApollo from 'next-with-apollo';
import ApolloClient from 'apollo-boost';
// can also be a function that accepts a `headers` object (SSR only) and returns a config
const client = new ApolloClient({
uri: 'http://localhost:4444',
ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
request: operation => {
// if we're in the client and the user has a token, send it along with the request
if (typeof localStorage !== 'undefined' && localStorage.getItem('token')) {
operation.setContext({
headers: {
authorization: `Bearer ${localStorage.getItem('token')}`,
},
});
}
},
});
export { client };
export default withApollo({ client });
|