-
Notifications
You must be signed in to change notification settings - Fork 2
/
agility-graphql-client.js
51 lines (45 loc) · 1.23 KB
/
agility-graphql-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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ApolloClient, createHttpLink, InMemoryCache } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
// default options for apollo to disable their caching
const defaultOptions = {
watchQuery: {
fetchPolicy: "no-cache",
errorPolicy: "ignore",
},
query: {
fetchPolicy: "no-cache",
errorPolicy: "all",
},
};
const httpLink = createHttpLink({
uri: () => {
const type = global?.IS_PREVIEW ? 'preview' : 'fetch';
return `https://api.aglty.io/v1/${process.env.AGILITY_GUID}/${type}/${process.env.AGILITY_LOCALES}/graphql`
}
});
const authLink = setContext((_, { headers }) => {
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
apiKey: global?.IS_PREVIEW ? process.env.AGILITY_API_PREVIEW_KEY : process.env.AGILITY_API_FETCH_KEY,
}
}
});
// set up our apollo client
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache(),
defaultOptions
})
const getContext = ({ config }) => {
return {
headers: {
apiKey: config.apiKey,
},
}
}
export {
client,
getContext
};