@@ -141,8 +141,8 @@ export interface ExecutionContext {
141
141
fieldResolver : GraphQLFieldResolver < any , any > ;
142
142
typeResolver : GraphQLTypeResolver < any , any > ;
143
143
subscribeFieldResolver : GraphQLFieldResolver < any , any > ;
144
- errors : Array < GraphQLError > ;
145
- cancellableStreams : Set < StreamRecord > ;
144
+ errors : Array < GraphQLError > | undefined ;
145
+ cancellableStreams : Set < StreamRecord > | undefined ;
146
146
}
147
147
148
148
export interface ExecutionArgs {
@@ -163,7 +163,7 @@ export interface StreamUsage {
163
163
fieldGroup : FieldGroup ;
164
164
}
165
165
166
- type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > ] ;
166
+ type GraphQLResult < T > = [ T , ReadonlyArray < IncrementalDataRecord > | undefined ] ;
167
167
168
168
const UNEXPECTED_EXPERIMENTAL_DIRECTIVES =
169
169
'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(
329
329
}
330
330
331
331
function withError (
332
- errors : Array < GraphQLError > ,
332
+ errors : Array < GraphQLError > | undefined ,
333
333
error : GraphQLError ,
334
334
) : ReadonlyArray < GraphQLError > {
335
- return errors . length === 0 ? [ error ] : [ ...errors , error ] ;
335
+ return errors === undefined ? [ error ] : [ ...errors , error ] ;
336
336
}
337
337
338
338
function buildDataResponse (
339
339
exeContext : ExecutionContext ,
340
340
data : ObjMap < unknown > ,
341
- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
341
+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
342
342
) : ExecutionResult | ExperimentalIncrementalExecutionResults {
343
343
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 } ;
346
346
}
347
347
348
348
return buildIncrementalResponse (
@@ -454,8 +454,8 @@ export function buildExecutionContext(
454
454
fieldResolver : fieldResolver ?? defaultFieldResolver ,
455
455
typeResolver : typeResolver ?? defaultTypeResolver ,
456
456
subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
457
- errors : [ ] ,
458
- cancellableStreams : new Set ( ) ,
457
+ errors : undefined ,
458
+ cancellableStreams : undefined ,
459
459
} ;
460
460
}
461
461
@@ -466,7 +466,7 @@ function buildPerEventExecutionContext(
466
466
return {
467
467
...exeContext ,
468
468
rootValue : payload ,
469
- errors : [ ] ,
469
+ errors : undefined ,
470
470
} ;
471
471
}
472
472
@@ -552,16 +552,16 @@ function executeFieldsSerially(
552
552
appendNewIncrementalDataRecords ( acc , result [ 1 ] ) ;
553
553
return acc ;
554
554
} ,
555
- [ Object . create ( null ) , [ ] ] as GraphQLResult < ObjMap < unknown > > ,
555
+ [ Object . create ( null ) , undefined ] as GraphQLResult < ObjMap < unknown > > ,
556
556
) ;
557
557
}
558
558
559
559
function appendNewIncrementalDataRecords (
560
560
acc : GraphQLResult < unknown > ,
561
- newRecords : ReadonlyArray < IncrementalDataRecord > ,
561
+ newRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
562
562
) : 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 ] ;
565
565
}
566
566
}
567
567
@@ -721,7 +721,7 @@ function executeField(
721
721
path ,
722
722
incrementalContext ,
723
723
) ;
724
- return [ null , [ ] ] ;
724
+ return [ null , undefined ] ;
725
725
} ) ;
726
726
}
727
727
return completed ;
@@ -734,7 +734,7 @@ function executeField(
734
734
path ,
735
735
incrementalContext ,
736
736
) ;
737
- return [ null , [ ] ] ;
737
+ return [ null , undefined ] ;
738
738
}
739
739
}
740
740
@@ -783,7 +783,13 @@ function handleFieldError(
783
783
784
784
// Otherwise, error protection is applied, logging the error and resolving
785
785
// 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 ) ;
787
793
}
788
794
789
795
/**
@@ -845,7 +851,7 @@ function completeValue(
845
851
846
852
// If result value is null or undefined then return null.
847
853
if ( result == null ) {
848
- return [ null , [ ] ] ;
854
+ return [ null , undefined ] ;
849
855
}
850
856
851
857
// If field type is List, complete each item in the list with the inner type
@@ -865,7 +871,7 @@ function completeValue(
865
871
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
866
872
// returning null if serialization is not possible.
867
873
if ( isLeafType ( returnType ) ) {
868
- return [ completeLeafValue ( returnType , result ) , [ ] ] ;
874
+ return [ completeLeafValue ( returnType , result ) , undefined ] ;
869
875
}
870
876
871
877
// If field type is an abstract type, Interface or Union, determine the
@@ -940,7 +946,7 @@ async function completePromisedValue(
940
946
path ,
941
947
incrementalContext ,
942
948
) ;
943
- return [ null , [ ] ] ;
949
+ return [ null , undefined ] ;
944
950
}
945
951
}
946
952
@@ -1030,7 +1036,7 @@ async function completeAsyncIteratorValue(
1030
1036
) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
1031
1037
let containsPromise = false ;
1032
1038
const completedResults : Array < unknown > = [ ] ;
1033
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1039
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1034
1040
let index = 0 ;
1035
1041
// eslint-disable-next-line no-constant-condition
1036
1042
while ( true ) {
@@ -1107,7 +1113,7 @@ async function completeAsyncIteratorValueWithPossibleStream(
1107
1113
) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
1108
1114
let containsPromise = false ;
1109
1115
const completedResults : Array < unknown > = [ ] ;
1110
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1116
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1111
1117
let index = 0 ;
1112
1118
const initialCount = streamUsage . initialCount ;
1113
1119
// eslint-disable-next-line no-constant-condition
@@ -1119,6 +1125,9 @@ async function completeAsyncIteratorValueWithPossibleStream(
1119
1125
earlyReturn : asyncIterator . return ?. bind ( asyncIterator ) ,
1120
1126
} ) ;
1121
1127
1128
+ if ( exeContext . cancellableStreams === undefined ) {
1129
+ exeContext . cancellableStreams = new Set ( ) ;
1130
+ }
1122
1131
exeContext . cancellableStreams . add ( streamRecord ) ;
1123
1132
1124
1133
const firstStreamItems = firstAsyncStreamItems (
@@ -1300,7 +1309,7 @@ function completeIterableValue(
1300
1309
// where the list contains no Promises by avoiding creating another Promise.
1301
1310
let containsPromise = false ;
1302
1311
const completedResults : Array < unknown > = [ ] ;
1303
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1312
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1304
1313
let index = 0 ;
1305
1314
for ( const item of items ) {
1306
1315
// No need to modify the info object containing the path,
@@ -1361,7 +1370,7 @@ function completeIterableValueWithPossibleStream(
1361
1370
// where the list contains no Promises by avoiding creating another Promise.
1362
1371
let containsPromise = false ;
1363
1372
const completedResults : Array < unknown > = [ ] ;
1364
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1373
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1365
1374
let index = 0 ;
1366
1375
const initialCount = streamUsage . initialCount ;
1367
1376
const iterator = items [ Symbol . iterator ] ( ) ;
@@ -2332,7 +2341,7 @@ function buildDeferredGroupedFieldSetResult(
2332
2341
deferredFragmentRecords,
2333
2342
path : pathToArray ( path ) ,
2334
2343
result :
2335
- errors . length === 0 ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2344
+ errors === undefined ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2336
2345
incrementalDataRecords : result [ 1 ] ,
2337
2346
} ;
2338
2347
}
@@ -2548,7 +2557,7 @@ function completeStreamItems(
2548
2557
itemPath ,
2549
2558
incrementalContext ,
2550
2559
) ;
2551
- return [ null , [ ] ] as GraphQLResult < unknown > ;
2560
+ return [ null , undefined ] as GraphQLResult < unknown > ;
2552
2561
} )
2553
2562
. then (
2554
2563
( resolvedItem ) =>
@@ -2587,7 +2596,7 @@ function completeStreamItems(
2587
2596
itemPath ,
2588
2597
incrementalContext ,
2589
2598
) ;
2590
- result = [ null , [ ] ] ;
2599
+ result = [ null , undefined ] ;
2591
2600
}
2592
2601
} catch ( error ) {
2593
2602
return {
@@ -2608,7 +2617,7 @@ function completeStreamItems(
2608
2617
itemPath ,
2609
2618
incrementalContext ,
2610
2619
) ;
2611
- return [ null , [ ] ] as GraphQLResult < unknown > ;
2620
+ return [ null , undefined ] as GraphQLResult < unknown > ;
2612
2621
} )
2613
2622
. then (
2614
2623
( resolvedItem ) =>
@@ -2637,7 +2646,7 @@ function buildStreamItemsResult(
2637
2646
return {
2638
2647
streamRecord,
2639
2648
result :
2640
- errors . length === 0
2649
+ errors === undefined
2641
2650
? { items : [ result [ 0 ] ] }
2642
2651
: {
2643
2652
items : [ result [ 0 ] ] ,
0 commit comments