-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapolloClient.ts
35 lines (32 loc) · 950 Bytes
/
apolloClient.ts
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
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import { setContext } from "apollo-link-context";
import nextCookie from "next-cookies";
import fetch from "isomorphic-unfetch";
export default function createApolloClient(initialState, ctx) {
const authLink = setContext((_, { headers }) => {
const { token } = nextCookie(ctx || {});
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : ""
}
};
});
return new ApolloClient({
ssrMode: Boolean(ctx),
link: authLink.concat(
new HttpLink({
uri: `${
typeof window !== "undefined"
? window.location.origin
: "http://localhost:3000"
}/api/graphql`,
credentials: "same-origin",
fetch
})
),
cache: new InMemoryCache().restore(initialState)
});
}