-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
41 lines (33 loc) · 1.14 KB
/
client.js
File metadata and controls
41 lines (33 loc) · 1.14 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
import { ApolloClient, createHttpLink, InMemoryCache } from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
import { onError } from "@apollo/client/link/error";
import { getTokenFromStorage } from "./src/utils/token";
const authenticationHeaders = setContext((_, {headers}) => {
const token = getTokenFromStorage("auth"); //need to pass in a key for the value stored
if (token) {
return {
headers: {
...headers,
authorization: `Bearer ${token}`
}
}
}
});
const httpLink = createHttpLink({
uri: "http://localhost:4000/graphql"
});
const errors = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.map(({ message, locations, path }) =>
console.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
})
const client = new ApolloClient({
link: errors.concat(authenticationHeaders.concat(httpLink)),
cache: new InMemoryCache(),
credentials: 'include'
});
export { client }