-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Replace OperationBreadcrumb class with plain GraphQLBreadcrumb…
… object BREAKING CHANGE: rename response key to fetchResult add operationName key add url key
- Loading branch information
Showing
4 changed files
with
223 additions
and
316 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { FetchResult, Operation } from '@apollo/client/core'; | ||
import { Breadcrumb as SentryBreadcrumb } from '@sentry/types'; | ||
import dotProp from 'dot-prop'; | ||
import { print } from 'graphql'; | ||
|
||
import { extractDefinition } from './operation'; | ||
import { FullOptions, AttachBreadcrumbsOptions } from './options'; | ||
|
||
export interface BreadcrumbData { | ||
url?: string; | ||
query?: string; | ||
variables?: Record<string, unknown>; | ||
operationName?: string; | ||
fetchResult?: FetchResult; | ||
error?: Error; | ||
cache?: Record<string, unknown>; | ||
context?: Record<string, unknown>; | ||
} | ||
|
||
export interface GraphQLBreadcrumb extends SentryBreadcrumb { | ||
data: BreadcrumbData; | ||
} | ||
|
||
export function makeBreadcrumb( | ||
operation: Operation, | ||
options: FullOptions, | ||
): GraphQLBreadcrumb { | ||
// We validated this is set before calling this function | ||
const attachBreadcrumbs = options.attachBreadcrumbs as AttachBreadcrumbsOptions; | ||
|
||
const definition = extractDefinition(operation); | ||
|
||
const data: BreadcrumbData = {}; | ||
|
||
const uri = options.uri; | ||
if (uri) { | ||
data.url = uri; | ||
} | ||
|
||
const operationName = definition.name?.value; | ||
if (operationName) { | ||
data.operationName = operationName; | ||
} | ||
|
||
if (attachBreadcrumbs.includeQuery) { | ||
data.query = | ||
// The document might have been parsed with `noLocation: true` | ||
definition.loc?.source?.body ?? print(definition); | ||
} | ||
|
||
if (attachBreadcrumbs.includeVariables) { | ||
data.variables = operation.variables; | ||
} | ||
|
||
if (attachBreadcrumbs.includeCache) { | ||
data.cache = | ||
(operation.getContext().cache?.data?.data as Record<string, unknown>) ?? | ||
undefined; | ||
} | ||
|
||
const contextKeys = attachBreadcrumbs.includeContext; | ||
if (contextKeys) { | ||
data.context = extractKeys(operation.getContext(), contextKeys); | ||
} | ||
|
||
return { | ||
type: 'http', | ||
category: `graphql.${definition.operation}`, | ||
data, | ||
}; | ||
} | ||
|
||
function extractKeys( | ||
context: Record<string, unknown>, | ||
keys: Array<string>, | ||
): Record<string, unknown> { | ||
const result: Record<string, unknown> = {}; | ||
|
||
keys.forEach((key) => { | ||
result[key] = dotProp.get(context, key); | ||
}); | ||
|
||
return result; | ||
} |
Oops, something went wrong.