debian-mirror-gitlab/app/assets/javascripts/lib/apollo/instrumentation_link.js

30 lines
821 B
JavaScript
Raw Normal View History

2021-11-11 11:23:49 +05:30
import { ApolloLink } from 'apollo-link';
import { memoize } from 'lodash';
export const FEATURE_CATEGORY_HEADER = 'x-gitlab-feature-category';
/**
* Returns the ApolloLink (or null) used to add instrumentation metadata to the GraphQL request.
*
* - The result will be null if the `feature_category` cannot be found.
* - The result is memoized since the `feature_category` is the same for the entire page.
*/
export const getInstrumentationLink = memoize(() => {
const { feature_category: featureCategory } = gon;
if (!featureCategory) {
return null;
}
return new ApolloLink((operation, forward) => {
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
[FEATURE_CATEGORY_HEADER]: featureCategory,
},
}));
return forward(operation);
});
});