2019-04-02 07:26:09 +00:00
|
|
|
import { ApolloClient } from 'apollo-client';
|
|
|
|
import { InMemoryCache } from 'apollo-cache-inmemory';
|
|
|
|
import { createUploadLink } from 'apollo-upload-client';
|
2019-05-28 13:41:57 +00:00
|
|
|
import { ApolloLink } from 'apollo-link';
|
|
|
|
import { BatchHttpLink } from 'apollo-link-batch-http';
|
2018-11-27 15:10:40 +00:00
|
|
|
import csrf from '~/lib/utils/csrf';
|
|
|
|
|
2020-01-30 15:09:15 +00:00
|
|
|
export const fetchPolicies = {
|
|
|
|
CACHE_FIRST: 'cache-first',
|
|
|
|
CACHE_AND_NETWORK: 'cache-and-network',
|
|
|
|
NETWORK_ONLY: 'network-only',
|
|
|
|
NO_CACHE: 'no-cache',
|
|
|
|
CACHE_ONLY: 'cache-only',
|
|
|
|
};
|
|
|
|
|
2019-05-24 11:10:56 +00:00
|
|
|
export default (resolvers = {}, config = {}) => {
|
2019-04-25 06:55:21 +00:00
|
|
|
let uri = `${gon.relative_url_root}/api/graphql`;
|
|
|
|
|
2019-05-24 11:10:56 +00:00
|
|
|
if (config.baseUrl) {
|
2019-04-25 06:55:21 +00:00
|
|
|
// Prepend baseUrl and ensure that `///` are replaced with `/`
|
2019-05-24 11:10:56 +00:00
|
|
|
uri = `${config.baseUrl}${uri}`.replace(/\/{3,}/g, '/');
|
2019-04-25 06:55:21 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:41:57 +00:00
|
|
|
const httpOptions = {
|
|
|
|
uri,
|
|
|
|
headers: {
|
|
|
|
[csrf.headerKey]: csrf.token,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-04-25 06:55:21 +00:00
|
|
|
return new ApolloClient({
|
2019-05-28 13:41:57 +00:00
|
|
|
link: ApolloLink.split(
|
2019-06-28 12:36:18 +00:00
|
|
|
operation => operation.getContext().hasUpload || operation.getContext().isSingleRequest,
|
2019-05-28 13:41:57 +00:00
|
|
|
createUploadLink(httpOptions),
|
|
|
|
new BatchHttpLink(httpOptions),
|
|
|
|
),
|
2019-11-13 09:06:41 +00:00
|
|
|
cache: new InMemoryCache({
|
|
|
|
...config.cacheConfig,
|
|
|
|
freezeResults: config.assumeImmutableResults,
|
|
|
|
}),
|
2019-04-02 07:26:09 +00:00
|
|
|
resolvers,
|
2019-11-13 09:06:41 +00:00
|
|
|
assumeImmutableResults: config.assumeImmutableResults,
|
2020-01-30 15:09:15 +00:00
|
|
|
defaultOptions: {
|
|
|
|
query: {
|
|
|
|
fetchPolicy: config.fetchPolicy || fetchPolicies.CACHE_FIRST,
|
|
|
|
},
|
|
|
|
},
|
2019-02-22 12:54:06 +00:00
|
|
|
});
|
2019-04-25 06:55:21 +00:00
|
|
|
};
|