@@ -13,6 +13,8 @@ import { promiseForObject } from '../jsutils/promiseForObject';
13
13
import type { PromiseOrValue } from '../jsutils/PromiseOrValue' ;
14
14
import { promiseReduce } from '../jsutils/promiseReduce' ;
15
15
16
+ import type { GraphQLErrorBehavior } from '../error/ErrorBehavior' ;
17
+ import { isErrorBehavior } from '../error/ErrorBehavior' ;
16
18
import type { GraphQLFormattedError } from '../error/GraphQLError' ;
17
19
import { GraphQLError } from '../error/GraphQLError' ;
18
20
import { locatedError } from '../error/locatedError' ;
@@ -115,6 +117,7 @@ export interface ExecutionContext {
115
117
typeResolver : GraphQLTypeResolver < any , any > ;
116
118
subscribeFieldResolver : GraphQLFieldResolver < any , any > ;
117
119
errors : Array < GraphQLError > ;
120
+ errorBehavior : GraphQLErrorBehavior ;
118
121
}
119
122
120
123
/**
@@ -130,6 +133,7 @@ export interface ExecutionResult<
130
133
> {
131
134
errors ?: ReadonlyArray < GraphQLError > ;
132
135
data ?: TData | null ;
136
+ onError ?: GraphQLErrorBehavior ;
133
137
extensions ?: TExtensions ;
134
138
}
135
139
@@ -152,6 +156,15 @@ export interface ExecutionArgs {
152
156
fieldResolver ?: Maybe < GraphQLFieldResolver < any , any > > ;
153
157
typeResolver ?: Maybe < GraphQLTypeResolver < any , any > > ;
154
158
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 ;
155
168
}
156
169
157
170
/**
@@ -286,8 +299,17 @@ export function buildExecutionContext(
286
299
fieldResolver,
287
300
typeResolver,
288
301
subscribeFieldResolver,
302
+ onError,
289
303
} = args ;
290
304
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
+
291
313
let operation : OperationDefinitionNode | undefined ;
292
314
const fragments : ObjMap < FragmentDefinitionNode > = Object . create ( null ) ;
293
315
for ( const definition of document . definitions ) {
@@ -347,6 +369,7 @@ export function buildExecutionContext(
347
369
typeResolver : typeResolver ?? defaultTypeResolver ,
348
370
subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
349
371
errors : [ ] ,
372
+ errorBehavior : onError ?? 'PROPAGATE' ,
350
373
} ;
351
374
}
352
375
@@ -585,6 +608,7 @@ export function buildResolveInfo(
585
608
rootValue : exeContext . rootValue ,
586
609
operation : exeContext . operation ,
587
610
variableValues : exeContext . variableValues ,
611
+ errorBehavior : exeContext . errorBehavior ,
588
612
} ;
589
613
}
590
614
@@ -593,10 +617,25 @@ function handleFieldError(
593
617
returnType : GraphQLOutputType ,
594
618
exeContext : ExecutionContext ,
595
619
) : 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
599
630
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
+ ) ;
600
639
}
601
640
602
641
// Otherwise, error protection is applied, logging the error and resolving
0 commit comments