Skip to content

Commit

Permalink
feat!: Replace OperationBreadcrumb class with plain GraphQLBreadcrumb…
Browse files Browse the repository at this point in the history
… object

BREAKING CHANGE: rename response key to fetchResult

add operationName key

add url key
  • Loading branch information
spawnia committed Feb 11, 2021
1 parent 370b9b5 commit 036b746
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 316 deletions.
215 changes: 0 additions & 215 deletions src/OperationBreadcrumb.ts

This file was deleted.

84 changes: 84 additions & 0 deletions src/breadcrumb.ts
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;
}
Loading

0 comments on commit 036b746

Please sign in to comment.