@@ -141,8 +141,8 @@ export interface ExecutionContext {
141141 fieldResolver : GraphQLFieldResolver < any , any > ;
142142 typeResolver : GraphQLTypeResolver < any , any > ;
143143 subscribeFieldResolver : GraphQLFieldResolver < any , any > ;
144- errors : Array < GraphQLError > ;
145- cancellableStreams : Set < StreamRecord > ;
144+ errors : Array < GraphQLError > | undefined ;
145+ cancellableStreams : Set < StreamRecord > | undefined ;
146146}
147147
148148export interface ExecutionArgs {
@@ -163,7 +163,7 @@ export interface StreamUsage {
163163 fieldGroup : FieldGroup ;
164164}
165165
166- type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > ] ;
166+ type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > | undefined ] ;
167167
168168const UNEXPECTED_EXPERIMENTAL_DIRECTIVES =
169169 'The provided schema unexpectedly contains experimental directives (@defer or @stream). These directives may only be utilized if experimental execution features are explicitly enabled.' ;
@@ -329,20 +329,20 @@ function executeOperation(
329329}
330330
331331function withError (
332- errors : Array < GraphQLError > ,
332+ errors : Array < GraphQLError > | undefined ,
333333 error : GraphQLError ,
334334) : ReadonlyArray < GraphQLError > {
335- return errors . length === 0 ? [ error ] : [ ...errors , error ] ;
335+ return errors === undefined ? [ error ] : [ ...errors , error ] ;
336336}
337337
338338function buildDataResponse (
339339 exeContext : ExecutionContext ,
340340 data : ObjMap < unknown > ,
341- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
341+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
342342) : ExecutionResult | ExperimentalIncrementalExecutionResults {
343343 const { errors } = exeContext ;
344- if ( incrementalDataRecords . length === 0 ) {
345- return errors . length > 0 ? { errors, data } : { data } ;
344+ if ( incrementalDataRecords === undefined ) {
345+ return errors !== undefined ? { errors, data } : { data } ;
346346 }
347347
348348 return buildIncrementalResponse (
@@ -454,8 +454,8 @@ export function buildExecutionContext(
454454 fieldResolver : fieldResolver ?? defaultFieldResolver ,
455455 typeResolver : typeResolver ?? defaultTypeResolver ,
456456 subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
457- errors : [ ] ,
458- cancellableStreams : new Set ( ) ,
457+ errors : undefined ,
458+ cancellableStreams : undefined ,
459459 } ;
460460}
461461
@@ -466,7 +466,7 @@ function buildPerEventExecutionContext(
466466 return {
467467 ...exeContext ,
468468 rootValue : payload ,
469- errors : [ ] ,
469+ errors : undefined ,
470470 } ;
471471}
472472
@@ -552,16 +552,16 @@ function executeFieldsSerially(
552552 appendNewIncrementalDataRecords ( acc , result [ 1 ] ) ;
553553 return acc ;
554554 } ,
555- [ Object . create ( null ) , [ ] ] as GraphQLResult < ObjMap < unknown > > ,
555+ [ Object . create ( null ) , undefined ] as GraphQLResult < ObjMap < unknown > > ,
556556 ) ;
557557}
558558
559559function appendNewIncrementalDataRecords (
560560 acc : GraphQLResult < unknown > ,
561- newRecords : ReadonlyArray < IncrementalDataRecord > ,
561+ newRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
562562) : void {
563- if ( newRecords . length > 0 ) {
564- acc [ 1 ] = acc [ 1 ] . length === 0 ? newRecords : [ ...acc [ 1 ] , ...newRecords ] ;
563+ if ( newRecords !== undefined ) {
564+ acc [ 1 ] = acc [ 1 ] === undefined ? newRecords : [ ...acc [ 1 ] , ...newRecords ] ;
565565 }
566566}
567567
@@ -721,7 +721,7 @@ function executeField(
721721 path ,
722722 incrementalContext ,
723723 ) ;
724- return [ null , [ ] ] ;
724+ return [ null , undefined ] ;
725725 } ) ;
726726 }
727727 return completed ;
@@ -734,7 +734,7 @@ function executeField(
734734 path ,
735735 incrementalContext ,
736736 ) ;
737- return [ null , [ ] ] ;
737+ return [ null , undefined ] ;
738738 }
739739}
740740
@@ -783,7 +783,13 @@ function handleFieldError(
783783
784784 // Otherwise, error protection is applied, logging the error and resolving
785785 // a null value for this field if one is encountered.
786- ( incrementalContext ?? exeContext ) . errors . push ( error ) ;
786+ const context = incrementalContext ?? exeContext ;
787+ let errors = context . errors ;
788+ if ( errors === undefined ) {
789+ errors = [ ] ;
790+ context . errors = errors ;
791+ }
792+ errors . push ( error ) ;
787793}
788794
789795/**
@@ -845,7 +851,7 @@ function completeValue(
845851
846852 // If result value is null or undefined then return null.
847853 if ( result == null ) {
848- return [ null , [ ] ] ;
854+ return [ null , undefined ] ;
849855 }
850856
851857 // If field type is List, complete each item in the list with the inner type
@@ -865,7 +871,7 @@ function completeValue(
865871 // If field type is a leaf type, Scalar or Enum, serialize to a valid value,
866872 // returning null if serialization is not possible.
867873 if ( isLeafType ( returnType ) ) {
868- return [ completeLeafValue ( returnType , result ) , [ ] ] ;
874+ return [ completeLeafValue ( returnType , result ) , undefined ] ;
869875 }
870876
871877 // If field type is an abstract type, Interface or Union, determine the
@@ -940,7 +946,7 @@ async function completePromisedValue(
940946 path ,
941947 incrementalContext ,
942948 ) ;
943- return [ null , [ ] ] ;
949+ return [ null , undefined ] ;
944950 }
945951}
946952
@@ -1030,7 +1036,7 @@ async function completeAsyncIteratorValue(
10301036) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
10311037 let containsPromise = false ;
10321038 const completedResults : Array < unknown > = [ ] ;
1033- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1039+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
10341040 let index = 0 ;
10351041 // eslint-disable-next-line no-constant-condition
10361042 while ( true ) {
@@ -1107,7 +1113,7 @@ async function completeAsyncIteratorValueWithPossibleStream(
11071113) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
11081114 let containsPromise = false ;
11091115 const completedResults : Array < unknown > = [ ] ;
1110- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1116+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
11111117 let index = 0 ;
11121118 const initialCount = streamUsage . initialCount ;
11131119 // eslint-disable-next-line no-constant-condition
@@ -1119,6 +1125,9 @@ async function completeAsyncIteratorValueWithPossibleStream(
11191125 earlyReturn : asyncIterator . return ?. bind ( asyncIterator ) ,
11201126 } ) ;
11211127
1128+ if ( exeContext . cancellableStreams === undefined ) {
1129+ exeContext . cancellableStreams = new Set ( ) ;
1130+ }
11221131 exeContext . cancellableStreams . add ( streamRecord ) ;
11231132
11241133 const firstStreamItems = firstAsyncStreamItems (
@@ -1300,7 +1309,7 @@ function completeIterableValue(
13001309 // where the list contains no Promises by avoiding creating another Promise.
13011310 let containsPromise = false ;
13021311 const completedResults : Array < unknown > = [ ] ;
1303- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1312+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
13041313 let index = 0 ;
13051314 for ( const item of items ) {
13061315 // No need to modify the info object containing the path,
@@ -1361,7 +1370,7 @@ function completeIterableValueWithPossibleStream(
13611370 // where the list contains no Promises by avoiding creating another Promise.
13621371 let containsPromise = false ;
13631372 const completedResults : Array < unknown > = [ ] ;
1364- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1373+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
13651374 let index = 0 ;
13661375 const initialCount = streamUsage . initialCount ;
13671376 const iterator = items [ Symbol . iterator ] ( ) ;
@@ -2332,7 +2341,7 @@ function buildDeferredGroupedFieldSetResult(
23322341 deferredFragmentRecords,
23332342 path : pathToArray ( path ) ,
23342343 result :
2335- errors . length === 0 ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2344+ errors === undefined ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
23362345 incrementalDataRecords : result [ 1 ] ,
23372346 } ;
23382347}
@@ -2548,7 +2557,7 @@ function completeStreamItems(
25482557 itemPath ,
25492558 incrementalContext ,
25502559 ) ;
2551- return [ null , [ ] ] as GraphQLResult < unknown > ;
2560+ return [ null , undefined ] as GraphQLResult < unknown > ;
25522561 } )
25532562 . then (
25542563 ( resolvedItem ) =>
@@ -2587,7 +2596,7 @@ function completeStreamItems(
25872596 itemPath ,
25882597 incrementalContext ,
25892598 ) ;
2590- result = [ null , [ ] ] ;
2599+ result = [ null , undefined ] ;
25912600 }
25922601 } catch ( error ) {
25932602 return {
@@ -2608,7 +2617,7 @@ function completeStreamItems(
26082617 itemPath ,
26092618 incrementalContext ,
26102619 ) ;
2611- return [ null , [ ] ] as GraphQLResult < unknown > ;
2620+ return [ null , undefined ] as GraphQLResult < unknown > ;
26122621 } )
26132622 . then (
26142623 ( resolvedItem ) =>
@@ -2637,7 +2646,7 @@ function buildStreamItemsResult(
26372646 return {
26382647 streamRecord,
26392648 result :
2640- errors . length === 0
2649+ errors === undefined
26412650 ? { items : [ result [ 0 ] ] }
26422651 : {
26432652 items : [ result [ 0 ] ] ,
0 commit comments