@@ -13,6 +13,8 @@ import { promiseForObject } from '../jsutils/promiseForObject';
1313import type { PromiseOrValue } from '../jsutils/PromiseOrValue' ;
1414import { promiseReduce } from '../jsutils/promiseReduce' ;
1515
16+ import type { GraphQLErrorBehavior } from '../error/ErrorBehavior' ;
17+ import { isErrorBehavior } from '../error/ErrorBehavior' ;
1618import type { GraphQLFormattedError } from '../error/GraphQLError' ;
1719import { GraphQLError } from '../error/GraphQLError' ;
1820import { locatedError } from '../error/locatedError' ;
@@ -115,6 +117,7 @@ export interface ExecutionContext {
115117 typeResolver : GraphQLTypeResolver < any , any > ;
116118 subscribeFieldResolver : GraphQLFieldResolver < any , any > ;
117119 errors : Array < GraphQLError > ;
120+ errorBehavior : GraphQLErrorBehavior ;
118121}
119122
120123/**
@@ -130,6 +133,7 @@ export interface ExecutionResult<
130133> {
131134 errors ?: ReadonlyArray < GraphQLError > ;
132135 data ?: TData | null ;
136+ onError ?: GraphQLErrorBehavior ;
133137 extensions ?: TExtensions ;
134138}
135139
@@ -152,6 +156,15 @@ export interface ExecutionArgs {
152156 fieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ;
153157 typeResolver ?: Maybe < GraphQLTypeResolver < any , any > > ;
154158 subscribeFieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ;
159+ /**
160+ * Experimental. Set to NO_PROPAGATE to prevent error propagation. Set to ABORT to
161+ * abort a request when any error occurs.
162+ *
163+ * Default: PROPAGATE
164+ *
165+ * @experimental
166+ */
167+ onError ?: GraphQLErrorBehavior ;
155168 /** Additional execution options. */
156169 options ?: {
157170 /** Set the maximum number of errors allowed for coercing (defaults to 50). */
@@ -291,9 +304,18 @@ export function buildExecutionContext(
291304 fieldResolver,
292305 typeResolver,
293306 subscribeFieldResolver,
307+ onError,
294308 options,
295309 } = args ;
296310
311+ if ( onError != null && ! isErrorBehavior ( onError ) ) {
312+ return [
313+ new GraphQLError (
314+ 'Unsupported `onError` value; supported values are `PROPAGATE`, `NO_PROPAGATE` and `ABORT`.' ,
315+ ) ,
316+ ] ;
317+ }
318+
297319 let operation : OperationDefinitionNode | undefined ;
298320 const fragments : ObjMap < FragmentDefinitionNode > = Object . create ( null ) ;
299321 for ( const definition of document . definitions ) {
@@ -353,6 +375,7 @@ export function buildExecutionContext(
353375 typeResolver : typeResolver ?? defaultTypeResolver ,
354376 subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
355377 errors : [ ] ,
378+ errorBehavior : onError ?? 'PROPAGATE' ,
356379 } ;
357380}
358381
@@ -591,6 +614,7 @@ export function buildResolveInfo(
591614 rootValue : exeContext . rootValue ,
592615 operation : exeContext . operation ,
593616 variableValues : exeContext . variableValues ,
617+ errorBehavior : exeContext . errorBehavior ,
594618 } ;
595619}
596620
@@ -599,10 +623,25 @@ function handleFieldError(
599623 returnType : GraphQLOutputType ,
600624 exeContext : ExecutionContext ,
601625) : null {
602- // If the field type is non-nullable, then it is resolved without any
603- // protection from errors, however it still properly locates the error.
604- if ( isNonNullType ( returnType ) ) {
626+ if ( exeContext . errorBehavior === 'PROPAGATE' ) {
627+ // If the field type is non-nullable, then it is resolved without any
628+ // protection from errors, however it still properly locates the error.
629+ // Note: semantic non-null types are treated as nullable for the purposes
630+ // of error handling.
631+ if ( isNonNullType ( returnType ) ) {
632+ throw error ;
633+ }
634+ } else if ( exeContext . errorBehavior === 'ABORT' ) {
635+ // In this mode, any error aborts the request
605636 throw error ;
637+ } else if ( exeContext . errorBehavior === 'NO_PROPAGATE' ) {
638+ // In this mode, the client takes responsibility for error handling, so we
639+ // treat the field as if it were nullable.
640+ } else {
641+ invariant (
642+ false ,
643+ 'Unexpected errorBehavior setting: ' + inspect ( exeContext . errorBehavior ) ,
644+ ) ;
606645 }
607646
608647 // Otherwise, error protection is applied, logging the error and resolving
0 commit comments