debian-mirror-gitlab/app/assets/javascripts/lib/graphql.js

121 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-07-07 11:18:12 +05:30
import { InMemoryCache } from 'apollo-cache-inmemory';
2021-03-11 19:13:27 +05:30
import { ApolloClient } from 'apollo-client';
2019-09-04 21:01:54 +05:30
import { ApolloLink } from 'apollo-link';
import { BatchHttpLink } from 'apollo-link-batch-http';
2021-04-17 20:07:23 +05:30
import { createHttpLink } from 'apollo-link-http';
2021-03-11 19:13:27 +05:30
import { createUploadLink } from 'apollo-upload-client';
2021-06-08 01:23:25 +05:30
import ActionCableLink from '~/actioncable_link';
2021-04-29 21:17:54 +05:30
import { apolloCaptchaLink } from '~/captcha/apollo_captcha_link';
2021-03-11 19:13:27 +05:30
import { StartupJSLink } from '~/lib/utils/apollo_startup_js_link';
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 = {}) => {
2021-06-08 01:23:25 +05:30
const {
assumeImmutableResults,
baseUrl,
batchMax = 10,
cacheConfig,
fetchPolicy = fetchPolicies.CACHE_FIRST,
typeDefs,
path = '/api/graphql',
useGet = false,
} = config;
let uri = `${gon.relative_url_root || ''}${path}`;
2019-07-31 22:56:46 +05:30
2021-06-08 01:23:25 +05:30
if (baseUrl) {
2019-07-31 22:56:46 +05:30
// Prepend baseUrl and ensure that `///` are replaced with `/`
2021-06-08 01:23:25 +05:30
uri = `${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 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',
2021-06-08 01:23:25 +05:30
batchMax,
2019-09-04 21:01:54 +05:30
};
2021-03-11 19:13:27 +05:30
const requestCounterLink = new ApolloLink((operation, forward) => {
window.pendingApolloRequests = window.pendingApolloRequests || 0;
window.pendingApolloRequests += 1;
return forward(operation).map((response) => {
window.pendingApolloRequests -= 1;
return response;
});
});
2020-11-24 15:15:51 +05:30
const uploadsLink = ApolloLink.split(
2021-03-08 18:12:59 +05:30
(operation) => operation.getContext().hasUpload || operation.getContext().isSingleRequest,
2020-11-24 15:15:51 +05:30
createUploadLink(httpOptions),
2021-06-08 01:23:25 +05:30
useGet ? createHttpLink(httpOptions) : new BatchHttpLink(httpOptions),
2020-11-24 15:15:51 +05:30
);
const performanceBarLink = new ApolloLink((operation, forward) => {
2021-03-08 18:12:59 +05:30
return forward(operation).map((response) => {
2020-11-24 15:15:51 +05:30
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;
});
});
2021-06-08 01:23:25 +05:30
const hasSubscriptionOperation = ({ query: { definitions } }) => {
return definitions.some(
({ kind, operation }) => kind === 'OperationDefinition' && operation === 'subscription',
);
};
const appLink = ApolloLink.split(
hasSubscriptionOperation,
new ActionCableLink(),
ApolloLink.from([
2021-03-11 19:13:27 +05:30
requestCounterLink,
performanceBarLink,
new StartupJSLink(),
2021-04-29 21:17:54 +05:30
apolloCaptchaLink,
2021-03-11 19:13:27 +05:30
uploadsLink,
]),
2021-06-08 01:23:25 +05:30
);
return new ApolloClient({
typeDefs,
link: appLink,
2019-12-26 22:10:19 +05:30
cache: new InMemoryCache({
2021-06-08 01:23:25 +05:30
...cacheConfig,
freezeResults: assumeImmutableResults,
2019-12-26 22:10:19 +05:30
}),
2019-07-07 11:18:12 +05:30
resolvers,
2021-06-08 01:23:25 +05:30
assumeImmutableResults,
2020-03-13 15:44:24 +05:30
defaultOptions: {
query: {
2021-06-08 01:23:25 +05:30
fetchPolicy,
2020-03-13 15:44:24 +05:30
},
},
2019-07-07 11:18:12 +05:30
});
2019-07-31 22:56:46 +05:30
};