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