-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-apollo-client.ts
More file actions
36 lines (31 loc) · 866 Bytes
/
server-apollo-client.ts
File metadata and controls
36 lines (31 loc) · 866 Bytes
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
import { InMemoryCache, ApolloClient, createHttpLink } from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
import { getCookie } from 'cookies-next'
import { NextPageContext } from 'next'
const httpLink = createHttpLink({
uri: 'http://localhost:8000/graphql',
credentials: 'same-origin',
})
const authLink = (ctx?: NextPageContext) =>
setContext((_, { headers }) => {
const token =
ctx && ctx.req
? getCookie('auth-token', {
req: ctx.req,
res: ctx.res,
})
: ''
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
}
})
const client = (ctx?: NextPageContext) =>
new ApolloClient({
ssrMode: true,
link: authLink(ctx).concat(httpLink),
cache: new InMemoryCache(),
})
export default client