Skip to content

Commit 493f428

Browse files
committed
refactor: move errorPropagation to ValidatedExecutionArgs (#4531)
appropriate exposes the error propagation mode as part of the execution arguments, as the context (besides the reference to args) should only store mutable data
1 parent b21afb6 commit 493f428

2 files changed

Lines changed: 13 additions & 28 deletions

File tree

src/execution/entrypoints.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
} from '../language/ast.js';
1515
import { Kind } from '../language/kinds.js';
1616

17+
import { GraphQLDisableErrorPropagationDirective } from '../type/directives.js';
1718
import type {
1819
GraphQLFieldResolver,
1920
GraphQLTypeResolver,
@@ -282,6 +283,12 @@ export function validateExecutionArgs(
282283
return variableValuesOrErrors.errors;
283284
}
284285

286+
const errorPropagation =
287+
operation.directives?.find(
288+
(directive) =>
289+
directive.name.value === GraphQLDisableErrorPropagationDirective.name,
290+
) === undefined;
291+
285292
return {
286293
schema,
287294
fragmentDefinitions,
@@ -295,6 +302,7 @@ export function validateExecutionArgs(
295302
subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver,
296303
perEventExecutor: perEventExecutor ?? executeSubscriptionEvent,
297304
hideSuggestions,
305+
errorPropagation,
298306
abortSignal: args.abortSignal ?? undefined,
299307
};
300308
}

src/execution/execute.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
isNonNullType,
4040
isObjectType,
4141
} from '../type/definition.js';
42-
import { GraphQLDisableErrorPropagationDirective } from '../type/directives.js';
4342
import type { GraphQLSchema } from '../type/schema.js';
4443

4544
import {
@@ -134,6 +133,7 @@ export interface ValidatedExecutionArgs {
134133
validatedExecutionArgs: ValidatedExecutionArgs,
135134
) => PromiseOrValue<ExecutionResult>;
136135
hideSuggestions: boolean;
136+
errorPropagation: boolean;
137137
abortSignal: AbortSignal | undefined;
138138
}
139139

@@ -181,7 +181,6 @@ export interface ExecutionContext {
181181
collectedErrors: CollectedErrors;
182182
abortSignalListener: AbortSignalListener | undefined;
183183
completed: boolean;
184-
errorPropagation: boolean;
185184
}
186185

187186
/**
@@ -209,30 +208,6 @@ export interface FormattedExecutionResult<
209208
extensions?: TExtensions;
210209
}
211210

212-
function errorPropagation(operation: OperationDefinitionNode): boolean {
213-
const directiveNode = operation.directives?.find(
214-
(directive) =>
215-
directive.name.value === GraphQLDisableErrorPropagationDirective.name,
216-
);
217-
218-
return directiveNode === undefined;
219-
}
220-
221-
/**
222-
* Implements the "Executing operations" section of the spec.
223-
*
224-
* Returns a Promise that will eventually resolve to the data described by
225-
* The "Response" section of the GraphQL specification.
226-
*
227-
* If errors are encountered while executing a GraphQL field, only that
228-
* field and its descendants will be omitted, and sibling fields will still
229-
* be executed. An execution which encounters errors will still result in a
230-
* resolved Promise.
231-
*
232-
* Errors from sub-fields of a NonNull type may propagate to the top level,
233-
* at which point we still log the error and null the parent field, which
234-
* in this case is the entire response.
235-
*/
236211
export function executeQueryOrMutationOrSubscriptionEvent(
237212
validatedExecutionArgs: ValidatedExecutionArgs,
238213
): PromiseOrValue<ExecutionResult> {
@@ -244,7 +219,6 @@ export function executeQueryOrMutationOrSubscriptionEvent(
244219
? new AbortSignalListener(abortSignal)
245220
: undefined,
246221
completed: false,
247-
errorPropagation: errorPropagation(validatedExecutionArgs.operation),
248222
};
249223
try {
250224
const {
@@ -575,7 +549,10 @@ function handleFieldError(
575549

576550
// If the field type is non-nullable, then it is resolved without any
577551
// protection from errors, however it still properly locates the error.
578-
if (exeContext.errorPropagation && isNonNullType(returnType)) {
552+
if (
553+
exeContext.validatedExecutionArgs.errorPropagation &&
554+
isNonNullType(returnType)
555+
) {
579556
throw error;
580557
}
581558

0 commit comments

Comments
 (0)