Skip to content

Commit 56e42f0

Browse files
committed
Simplify and document a bit.
1 parent 656f0de commit 56e42f0

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

packages/tracing/src/integrations/apollo.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
/* eslint-disable no-console */
31
/* eslint-disable no-debugger */
42
import { Hub } from '@sentry/hub';
53
import { EventProcessor, Integration } from '@sentry/types';
@@ -9,11 +7,11 @@ type ApolloResolverGroup = {
97
[key: string]: () => any;
108
};
119

12-
type ApolloField = {
10+
type ApolloModelResolvers = {
1311
[key: string]: ApolloResolverGroup;
1412
};
1513

16-
/** Tracing integration for Apollo package */
14+
/** Tracing integration for Apollo */
1715
export class Apollo implements Integration {
1816
/**
1917
* @inheritDoc
@@ -37,26 +35,28 @@ export class Apollo implements Integration {
3735
};
3836
}>(`apollo-server-core`);
3937

40-
debugger;
4138
if (!pkg) {
42-
logger.error(`Apollo Integration was unable to require apollo package.`);
39+
logger.error('Apollo Integration was unable to require apollo-server-core package.');
4340
return;
4441
}
4542

43+
/**
44+
* Iterate over resolvers of the ApolloServer instance before schemas are constructed.
45+
*/
4646
fill(pkg.ApolloServerBase.prototype, 'constructSchema', function(orig: () => void) {
47-
return function(this: { config: { resolvers: ApolloField[] } }) {
48-
this.config.resolvers = this.config.resolvers.map(field => {
49-
Object.keys(field).forEach(resolverGroupName => {
50-
Object.keys(field[resolverGroupName]).forEach(resolverName => {
51-
if (typeof field[resolverGroupName][resolverName] !== 'function') {
47+
return function(this: { config: { resolvers: ApolloModelResolvers[] } }) {
48+
this.config.resolvers = this.config.resolvers.map(model => {
49+
Object.keys(model).forEach(resolverGroupName => {
50+
Object.keys(model[resolverGroupName]).forEach(resolverName => {
51+
if (typeof model[resolverGroupName][resolverName] !== 'function') {
5252
return;
5353
}
5454

55-
patchResolver(field, resolverGroupName, resolverName, getCurrentHub);
55+
wrapResolver(model, resolverGroupName, resolverName, getCurrentHub);
5656
});
5757
});
5858

59-
return field;
59+
return model;
6060
});
6161

6262
return orig.call(this);
@@ -66,18 +66,15 @@ export class Apollo implements Integration {
6666
}
6767

6868
/**
69-
*
70-
* @param field
71-
* @param resolverGroupName
72-
* @param resolverName
69+
* Wrap a single resolver which can be a parent of other resolvers and/or db operations.
7370
*/
74-
function patchResolver(
75-
field: ApolloField,
71+
function wrapResolver(
72+
model: ApolloModelResolvers,
7673
resolverGroupName: string,
7774
resolverName: string,
7875
getCurrentHub: () => Hub,
7976
): void {
80-
fill(field[resolverGroupName], resolverName, function(orig: () => unknown | Promise<unknown>) {
77+
fill(model[resolverGroupName], resolverName, function(orig: () => unknown | Promise<unknown>) {
8178
return function(this: unknown, ...args: unknown[]) {
8279
const scope = getCurrentHub().getScope();
8380
const parentSpan = scope?.getSpan();

packages/tracing/src/integrations/graphql.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-debugger */
22
import { Hub } from '@sentry/hub';
33
import { EventProcessor, Integration } from '@sentry/types';
4-
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
4+
import { fill, loadModule, logger } from '@sentry/utils';
55

66
/** Tracing integration for graphql package */
77
export class GraphQL implements Integration {
@@ -24,14 +24,13 @@ export class GraphQL implements Integration {
2424
}>(`graphql/execution/execute.js`);
2525

2626
if (!pkg) {
27-
logger.error(`GraphQL Integration was unable to require @graphql/execution package.`);
27+
logger.error('GraphQL Integration was unable to require graphql/execution package.');
2828
return;
2929
}
3030

3131
fill(pkg, 'execute', function(orig: () => void | Promise<unknown>) {
3232
return function(this: unknown, ...args: unknown[]) {
33-
const hub = getCurrentHub();
34-
const scope = hub.getScope();
33+
const scope = getCurrentHub().getScope();
3534
const parentSpan = scope?.getSpan();
3635

3736
const span = parentSpan?.startChild({
@@ -43,17 +42,11 @@ export class GraphQL implements Integration {
4342

4443
const rv = orig.call(this, ...args) as Promise<unknown>;
4544

46-
if (isThenable(rv)) {
47-
return rv.then((res: unknown) => {
48-
span?.finish();
49-
scope?.setSpan(parentSpan);
50-
return res;
51-
});
52-
}
53-
54-
span?.finish();
55-
scope?.setSpan(parentSpan);
56-
return rv;
45+
return rv.then((res: unknown) => {
46+
span?.finish();
47+
scope?.setSpan(parentSpan);
48+
return res;
49+
});
5750
};
5851
});
5952
}

0 commit comments

Comments
 (0)