Skip to content

Commit ff645a4

Browse files
committed
Refactor Apollo and GraphQL integrations.
1 parent 9fbb231 commit ff645a4

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

packages/tracing/src/integrations/node/apollo.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export class Apollo implements Integration {
4242
/**
4343
* Iterate over resolvers of the ApolloServer instance before schemas are constructed.
4444
*/
45-
fill(pkg.ApolloServerBase.prototype, 'constructSchema', function (orig: () => void) {
45+
fill(pkg.ApolloServerBase.prototype, 'constructSchema', function (orig: () => unknown) {
4646
return function (this: { config: { resolvers: ApolloModelResolvers[] } }) {
47-
this.config.resolvers = this.config.resolvers.map(model => {
47+
const resolvers = Array.isArray(this.config.resolvers) ? this.config.resolvers : [this.config.resolvers];
48+
49+
this.config.resolvers = resolvers.map(model => {
4850
Object.keys(model).forEach(resolverGroupName => {
4951
Object.keys(model[resolverGroupName]).forEach(resolverName => {
5052
if (typeof model[resolverGroupName][resolverName] !== 'function') {
@@ -79,23 +81,20 @@ function wrapResolver(
7981
const parentSpan = scope?.getSpan();
8082
const span = parentSpan?.startChild({
8183
description: `${resolverGroupName}.${resolverName}`,
82-
op: 'apollo',
84+
op: 'db.graphql.apollo',
8385
});
8486

85-
scope?.setSpan(span);
86-
8787
const rv = orig.call(this, ...args);
8888

8989
if (isThenable(rv)) {
90-
return (rv as Promise<unknown>).then((res: unknown) => {
90+
return rv.then((res: unknown) => {
9191
span?.finish();
92-
scope?.setSpan(parentSpan);
9392
return res;
9493
});
9594
}
9695

9796
span?.finish();
98-
scope?.setSpan(parentSpan);
97+
9998
return rv;
10099
};
101100
});

packages/tracing/src/integrations/node/graphql.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Hub } from '@sentry/hub';
22
import { EventProcessor, Integration } from '@sentry/types';
3-
import { fill, loadModule, logger } from '@sentry/utils';
3+
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
44

55
/** Tracing integration for graphql package */
66
export class GraphQL implements Integration {
@@ -34,18 +34,25 @@ export class GraphQL implements Integration {
3434

3535
const span = parentSpan?.startChild({
3636
description: 'execute',
37-
op: 'graphql',
37+
op: 'db.graphql',
3838
});
3939

4040
scope?.setSpan(span);
4141

42-
const rv = orig.call(this, ...args) as Promise<unknown>;
42+
const rv = orig.call(this, ...args);
4343

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

0 commit comments

Comments
 (0)