2019-07-07 11:18:12 +05:30
|
|
|
|
import { ApolloClient } from 'apollo-client';
|
|
|
|
|
import { InMemoryCache } from 'apollo-cache-inmemory';
|
|
|
|
|
import { createUploadLink } from 'apollo-upload-client';
|
2019-09-04 21:01:54 +05:30
|
|
|
|
import { ApolloLink } from 'apollo-link';
|
|
|
|
|
import { BatchHttpLink } from 'apollo-link-batch-http';
|
2019-02-15 15:39:39 +05:30
|
|
|
|
import csrf from '~/lib/utils/csrf';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
import PerformanceBarService from '~/performance_bar/services/performance_bar_service';
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
|
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-09-04 21:01:54 +05:30
|
|
|
|
export default (resolvers = {}, config = {}) => {
|
2020-10-24 23:57:45 +05:30
|
|
|
|
let uri = `${gon.relative_url_root || ''}/api/graphql`;
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
|
if (config.baseUrl) {
|
2019-07-31 22:56:46 +05:30
|
|
|
|
// Prepend baseUrl and ensure that `///` are replaced with `/`
|
2019-09-04 21:01:54 +05:30
|
|
|
|
uri = `${config.baseUrl}${uri}`.replace(/\/{3,}/g, '/');
|
2019-07-31 22:56:46 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
|
const httpOptions = {
|
|
|
|
|
uri,
|
|
|
|
|
headers: {
|
|
|
|
|
[csrf.headerKey]: csrf.token,
|
|
|
|
|
},
|
2020-03-13 15:44:24 +05:30
|
|
|
|
// fetch won’t 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',
|
2021-01-03 14:25:43 +05:30
|
|
|
|
batchMax: config.batchMax || 10,
|
2019-09-04 21:01:54 +05:30
|
|
|
|
};
|
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
|
const uploadsLink = ApolloLink.split(
|
|
|
|
|
operation => operation.getContext().hasUpload || operation.getContext().isSingleRequest,
|
|
|
|
|
createUploadLink(httpOptions),
|
|
|
|
|
new BatchHttpLink(httpOptions),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const performanceBarLink = new ApolloLink((operation, forward) => {
|
|
|
|
|
return forward(operation).map(response => {
|
|
|
|
|
const httpResponse = operation.getContext().response;
|
|
|
|
|
|
|
|
|
|
if (PerformanceBarService.interceptor) {
|
|
|
|
|
PerformanceBarService.interceptor({
|
|
|
|
|
config: {
|
|
|
|
|
url: httpResponse.url,
|
|
|
|
|
},
|
|
|
|
|
headers: {
|
|
|
|
|
'x-request-id': httpResponse.headers.get('x-request-id'),
|
|
|
|
|
'x-gitlab-from-cache': httpResponse.headers.get('x-gitlab-from-cache'),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-07-31 22:56:46 +05:30
|
|
|
|
return new ApolloClient({
|
2020-05-24 23:13:21 +05:30
|
|
|
|
typeDefs: config.typeDefs,
|
2020-11-24 15:15:51 +05:30
|
|
|
|
link: ApolloLink.from([performanceBarLink, uploadsLink]),
|
2019-12-26 22:10:19 +05:30
|
|
|
|
cache: new InMemoryCache({
|
|
|
|
|
...config.cacheConfig,
|
|
|
|
|
freezeResults: config.assumeImmutableResults,
|
|
|
|
|
}),
|
2019-07-07 11:18:12 +05:30
|
|
|
|
resolvers,
|
2019-12-26 22:10:19 +05:30
|
|
|
|
assumeImmutableResults: config.assumeImmutableResults,
|
2020-03-13 15:44:24 +05:30
|
|
|
|
defaultOptions: {
|
|
|
|
|
query: {
|
|
|
|
|
fetchPolicy: config.fetchPolicy || fetchPolicies.CACHE_FIRST,
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-07-07 11:18:12 +05:30
|
|
|
|
});
|
2019-07-31 22:56:46 +05:30
|
|
|
|
};
|