summaryrefslogtreecommitdiffstats
path: root/frontend/lib/withData.js
blob: 5c4614fa75434b7c661ac0fda182fbe1536d8b69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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',
  // cache: new InMemoryCache().restore(initialState || {}),
  ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
  request: operation => {
    console.log(operation);
    if (typeof localStorage !== 'undefined' && localStorage.getItem('token')) {
      console.log(`Bearer ${localStorage.getItem('token')}`);
      operation.setContext({
        headers: {
          authorization: `Bearer ${localStorage.getItem('token')}`,
        },
      });
    }
  },
});

export { client };

export default withApollo({ client });

// OPTION 2

// import { withData } from 'next-apollo';
// import { HttpLink } from 'apollo-link-http';
// import { ApolloLink } from 'apollo-link';

// // can also be a function that accepts a `headers` object (SSR only) and returns a config
// const networkLink = new HttpLink({
//   uri: 'http://localhost:4444', // Server URL (must be absolute)
//   opts: {
//     credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
//   },
// });

// const middlewareLink = new ApolloLink((operation, forward) => {
//   if (typeof localStorage !== 'undefined' && localStorage.getItem('token')) {
//     console.log('heyyy');
//     console.log(`Bearer ${localStorage.getItem('token')}`);
//     operation.setContext({
//       headers: {
//         authorization: `Bearer ${localStorage.getItem('token')}`,
//       },
//     });
//   }
//   return forward(operation);
// });

// const link = middlewareLink.concat(networkLink);

// export default withData({ link });