-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapollo-client.js
More file actions
39 lines (35 loc) · 849 Bytes
/
apollo-client.js
File metadata and controls
39 lines (35 loc) · 849 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
37
38
39
import { ApolloClient, createHttpLink, InMemoryCache } from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
const httpLink = createHttpLink({
uri: "http://localhost:7500/graphql",
});
const authLink = setContext((_, { headers }) => {
if (typeof window !== "undefined") {
const token = localStorage.getItem("cart-token");
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
};
} else {
return {
headers,
};
}
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {
fetchPolicy: "no-cache",
errorPolicy: "ignore",
},
query: {
fetchPolicy: "no-cache",
errorPolicy: "all",
},
},
});
export default client;