debian-mirror-gitlab/app/assets/javascripts/lib/graphql.js
2020-03-13 15:44:24 +05:30

53 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { createUploadLink } from 'apollo-upload-client';
import { ApolloLink } from 'apollo-link';
import { BatchHttpLink } from 'apollo-link-batch-http';
import csrf from '~/lib/utils/csrf';
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',
};
export default (resolvers = {}, config = {}) => {
let uri = `${gon.relative_url_root}/api/graphql`;
if (config.baseUrl) {
// Prepend baseUrl and ensure that `///` are replaced with `/`
uri = `${config.baseUrl}${uri}`.replace(/\/{3,}/g, '/');
}
const httpOptions = {
uri,
headers: {
[csrf.headerKey]: csrf.token,
},
// fetch wont send cookies in older browsers, unless you set the credentials init option.
// We set to `same-origin` which is default value in modern browsers.
// See https://github.com/whatwg/fetch/pull/585 for more information.
credentials: 'same-origin',
};
return new ApolloClient({
link: ApolloLink.split(
operation => operation.getContext().hasUpload || operation.getContext().isSingleRequest,
createUploadLink(httpOptions),
new BatchHttpLink(httpOptions),
),
cache: new InMemoryCache({
...config.cacheConfig,
freezeResults: config.assumeImmutableResults,
}),
resolvers,
assumeImmutableResults: config.assumeImmutableResults,
defaultOptions: {
query: {
fetchPolicy: config.fetchPolicy || fetchPolicies.CACHE_FIRST,
},
},
});
};