@@ -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}
156169
157170/**
@@ -286,8 +299,17 @@ export function buildExecutionContext(
286299 fieldResolver,
287300 typeResolver,
288301 subscribeFieldResolver,
302+ onError,
289303 } = args ;
290304
305+ if ( onError != null && ! isErrorBehavior ( onError ) ) {
306+ return [
307+ new GraphQLError (
308+ 'Unsupported `onError` value; supported values are `PROPAGATE`, `NO_PROPAGATE` and `ABORT`.' ,
309+ ) ,
310+ ] ;
311+ }
312+
291313 let operation : OperationDefinitionNode | undefined ;
292314 const fragments : ObjMap < FragmentDefinitionNode > = Object . create ( null ) ;
293315 for ( const definition of document . definitions ) {
@@ -347,6 +369,7 @@ export function buildExecutionContext(
347369 typeResolver : typeResolver ?? defaultTypeResolver ,
348370 subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
349371 errors : [ ] ,
372+ errorBehavior : onError ?? 'PROPAGATE' ,
350373 } ;
351374}
352375
@@ -585,6 +608,7 @@ export function buildResolveInfo(
585608 rootValue : exeContext . rootValue ,
586609 operation : exeContext . operation ,
587610 variableValues : exeContext . variableValues ,
611+ errorBehavior : exeContext . errorBehavior ,
588612 } ;
589613}
590614
@@ -593,10 +617,25 @@ function handleFieldError(
593617 returnType : GraphQLOutputType ,
594618 exeContext : ExecutionContext ,
595619) : null {
596- // If the field type is non-nullable, then it is resolved without any
597- // protection from errors, however it still properly locates the error.
598- if ( isNonNullType ( returnType ) ) {
620+ if ( exeContext . errorBehavior === 'PROPAGATE' ) {
621+ // If the field type is non-nullable, then it is resolved without any
622+ // protection from errors, however it still properly locates the error.
623+ // Note: semantic non-null types are treated as nullable for the purposes
624+ // of error handling.
625+ if ( isNonNullType ( returnType ) ) {
626+ throw error ;
627+ }
628+ } else if ( exeContext . errorBehavior === 'ABORT' ) {
629+ // In this mode, any error aborts the request
599630 throw error ;
631+ } else if ( exeContext . errorBehavior === 'NO_PROPAGATE' ) {
632+ // In this mode, the client takes responsibility for error handling, so we
633+ // treat the field as if it were nullable.
634+ } else {
635+ invariant (
636+ false ,
637+ 'Unexpected errorBehavior setting: ' + inspect ( exeContext . errorBehavior ) ,
638+ ) ;
600639 }
601640
602641 // Otherwise, error protection is applied, logging the error and resolving
0 commit comments