-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.js
29 lines (24 loc) · 811 Bytes
/
client.js
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
import { ApolloClient, InMemoryCache, createHttpLink } from "@apollo/client";
import { setContext } from "@apollo/client/link/context";
const { GITHUB_TOKEN } = process.env;
// Create the http link
const httpLink = createHttpLink({
uri: "https://api.github.com/graphql",
});
// Generate and set the header with the auth details
const authLink = setContext((_, { headers }) => {
// get the authentication token from env variables if it exists
const token = GITHUB_TOKEN;
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: `Bearer ${token}`,
},
};
});
// Generate your client with the authLink and httpLink
export const client = new ApolloClient({
cache: new InMemoryCache(),
link: authLink.concat(httpLink),
});