Skip to content

Commit 252abf1

Browse files
committed
Allow the schema to set a default error behavior
1 parent a5474f4 commit 252abf1

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/execution/execute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export function buildExecutionContext(
375375
typeResolver: typeResolver ?? defaultTypeResolver,
376376
subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver,
377377
errors: [],
378-
errorBehavior: onError ?? 'PROPAGATE',
378+
errorBehavior: onError ?? schema.defaultErrorBehavior,
379379
};
380380
}
381381

src/type/schema.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import {
3131
import type { GraphQLDirective } from './directives';
3232
import { isDirective, specifiedDirectives } from './directives';
3333
import { __Schema } from './introspection';
34+
import type { GraphQLErrorBehavior } from '../error';
35+
import { isErrorBehavior } from '../error/ErrorBehavior';
3436

3537
/**
3638
* Test if the given value is a GraphQL schema.
@@ -129,6 +131,8 @@ export interface GraphQLSchemaExtensions {
129131
*/
130132
export class GraphQLSchema {
131133
description: Maybe<string>;
134+
/** @experimental */
135+
readonly defaultErrorBehavior: GraphQLErrorBehavior;
132136
extensions: Readonly<GraphQLSchemaExtensions>;
133137
astNode: Maybe<SchemaDefinitionNode>;
134138
extensionASTNodes: ReadonlyArray<SchemaExtensionNode>;
@@ -163,8 +167,15 @@ export class GraphQLSchema {
163167
'"directives" must be Array if provided but got: ' +
164168
`${inspect(config.directives)}.`,
165169
);
170+
devAssert(
171+
!config.defaultErrorBehavior ||
172+
isErrorBehavior(config.defaultErrorBehavior),
173+
'"defaultErrorBehavior" must be one of "NO_PROPAGATE", "PROPAGATE" or "ABORT", but got: ' +
174+
`${inspect(config.defaultErrorBehavior)}.`,
175+
);
166176

167177
this.description = config.description;
178+
this.defaultErrorBehavior = config.defaultErrorBehavior ?? 'PROPAGATE';
168179
this.extensions = toObjMap(config.extensions);
169180
this.astNode = config.astNode;
170181
this.extensionASTNodes = config.extensionASTNodes ?? [];
@@ -386,6 +397,20 @@ export interface GraphQLSchemaConfig extends GraphQLSchemaValidationOptions {
386397
subscription?: Maybe<GraphQLObjectType>;
387398
types?: Maybe<ReadonlyArray<GraphQLNamedType>>;
388399
directives?: Maybe<ReadonlyArray<GraphQLDirective>>;
400+
/**
401+
* Experimental. Defines the default GraphQL error behavior when the
402+
* GraphQLArgs does not include an `onError` property.
403+
*
404+
* Set to NO_PROPAGATE if your schema only needs to support modern
405+
* "error-handling" clients.
406+
*
407+
* It is not recommended to set this to ABORT.
408+
*
409+
* Default: PROPAGATE
410+
*
411+
* @experimental
412+
*/
413+
defaultErrorBehavior?: GraphQLErrorBehavior;
389414
extensions?: Maybe<Readonly<GraphQLSchemaExtensions>>;
390415
astNode?: Maybe<SchemaDefinitionNode>;
391416
extensionASTNodes?: Maybe<ReadonlyArray<SchemaExtensionNode>>;

0 commit comments

Comments
 (0)