diff options
Diffstat (limited to 'app/assets/javascripts/lib/apollo/instrumentation_link.js')
-rw-r--r-- | app/assets/javascripts/lib/apollo/instrumentation_link.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/apollo/instrumentation_link.js b/app/assets/javascripts/lib/apollo/instrumentation_link.js new file mode 100644 index 00000000000..2ab364557b8 --- /dev/null +++ b/app/assets/javascripts/lib/apollo/instrumentation_link.js @@ -0,0 +1,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); + }); +}); |