|
| 1 | +import { FetchResult, Operation } from '@apollo/client/core'; |
| 2 | +import { Breadcrumb as SentryBreadcrumb } from '@sentry/types'; |
| 3 | +import dotProp from 'dot-prop'; |
| 4 | +import { print } from 'graphql'; |
| 5 | + |
| 6 | +import { extractDefinition } from './operation'; |
| 7 | +import { FullOptions, AttachBreadcrumbsOptions } from './options'; |
| 8 | + |
| 9 | +export interface BreadcrumbData { |
| 10 | + url?: string; |
| 11 | + query?: string; |
| 12 | + variables?: Record<string, unknown>; |
| 13 | + operationName?: string; |
| 14 | + fetchResult?: FetchResult; |
| 15 | + error?: Error; |
| 16 | + cache?: Record<string, unknown>; |
| 17 | + context?: Record<string, unknown>; |
| 18 | +} |
| 19 | + |
| 20 | +export interface GraphQLBreadcrumb extends SentryBreadcrumb { |
| 21 | + data: BreadcrumbData; |
| 22 | +} |
| 23 | + |
| 24 | +export function makeBreadcrumb( |
| 25 | + operation: Operation, |
| 26 | + options: FullOptions, |
| 27 | +): GraphQLBreadcrumb { |
| 28 | + // We validated this is set before calling this function |
| 29 | + const attachBreadcrumbs = options.attachBreadcrumbs as AttachBreadcrumbsOptions; |
| 30 | + |
| 31 | + const definition = extractDefinition(operation); |
| 32 | + |
| 33 | + const data: BreadcrumbData = {}; |
| 34 | + |
| 35 | + const uri = options.uri; |
| 36 | + if (uri) { |
| 37 | + data.url = uri; |
| 38 | + } |
| 39 | + |
| 40 | + const operationName = definition.name?.value; |
| 41 | + if (operationName) { |
| 42 | + data.operationName = operationName; |
| 43 | + } |
| 44 | + |
| 45 | + if (attachBreadcrumbs.includeQuery) { |
| 46 | + data.query = |
| 47 | + // The document might have been parsed with `noLocation: true` |
| 48 | + definition.loc?.source?.body ?? print(definition); |
| 49 | + } |
| 50 | + |
| 51 | + if (attachBreadcrumbs.includeVariables) { |
| 52 | + data.variables = operation.variables; |
| 53 | + } |
| 54 | + |
| 55 | + if (attachBreadcrumbs.includeCache) { |
| 56 | + data.cache = |
| 57 | + (operation.getContext().cache?.data?.data as Record<string, unknown>) ?? |
| 58 | + undefined; |
| 59 | + } |
| 60 | + |
| 61 | + const contextKeys = attachBreadcrumbs.includeContext; |
| 62 | + if (contextKeys) { |
| 63 | + data.context = extractKeys(operation.getContext(), contextKeys); |
| 64 | + } |
| 65 | + |
| 66 | + return { |
| 67 | + type: 'http', |
| 68 | + category: `graphql.${definition.operation}`, |
| 69 | + data, |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +function extractKeys( |
| 74 | + context: Record<string, unknown>, |
| 75 | + keys: Array<string>, |
| 76 | +): Record<string, unknown> { |
| 77 | + const result: Record<string, unknown> = {}; |
| 78 | + |
| 79 | + keys.forEach((key) => { |
| 80 | + result[key] = dotProp.get(context, key); |
| 81 | + }); |
| 82 | + |
| 83 | + return result; |
| 84 | +} |
0 commit comments