gitlab-org--gitlab-foss/app/assets/javascripts/lib/graphql.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

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';
import csrf from '~/lib/utils/csrf';
2019-05-24 07:10:56 -04:00
export default (resolvers = {}, config = {}) => {
let uri = `${gon.relative_url_root}/api/graphql`;
2019-05-24 07:10:56 -04:00
if (config.baseUrl) {
// Prepend baseUrl and ensure that `///` are replaced with `/`
2019-05-24 07:10:56 -04:00
uri = `${config.baseUrl}${uri}`.replace(/\/{3,}/g, '/');
}
2019-05-28 09:41:57 -04:00
const httpOptions = {
uri,
headers: {
[csrf.headerKey]: csrf.token,
},
};
return new ApolloClient({
2019-05-28 09:41:57 -04:00
link: ApolloLink.split(
operation => operation.getContext().hasUpload || operation.getContext().isSingleRequest,
2019-05-28 09:41:57 -04:00
createUploadLink(httpOptions),
new BatchHttpLink(httpOptions),
),
cache: new InMemoryCache({
...config.cacheConfig,
freezeResults: config.assumeImmutableResults,
}),
resolvers,
assumeImmutableResults: config.assumeImmutableResults,
2019-02-22 07:54:06 -05:00
});
};