-
Notifications
You must be signed in to change notification settings - Fork 0
/
apollo.ts
30 lines (25 loc) · 822 Bytes
/
apollo.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
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import { API_URL, REST_API_URL } from './config/constants';
import { RestLink } from 'apollo-link-rest';
import { setContext } from '@apollo/client/link/context';
const httpLink = createHttpLink({
uri: API_URL,
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('access_token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : '',
},
};
});
export const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
});
export const restLink = new RestLink({
uri: REST_API_URL,
});