@@ -140,8 +140,8 @@ export interface ExecutionContext {
140
140
fieldResolver : GraphQLFieldResolver < any , any > ;
141
141
typeResolver : GraphQLTypeResolver < any , any > ;
142
142
subscribeFieldResolver : GraphQLFieldResolver < any , any > ;
143
- errors : Array < GraphQLError > ;
144
- cancellableStreams : Set < StreamRecord > ;
143
+ errors : Array < GraphQLError > | undefined ;
144
+ cancellableStreams : Set < StreamRecord > | undefined ;
145
145
}
146
146
147
147
export interface ExecutionArgs {
@@ -162,7 +162,7 @@ export interface StreamUsage {
162
162
fieldGroup : FieldGroup ;
163
163
}
164
164
165
- type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > ] ;
165
+ type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > | undefined ] ;
166
166
167
167
const UNEXPECTED_EXPERIMENTAL_DIRECTIVES =
168
168
'The provided schema unexpectedly contains experimental directives (@defer or @stream). These directives may only be utilized if experimental execution features are explicitly enabled.' ;
@@ -328,20 +328,20 @@ function executeOperation(
328
328
}
329
329
330
330
function withError (
331
- errors : Array < GraphQLError > ,
331
+ errors : Array < GraphQLError > | undefined ,
332
332
error : GraphQLError ,
333
333
) : ReadonlyArray < GraphQLError > {
334
- return errors . length === 0 ? [ error ] : [ ...errors , error ] ;
334
+ return errors === undefined ? [ error ] : [ ...errors , error ] ;
335
335
}
336
336
337
337
function buildDataResponse (
338
338
exeContext : ExecutionContext ,
339
339
data : ObjMap < unknown > ,
340
- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
340
+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
341
341
) : ExecutionResult | ExperimentalIncrementalExecutionResults {
342
342
const { errors } = exeContext ;
343
- if ( incrementalDataRecords . length === 0 ) {
344
- return errors . length > 0 ? { errors, data } : { data } ;
343
+ if ( incrementalDataRecords === undefined ) {
344
+ return errors !== undefined ? { errors, data } : { data } ;
345
345
}
346
346
347
347
return buildIncrementalResponse (
@@ -453,8 +453,8 @@ export function buildExecutionContext(
453
453
fieldResolver : fieldResolver ?? defaultFieldResolver ,
454
454
typeResolver : typeResolver ?? defaultTypeResolver ,
455
455
subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
456
- errors : [ ] ,
457
- cancellableStreams : new Set ( ) ,
456
+ errors : undefined ,
457
+ cancellableStreams : undefined ,
458
458
} ;
459
459
}
460
460
@@ -465,7 +465,7 @@ function buildPerEventExecutionContext(
465
465
return {
466
466
...exeContext ,
467
467
rootValue : payload ,
468
- errors : [ ] ,
468
+ errors : undefined ,
469
469
} ;
470
470
}
471
471
@@ -551,16 +551,16 @@ function executeFieldsSerially(
551
551
appendNewIncrementalDataRecords ( acc , result [ 1 ] ) ;
552
552
return acc ;
553
553
} ,
554
- [ Object . create ( null ) , [ ] ] as GraphQLResult < ObjMap < unknown > > ,
554
+ [ Object . create ( null ) , undefined ] as GraphQLResult < ObjMap < unknown > > ,
555
555
) ;
556
556
}
557
557
558
558
function appendNewIncrementalDataRecords (
559
559
acc : GraphQLResult < unknown > ,
560
- newRecords : ReadonlyArray < IncrementalDataRecord > ,
560
+ newRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
561
561
) : void {
562
- if ( newRecords . length > 0 ) {
563
- acc [ 1 ] = acc [ 1 ] . length === 0 ? newRecords : [ ...acc [ 1 ] , ...newRecords ] ;
562
+ if ( newRecords !== undefined ) {
563
+ acc [ 1 ] = acc [ 1 ] === undefined ? newRecords : [ ...acc [ 1 ] , ...newRecords ] ;
564
564
}
565
565
}
566
566
@@ -577,7 +577,7 @@ function executeFields(
577
577
incrementalContext : IncrementalContext | undefined ,
578
578
deferMap : ReadonlyMap < DeferUsage , DeferredFragmentRecord > | undefined ,
579
579
) : PromiseOrValue < GraphQLResult < ObjMap < unknown > > > {
580
- const acc : GraphQLResult < ObjMap < unknown > > = [ Object . create ( null ) , [ ] ] ;
580
+ const acc : GraphQLResult < ObjMap < unknown > > = [ Object . create ( null ) , undefined ] ;
581
581
const promises : Array < Promise < void > > = [ ] ;
582
582
583
583
try {
@@ -719,7 +719,7 @@ function executeField(
719
719
path ,
720
720
incrementalContext ,
721
721
) ;
722
- return [ null , [ ] ] ;
722
+ return [ null , undefined ] ;
723
723
} ) ;
724
724
}
725
725
return completed ;
@@ -732,7 +732,7 @@ function executeField(
732
732
path ,
733
733
incrementalContext ,
734
734
) ;
735
- return [ null , [ ] ] ;
735
+ return [ null , undefined ] ;
736
736
}
737
737
}
738
738
@@ -781,7 +781,13 @@ function handleFieldError(
781
781
782
782
// Otherwise, error protection is applied, logging the error and resolving
783
783
// a null value for this field if one is encountered.
784
- ( incrementalContext ?? exeContext ) . errors . push ( error ) ;
784
+ const context = incrementalContext ?? exeContext ;
785
+ let errors = context . errors ;
786
+ if ( errors === undefined ) {
787
+ errors = [ ] ;
788
+ context . errors = errors ;
789
+ }
790
+ errors . push ( error ) ;
785
791
}
786
792
787
793
/**
@@ -843,7 +849,7 @@ function completeValue(
843
849
844
850
// If result value is null or undefined then return null.
845
851
if ( result == null ) {
846
- return [ null , [ ] ] ;
852
+ return [ null , undefined ] ;
847
853
}
848
854
849
855
// If field type is List, complete each item in the list with the inner type
@@ -863,7 +869,7 @@ function completeValue(
863
869
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
864
870
// returning null if serialization is not possible.
865
871
if ( isLeafType ( returnType ) ) {
866
- return [ completeLeafValue ( returnType , result ) , [ ] ] ;
872
+ return [ completeLeafValue ( returnType , result ) , undefined ] ;
867
873
}
868
874
869
875
// If field type is an abstract type, Interface or Union, determine the
@@ -938,7 +944,7 @@ async function completePromisedValue(
938
944
path ,
939
945
incrementalContext ,
940
946
) ;
941
- return [ null , [ ] ] ;
947
+ return [ null , undefined ] ;
942
948
}
943
949
}
944
950
@@ -1028,7 +1034,7 @@ async function completeAsyncIteratorValue(
1028
1034
) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
1029
1035
let containsPromise = false ;
1030
1036
const completedResults : Array < unknown > = [ ] ;
1031
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1037
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1032
1038
let index = 0 ;
1033
1039
// eslint-disable-next-line no-constant-condition
1034
1040
while ( true ) {
@@ -1105,7 +1111,7 @@ async function completeAsyncIteratorValueWithPossibleStream(
1105
1111
) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
1106
1112
let containsPromise = false ;
1107
1113
const completedResults : Array < unknown > = [ ] ;
1108
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1114
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1109
1115
let index = 0 ;
1110
1116
const initialCount = streamUsage . initialCount ;
1111
1117
// eslint-disable-next-line no-constant-condition
@@ -1117,6 +1123,9 @@ async function completeAsyncIteratorValueWithPossibleStream(
1117
1123
earlyReturn : asyncIterator . return ?. bind ( asyncIterator ) ,
1118
1124
} ) ;
1119
1125
1126
+ if ( exeContext . cancellableStreams === undefined ) {
1127
+ exeContext . cancellableStreams = new Set ( ) ;
1128
+ }
1120
1129
exeContext . cancellableStreams . add ( streamRecord ) ;
1121
1130
1122
1131
const firstStreamItems = firstAsyncStreamItems (
@@ -1298,7 +1307,7 @@ function completeIterableValue(
1298
1307
// where the list contains no Promises by avoiding creating another Promise.
1299
1308
let containsPromise = false ;
1300
1309
const completedResults : Array < unknown > = [ ] ;
1301
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1310
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1302
1311
let index = 0 ;
1303
1312
for ( const item of items ) {
1304
1313
// No need to modify the info object containing the path,
@@ -1359,7 +1368,7 @@ function completeIterableValueWithPossibleStream(
1359
1368
// where the list contains no Promises by avoiding creating another Promise.
1360
1369
let containsPromise = false ;
1361
1370
const completedResults : Array < unknown > = [ ] ;
1362
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1371
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1363
1372
let index = 0 ;
1364
1373
const initialCount = streamUsage . initialCount ;
1365
1374
const iterator = items [ Symbol . iterator ] ( ) ;
@@ -2330,7 +2339,7 @@ function buildDeferredGroupedFieldSetResult(
2330
2339
deferredFragmentRecords,
2331
2340
path : pathToArray ( path ) ,
2332
2341
result :
2333
- errors . length === 0 ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2342
+ errors === undefined ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2334
2343
incrementalDataRecords : result [ 1 ] ,
2335
2344
} ;
2336
2345
}
@@ -2546,7 +2555,7 @@ function completeStreamItems(
2546
2555
itemPath ,
2547
2556
incrementalContext ,
2548
2557
) ;
2549
- return [ null , [ ] ] as GraphQLResult < unknown > ;
2558
+ return [ null , undefined ] as GraphQLResult < unknown > ;
2550
2559
} )
2551
2560
. then (
2552
2561
( resolvedItem ) =>
@@ -2585,7 +2594,7 @@ function completeStreamItems(
2585
2594
itemPath ,
2586
2595
incrementalContext ,
2587
2596
) ;
2588
- result = [ null , [ ] ] ;
2597
+ result = [ null , undefined ] ;
2589
2598
}
2590
2599
} catch ( error ) {
2591
2600
return {
@@ -2606,7 +2615,7 @@ function completeStreamItems(
2606
2615
itemPath ,
2607
2616
incrementalContext ,
2608
2617
) ;
2609
- return [ null , [ ] ] as GraphQLResult < unknown > ;
2618
+ return [ null , undefined ] as GraphQLResult < unknown > ;
2610
2619
} )
2611
2620
. then (
2612
2621
( resolvedItem ) =>
@@ -2635,7 +2644,7 @@ function buildStreamItemsResult(
2635
2644
return {
2636
2645
streamRecord,
2637
2646
result :
2638
- errors . length === 0
2647
+ errors === undefined
2639
2648
? { items : [ result [ 0 ] ] }
2640
2649
: {
2641
2650
items : [ result [ 0 ] ] ,
0 commit comments