@@ -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.' ;
@@ -324,20 +324,20 @@ function executeImpl(
324
324
}
325
325
326
326
function withError (
327
- errors : Array < GraphQLError > ,
327
+ errors : Array < GraphQLError > | undefined ,
328
328
error : GraphQLError ,
329
329
) : ReadonlyArray < GraphQLError > {
330
- return errors . length === 0 ? [ error ] : [ ...errors , error ] ;
330
+ return errors === undefined ? [ error ] : [ ...errors , error ] ;
331
331
}
332
332
333
333
function buildDataResponse (
334
334
exeContext : ExecutionContext ,
335
335
data : ObjMap < unknown > ,
336
- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
336
+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
337
337
) : ExecutionResult | ExperimentalIncrementalExecutionResults {
338
338
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 } ;
341
341
}
342
342
343
343
return buildIncrementalResponse (
@@ -449,8 +449,8 @@ export function buildExecutionContext(
449
449
fieldResolver : fieldResolver ?? defaultFieldResolver ,
450
450
typeResolver : typeResolver ?? defaultTypeResolver ,
451
451
subscribeFieldResolver : subscribeFieldResolver ?? defaultFieldResolver ,
452
- errors : [ ] ,
453
- cancellableStreams : new Set ( ) ,
452
+ errors : undefined ,
453
+ cancellableStreams : undefined ,
454
454
} ;
455
455
}
456
456
@@ -461,7 +461,7 @@ function buildPerEventExecutionContext(
461
461
return {
462
462
...exeContext ,
463
463
rootValue : payload ,
464
- errors : [ ] ,
464
+ errors : undefined ,
465
465
} ;
466
466
}
467
467
@@ -547,16 +547,16 @@ function executeFieldsSerially(
547
547
appendNewIncrementalDataRecords ( acc , result [ 1 ] ) ;
548
548
return acc ;
549
549
} ,
550
- [ Object . create ( null ) , [ ] ] as GraphQLResult < ObjMap < unknown > > ,
550
+ [ Object . create ( null ) , undefined ] as GraphQLResult < ObjMap < unknown > > ,
551
551
) ;
552
552
}
553
553
554
554
function appendNewIncrementalDataRecords (
555
555
acc : GraphQLResult < unknown > ,
556
- newRecords : ReadonlyArray < IncrementalDataRecord > ,
556
+ newRecords : ReadonlyArray < IncrementalDataRecord > | undefined ,
557
557
) : 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 ] ;
560
560
}
561
561
}
562
562
@@ -573,7 +573,7 @@ function executeFields(
573
573
incrementalContext : IncrementalContext | undefined ,
574
574
deferMap : ReadonlyMap < DeferUsage , DeferredFragmentRecord > | undefined ,
575
575
) : PromiseOrValue < GraphQLResult < ObjMap < unknown > > > {
576
- const acc : GraphQLResult < ObjMap < unknown > > = [ Object . create ( null ) , [ ] ] ;
576
+ const acc : GraphQLResult < ObjMap < unknown > > = [ Object . create ( null ) , undefined ] ;
577
577
const promises : Array < Promise < void > > = [ ] ;
578
578
579
579
try {
@@ -715,7 +715,7 @@ function executeField(
715
715
path ,
716
716
incrementalContext ,
717
717
) ;
718
- return [ null , [ ] ] ;
718
+ return [ null , undefined ] ;
719
719
} ) ;
720
720
}
721
721
return completed ;
@@ -728,7 +728,7 @@ function executeField(
728
728
path ,
729
729
incrementalContext ,
730
730
) ;
731
- return [ null , [ ] ] ;
731
+ return [ null , undefined ] ;
732
732
}
733
733
}
734
734
@@ -777,7 +777,13 @@ function handleFieldError(
777
777
778
778
// Otherwise, error protection is applied, logging the error and resolving
779
779
// 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 ) ;
781
787
}
782
788
783
789
/**
@@ -839,7 +845,7 @@ function completeValue(
839
845
840
846
// If result value is null or undefined then return null.
841
847
if ( result == null ) {
842
- return [ null , [ ] ] ;
848
+ return [ null , undefined ] ;
843
849
}
844
850
845
851
// If field type is List, complete each item in the list with the inner type
@@ -859,7 +865,7 @@ function completeValue(
859
865
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
860
866
// returning null if serialization is not possible.
861
867
if ( isLeafType ( returnType ) ) {
862
- return [ completeLeafValue ( returnType , result ) , [ ] ] ;
868
+ return [ completeLeafValue ( returnType , result ) , undefined ] ;
863
869
}
864
870
865
871
// If field type is an abstract type, Interface or Union, determine the
@@ -934,7 +940,7 @@ async function completePromisedValue(
934
940
path ,
935
941
incrementalContext ,
936
942
) ;
937
- return [ null , [ ] ] ;
943
+ return [ null , undefined ] ;
938
944
}
939
945
}
940
946
@@ -1024,7 +1030,7 @@ async function completeAsyncIteratorValue(
1024
1030
) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
1025
1031
let containsPromise = false ;
1026
1032
const completedResults : Array < unknown > = [ ] ;
1027
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1033
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1028
1034
let index = 0 ;
1029
1035
// eslint-disable-next-line no-constant-condition
1030
1036
while ( true ) {
@@ -1101,7 +1107,7 @@ async function completeAsyncIteratorValueWithPossibleStream(
1101
1107
) : Promise < GraphQLResult < ReadonlyArray < unknown > > > {
1102
1108
let containsPromise = false ;
1103
1109
const completedResults : Array < unknown > = [ ] ;
1104
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1110
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1105
1111
let index = 0 ;
1106
1112
const initialCount = streamUsage . initialCount ;
1107
1113
// eslint-disable-next-line no-constant-condition
@@ -1113,6 +1119,9 @@ async function completeAsyncIteratorValueWithPossibleStream(
1113
1119
earlyReturn : asyncIterator . return ?. bind ( asyncIterator ) ,
1114
1120
} ) ;
1115
1121
1122
+ if ( exeContext . cancellableStreams === undefined ) {
1123
+ exeContext . cancellableStreams = new Set ( ) ;
1124
+ }
1116
1125
exeContext . cancellableStreams . add ( streamRecord ) ;
1117
1126
1118
1127
const firstStreamItems = firstAsyncStreamItems (
@@ -1294,7 +1303,7 @@ function completeIterableValue(
1294
1303
// where the list contains no Promises by avoiding creating another Promise.
1295
1304
let containsPromise = false ;
1296
1305
const completedResults : Array < unknown > = [ ] ;
1297
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1306
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1298
1307
let index = 0 ;
1299
1308
for ( const item of items ) {
1300
1309
// No need to modify the info object containing the path,
@@ -1355,7 +1364,7 @@ function completeIterableValueWithPossibleStream(
1355
1364
// where the list contains no Promises by avoiding creating another Promise.
1356
1365
let containsPromise = false ;
1357
1366
const completedResults : Array < unknown > = [ ] ;
1358
- const acc : GraphQLResult < Array < unknown > > = [ completedResults , [ ] ] ;
1367
+ const acc : GraphQLResult < Array < unknown > > = [ completedResults , undefined ] ;
1359
1368
let index = 0 ;
1360
1369
const initialCount = streamUsage . initialCount ;
1361
1370
const iterator = items [ Symbol . iterator ] ( ) ;
@@ -2326,7 +2335,7 @@ function buildDeferredGroupedFieldSetResult(
2326
2335
deferredFragmentRecords,
2327
2336
path : pathToArray ( path ) ,
2328
2337
result :
2329
- errors . length === 0 ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2338
+ errors === undefined ? { data : result [ 0 ] } : { data : result [ 0 ] , errors } ,
2330
2339
incrementalDataRecords : result [ 1 ] ,
2331
2340
} ;
2332
2341
}
@@ -2542,7 +2551,7 @@ function completeStreamItems(
2542
2551
itemPath ,
2543
2552
incrementalContext ,
2544
2553
) ;
2545
- return [ null , [ ] ] as GraphQLResult < unknown > ;
2554
+ return [ null , undefined ] as GraphQLResult < unknown > ;
2546
2555
} )
2547
2556
. then (
2548
2557
( resolvedItem ) =>
@@ -2581,7 +2590,7 @@ function completeStreamItems(
2581
2590
itemPath ,
2582
2591
incrementalContext ,
2583
2592
) ;
2584
- result = [ null , [ ] ] ;
2593
+ result = [ null , undefined ] ;
2585
2594
}
2586
2595
} catch ( error ) {
2587
2596
return {
@@ -2602,7 +2611,7 @@ function completeStreamItems(
2602
2611
itemPath ,
2603
2612
incrementalContext ,
2604
2613
) ;
2605
- return [ null , [ ] ] as GraphQLResult < unknown > ;
2614
+ return [ null , undefined ] as GraphQLResult < unknown > ;
2606
2615
} )
2607
2616
. then (
2608
2617
( resolvedItem ) =>
@@ -2631,7 +2640,7 @@ function buildStreamItemsResult(
2631
2640
return {
2632
2641
streamRecord,
2633
2642
result :
2634
- errors . length === 0
2643
+ errors === undefined
2635
2644
? { items : [ result [ 0 ] ] }
2636
2645
: {
2637
2646
items : [ result [ 0 ] ] ,
0 commit comments