This repository was archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapolloClient.js
More file actions
42 lines (38 loc) · 1.39 KB
/
apolloClient.js
File metadata and controls
42 lines (38 loc) · 1.39 KB
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
import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import fetch from 'isomorphic-unfetch'
import cookie from 'cookie';
export default function createApolloClient(initialState, ctx) {
const httpLink = createHttpLink({
uri: 'https://graphql.fauna.com/graphql', // Server URL (must be absolute)
credentials: 'same-origin', // Additional fetch() options like `credentials` or `headers`
fetch,
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = getNextCookies(ctx).token;
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token
? `Bearer ${token}`
: `Bearer ${process.env.FAUNADB_KEY}`,
},
};
});
// The `ctx` (NextPageContext) will only be present on the server.
// use it to extract auth headers (ctx.req) or similar.
return new ApolloClient({
ssrMode: Boolean(ctx),
link: authLink.concat(httpLink),
cache: new InMemoryCache().restore(initialState),
});
}
const getNextCookies = ctx => {
const cookieStr =
ctx && ctx.req ? ctx.req.headers.cookie : window.document.cookie;
return cookie.parse(cookieStr || '');
};