Welcome to mirror list, hosted at ThFree Co, Russian Federation.

instrumentation_link.js « apollo « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2ab364557b8fedab847240055b6b273240ebccd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
  });
});