summaryrefslogtreecommitdiffstats
path: root/frontend/lib/withData.js
blob: 697cf798b9843456107ce25ef256b453b038b23b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

export default withApollo({
  client: new ApolloClient({
    uri: 'http://localhost:4444',
    // cache: new InMemoryCache().restore(initialState || {}),
    // ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
    request: operation => {
      if (typeof localStorage !== 'undefined' && localStorage.getItem('token')) {
        operation.setContext({
          headers: {
            authorization: `Bearer ${localStorage.getItem('token')}`,
          },
        });
      }
    },
  }),
});