@@ -140,8 +140,8 @@ export interface ExecutionContext {
140140 fieldResolver : GraphQLFieldResolver < any , any > ;
141141 typeResolver : GraphQLTypeResolver < any , any > ;
142142 subscribeFieldResolver : GraphQLFieldResolver < any , any > ;
143- errors : Array < GraphQLError > ;
144- cancellableStreams : Set < StreamRecord > ;
143+ errors : Array < GraphQLError > | undefined ;
144+ cancellableStreams : Set < StreamRecord > | undefined ;
145145}
146146
147147export interface ExecutionArgs {
@@ -162,7 +162,7 @@ export interface StreamUsage {
162162 fieldGroup : FieldGroup ;
163163}
164164
165- type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > ] ;
165+ type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > | undefined ] ;
166166
167167const UNEXPECTED_EXPERIMENTAL_DIRECTIVES =
168168 'The provided schema unexpectedly contains experimental directives (@defer or @stream). These directives may only be utilized if experimental execution features are explicitly enabled.' ;
@@ -324,20 +324,20 @@ function executeImpl(
324324}
325325
326326function withError (
327- errors : Array < GraphQLError > ,
327+ errors : Array < GraphQLError > | undefined ,
328328 error : GraphQLError ,
329329) : ReadonlyArray < GraphQLError > {
330- return errors . length === 0 ? [ error ] : [ ...errors , error ] ;
330+ return errors === undefined ? [ error ] : [ ...errors , error ] ;
331331}
332332
333333function buildDataResponse (
334334 exeContext : ExecutionContext ,
335335 data : ObjMap < unknown > ,
336- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
336+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
337337) : ExecutionResult | ExperimentalIncrementalExecutionResults {
338338 const { errors } = exeContext ;
339- if ( incrementalDataRecords . length === 0 ) {
340- return errors . length > 0 ? { errors, data } : { data } ;
339+ if ( incrementalDataRecords === undefined ) {
340+ return errors !== undefined ? { errors, data } : { data } ;
341341 }
342342
343343 return buildIncrementalResponse (
@@ -449,8 +449,8 @@ export function buildExecutionContext(
449449 fieldResolver : fieldResolver ?? defaultFieldResolver ,
450450 typeResolver : typeResolver ?? defaultTypeResolver ,
451451 subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
452- errors : [ ] ,
453- cancellableStreams : new Set ( ) ,
452+ errors : undefined ,
453+ cancellableStreams : undefined ,
454454 } ;
455455}
456456
@@ -461,7 +461,7 @@ function buildPerEventExecutionContext(
461461 return {
462462 ...exeContext ,
463463 rootValue : payload ,
464- errors : [ ] ,
464+ errors : undefined ,
465465 } ;
466466}
467467
@@ -547,16 +547,16 @@ function executeFieldsSerially(
547547 appendNewIncrementalDataRecords ( acc , result [ 1 ] ) ;
548548 return acc ;
549549 } ,
550- [ Object . create ( null ) , [ ] ] as GraphQLResult < ObjMap < unknown > > ,
550+ [ Object . create ( null ) , undefined ] as GraphQLResult < ObjMap < unknown > > ,
551551 ) ;
552552}
553553
554554function appendNewIncrementalDataRecords (
555555 acc : GraphQLResult < unknown > ,
556- newRecords : ReadonlyArray < IncrementalDataRecord > ,
556+ newRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
557557) : void {
558- if ( newRecords . length > 0 ) {
559- acc [ 1 ] = acc [ 1 ] . length === 0 ? newRecords : [ ...acc [ 1 ] , ...newRecords ] ;
558+ if ( newRecords !== undefined ) {
559+ acc [ 1 ] = acc [ 1 ] === undefined ? newRecords : [ ...acc [ 1 ] , ...newRecords ] ;
560560 }
561561}
562562
@@ -573,7 +573,7 @@ function executeFields(
573573 incrementalContext : IncrementalContext | undefined ,
574574 deferMap : ReadonlyMap < DeferUsage , DeferredFragmentRecord > | undefined ,
575575) : PromiseOrValue < GraphQLResult < ObjMap < unknown > > > {
576- const acc : GraphQLResult < ObjMap < unknown > > = [ Object . create ( null ) , [ ] ] ;
576+ const acc : GraphQLResult < ObjMap < unknown > > = [ Object . create ( null ) , undefined ] ;
577577 const promises : Array < Promise < void > > = [ ] ;
578578
579579 try {
@@ -715,7 +715,7 @@ function executeField(
715715 path ,
716716 incrementalContext ,
717717 ) ;
718- return [ null , [ ] ] ;
718+ return [ null , undefined ] ;
719719 } ) ;
720720 }
721721 return completed ;
@@ -728,7 +728,7 @@ function executeField(
728728 path ,
729729 incrementalContext ,
730730 ) ;
731- return [ null , [ ] ] ;
731+ return [ null , undefined ] ;
732732 }
733733}
734734
@@ -777,7 +777,13 @@ function handleFieldError(
777777
778778 // Otherwise, error protection is applied, logging the error and resolving
779779 // a null value for this field if one is encountered.
780- ( incrementalContext ?? exeContext ) . errors . push ( error ) ;
780+ const context = incrementalContext ?? exeContext ;
781+ let errors = context . errors ;
782+ if ( errors === undefined ) {
783+ errors = [ ] ;
784+ context . errors = errors ;
785+ }
786+ errors . push ( error ) ;
781787}
782788
783789/**
@@ -839,7 +845,7 @@ function completeValue(
839845
840846 // If result value is null or undefined then return null.
841847 if ( result == null ) {
842- return [ null , [ ] ] ;
848+ return [ null , undefined ] ;
843849 }
844850
845851 // If field type is List, complete each item in the list with the inner type
@@ -859,7 +865,7 @@ function completeValue(
859865 // If field type is a leaf type, Scalar or Enum, serialize to a valid value,
860866 // returning null if serialization is not possible.
861867 if ( isLeafType ( returnType ) ) {
862- return [ completeLeafValue ( returnType , result ) , [ ] ] ;
868+ return [ completeLeafValue ( returnType , result ) , undefined ] ;
863869 }
864870
865871 // If field type is an abstract type, Interface or Union, determine the
@@ -934,7 +940,7 @@ async function completePromisedValue(
934940 path ,
935941 incrementalContext ,
936942 ) ;
937- return [ null , [ ] ] ;
943+ return [ null , undefined ] ;
938944 }
939945}
940946
@@ -1024,7 +1030,7 @@ async function completeAsyncIteratorValue(
10241030) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
10251031 let containsPromise = false ;
10261032 const completedResults : Array < unknown > = [ ] ;
1027- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1033+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
10281034 let index = 0 ;
10291035 // eslint-disable-next-line no-constant-condition
10301036 while ( true ) {
@@ -1101,7 +1107,7 @@ async function completeAsyncIteratorValueWithPossibleStream(
11011107) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
11021108 let containsPromise = false ;
11031109 const completedResults : Array < unknown > = [ ] ;
1104- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1110+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
11051111 let index = 0 ;
11061112 const initialCount = streamUsage . initialCount ;
11071113 // eslint-disable-next-line no-constant-condition
@@ -1113,6 +1119,9 @@ async function completeAsyncIteratorValueWithPossibleStream(
11131119 earlyReturn : asyncIterator . return ?. bind ( asyncIterator ) ,
11141120 } ) ;
11151121
1122+ if ( exeContext . cancellableStreams === undefined ) {
1123+ exeContext . cancellableStreams = new Set ( ) ;
1124+ }
11161125 exeContext . cancellableStreams . add ( streamRecord ) ;
11171126
11181127 const firstStreamItems = firstAsyncStreamItems (
@@ -1294,7 +1303,7 @@ function completeIterableValue(
12941303 // where the list contains no Promises by avoiding creating another Promise.
12951304 let containsPromise = false ;
12961305 const completedResults : Array < unknown > = [ ] ;
1297- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1306+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
12981307 let index = 0 ;
12991308 for ( const item of items ) {
13001309 // No need to modify the info object containing the path,
@@ -1355,7 +1364,7 @@ function completeIterableValueWithPossibleStream(
13551364 // where the list contains no Promises by avoiding creating another Promise.
13561365 let containsPromise = false ;
13571366 const completedResults : Array < unknown > = [ ] ;
1358- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1367+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
13591368 let index = 0 ;
13601369 const initialCount = streamUsage . initialCount ;
13611370 const iterator = items [ Symbol . iterator ] ( ) ;
@@ -2326,7 +2335,7 @@ function buildDeferredGroupedFieldSetResult(
23262335 deferredFragmentRecords,
23272336 path : pathToArray ( path ) ,
23282337 result :
2329- errors . length === 0 ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2338+ errors === undefined ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
23302339 incrementalDataRecords : result [ 1 ] ,
23312340 } ;
23322341}
@@ -2542,7 +2551,7 @@ function completeStreamItems(
25422551 itemPath ,
25432552 incrementalContext ,
25442553 ) ;
2545- return [ null , [ ] ] as GraphQLResult < unknown > ;
2554+ return [ null , undefined ] as GraphQLResult < unknown > ;
25462555 } )
25472556 . then (
25482557 ( resolvedItem ) =>
@@ -2581,7 +2590,7 @@ function completeStreamItems(
25812590 itemPath ,
25822591 incrementalContext ,
25832592 ) ;
2584- result = [ null , [ ] ] ;
2593+ result = [ null , undefined ] ;
25852594 }
25862595 } catch ( error ) {
25872596 return {
@@ -2602,7 +2611,7 @@ function completeStreamItems(
26022611 itemPath ,
26032612 incrementalContext ,
26042613 ) ;
2605- return [ null , [ ] ] as GraphQLResult < unknown > ;
2614+ return [ null , undefined ] as GraphQLResult < unknown > ;
26062615 } )
26072616 . then (
26082617 ( resolvedItem ) =>
@@ -2631,7 +2640,7 @@ function buildStreamItemsResult(
26312640 return {
26322641 streamRecord,
26332642 result :
2634- errors . length === 0
2643+ errors === undefined
26352644 ? { items : [ result [ 0 ] ] }
26362645 : {
26372646 items : [ result [ 0 ] ] ,
0 commit comments