Skip to content

Commit 06f3baf

Browse files
committed
move tracing back to executeField
1 parent a559d52 commit 06f3baf

4 files changed

Lines changed: 18 additions & 52 deletions

File tree

src/diagnostics.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,3 @@ export function traceMixed<T>(
174174
});
175175
});
176176
}
177-
178-
/**
179-
* Check if a channel is defined and has subscribers.
180-
*/
181-
export function shouldTrace(
182-
channel: MinimalTracingChannel | undefined,
183-
): channel is MinimalTracingChannel {
184-
return channel?.hasSubscribers === true;
185-
}

src/execution/Executor.ts

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ import {
4444
} from '../type/definition.js';
4545
import type { GraphQLSchema } from '../type/schema.js';
4646

47-
import type { MinimalTracingChannel } from '../diagnostics.js';
48-
import { resolveChannel, shouldTrace, traceMixed } from '../diagnostics.js';
47+
import { resolveChannel, traceMixed } from '../diagnostics.js';
4948

5049
import { AbortedGraphQLExecutionError } from './AbortedGraphQLExecutionError.js';
5150
import { withCancellation } from './cancellablePromise.js';
@@ -481,9 +480,6 @@ export class Executor<
481480
groupedFieldSet: GroupedFieldSet,
482481
positionContext: TPositionContext | undefined,
483482
): PromiseOrValue<ObjMap<unknown>> {
484-
const tracingChannel = shouldTrace(resolveChannel)
485-
? resolveChannel
486-
: undefined;
487483
return promiseReduce(
488484
groupedFieldSet,
489485
(results, [responseName, fieldDetailsList]) => {
@@ -497,7 +493,6 @@ export class Executor<
497493
fieldDetailsList,
498494
fieldPath,
499495
positionContext,
500-
tracingChannel,
501496
);
502497
if (result === undefined) {
503498
return results;
@@ -528,9 +523,6 @@ export class Executor<
528523
): PromiseOrValue<ObjMap<unknown>> {
529524
const results = Object.create(null);
530525
let containsPromise = false;
531-
const tracingChannel = shouldTrace(resolveChannel)
532-
? resolveChannel
533-
: undefined;
534526

535527
try {
536528
for (const [responseName, fieldDetailsList] of groupedFieldSet) {
@@ -541,7 +533,6 @@ export class Executor<
541533
fieldDetailsList,
542534
fieldPath,
543535
positionContext,
544-
tracingChannel,
545536
);
546537

547538
if (result !== undefined) {
@@ -583,7 +574,6 @@ export class Executor<
583574
fieldDetailsList: FieldDetailsList,
584575
path: Path,
585576
positionContext: TPositionContext | undefined,
586-
tracingChannel: MinimalTracingChannel | undefined,
587577
): PromiseOrValue<unknown> {
588578
const validatedExecutionArgs = this.validatedExecutionArgs;
589579
const { schema, contextValue, variableValues, hideSuggestions } =
@@ -597,7 +587,18 @@ export class Executor<
597587
}
598588

599589
const returnType = fieldDef.type;
600-
const resolveFn = fieldDef.resolve ?? validatedExecutionArgs.fieldResolver;
590+
let resolveFn = fieldDef.resolve ?? validatedExecutionArgs.fieldResolver;
591+
592+
if (resolveChannel?.hasSubscribers) {
593+
const channel = resolveChannel;
594+
const originalResolveFn = resolveFn;
595+
resolveFn = (s, args, c, info) =>
596+
traceMixed(
597+
channel,
598+
this.buildResolveCtx(args, info, fieldDef.resolve === undefined),
599+
() => originalResolveFn(s, args, c, info),
600+
);
601+
}
601602

602603
const info = buildResolveInfo(
603604
validatedExecutionArgs,
@@ -625,17 +626,7 @@ export class Executor<
625626
// The resolve function's optional third argument is a context value that
626627
// is provided to every resolve function within an execution. It is commonly
627628
// used to represent an authenticated user, or request-specific caches.
628-
const result = tracingChannel
629-
? this.invokeResolverWithTracing(
630-
tracingChannel,
631-
resolveFn,
632-
source,
633-
args,
634-
contextValue,
635-
info,
636-
fieldDef.resolve === undefined,
637-
)
638-
: resolveFn(source, args, contextValue, info);
629+
const result = resolveFn(source, args, contextValue, info);
639630

640631
if (isPromiseLike(result)) {
641632
return this.completePromisedValue(
@@ -672,22 +663,6 @@ export class Executor<
672663
}
673664
}
674665

675-
invokeResolverWithTracing(
676-
tracingChannel: MinimalTracingChannel,
677-
resolveFn: GraphQLFieldResolver<unknown, unknown>,
678-
source: unknown,
679-
args: { readonly [argument: string]: unknown },
680-
contextValue: unknown,
681-
info: GraphQLResolveInfo,
682-
isTrivialResolver: boolean,
683-
): PromiseOrValue<unknown> {
684-
return traceMixed(
685-
tracingChannel,
686-
this.buildResolveCtx(args, info, isTrivialResolver),
687-
() => resolveFn(source, args, contextValue, info),
688-
);
689-
}
690-
691666
/**
692667
* Build a graphql:resolve channel context for a single field invocation.
693668
*

src/language/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Maybe } from '../jsutils/Maybe.js';
33
import type { GraphQLError } from '../error/GraphQLError.js';
44
import { syntaxError } from '../error/syntaxError.js';
55

6-
import { parseChannel, shouldTrace } from '../diagnostics.js';
6+
import { parseChannel } from '../diagnostics.js';
77

88
import type {
99
ArgumentCoordinateNode,
@@ -134,7 +134,7 @@ export function parse(
134134
source: string | Source,
135135
options?: ParseOptions,
136136
): DocumentNode {
137-
return shouldTrace(parseChannel)
137+
return parseChannel?.hasSubscribers
138138
? parseChannel.traceSync(() => parseImpl(source, options), { source })
139139
: parseImpl(source, options);
140140
}

src/validation/validate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { assertValidSchema } from '../type/validate.js';
1212

1313
import { TypeInfo, visitWithTypeInfo } from '../utilities/TypeInfo.js';
1414

15-
import { shouldTrace, validateChannel } from '../diagnostics.js';
15+
import { validateChannel } from '../diagnostics.js';
1616

1717
import { specifiedRules, specifiedSDLRules } from './specifiedRules.js';
1818
import type { SDLValidationRule, ValidationRule } from './ValidationContext.js';
@@ -63,7 +63,7 @@ export function validate(
6363
rules: ReadonlyArray<ValidationRule> = specifiedRules,
6464
options?: ValidationOptions,
6565
): ReadonlyArray<GraphQLError> {
66-
return shouldTrace(validateChannel)
66+
return validateChannel?.hasSubscribers
6767
? validateChannel.traceSync(
6868
() => validateImpl(schema, documentAST, rules, options),
6969
{ schema, document: documentAST },

0 commit comments

Comments
 (0)