blob: 5a32fe3d4c77a4b3c0c8e9210462a9e8595f1428 (
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
|
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 => {
console.log('hey');
if (typeof localStorage !== 'undefined' && localStorage.getItem('token')) {
console.log('heyyy');
console.log(`Bearer ${localStorage.getItem('token')}`);
operation.setContext({
headers: {
authorization: `Bearer ${localStorage.getItem('token')}`,
},
});
}
},
}),
});
// 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 });
|