1
+ import { invariant } from '../jsutils/invariant.js' ;
1
2
import { isPromise } from '../jsutils/isPromise.js' ;
2
3
import type { ObjMap } from '../jsutils/ObjMap.js' ;
3
4
import type { Path } from '../jsutils/Path.js' ;
@@ -172,7 +173,7 @@ export interface FormattedCompletedResult {
172
173
export function buildIncrementalResponse (
173
174
context : IncrementalPublisherContext ,
174
175
result : ObjMap < unknown > ,
175
- errors : ReadonlyArray < GraphQLError > ,
176
+ errors : ReadonlyArray < GraphQLError > | undefined ,
176
177
incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
177
178
) : ExperimentalIncrementalExecutionResults {
178
179
const incrementalPublisher = new IncrementalPublisher ( context ) ;
@@ -184,7 +185,7 @@ export function buildIncrementalResponse(
184
185
}
185
186
186
187
interface IncrementalPublisherContext {
187
- cancellableStreams : Set < CancellableStreamRecord > ;
188
+ cancellableStreams : Set < CancellableStreamRecord > | undefined ;
188
189
}
189
190
190
191
/**
@@ -218,7 +219,7 @@ class IncrementalPublisher {
218
219
219
220
buildResponse (
220
221
data : ObjMap < unknown > ,
221
- errors : ReadonlyArray < GraphQLError > ,
222
+ errors : ReadonlyArray < GraphQLError > | undefined ,
222
223
incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
223
224
) : ExperimentalIncrementalExecutionResults {
224
225
this . _addIncrementalDataRecords ( incrementalDataRecords ) ;
@@ -227,7 +228,7 @@ class IncrementalPublisher {
227
228
const pending = this . _pendingSourcesToResults ( ) ;
228
229
229
230
const initialResult : InitialIncrementalExecutionResult =
230
- errors . length === 0
231
+ errors === undefined
231
232
? { data, pending, hasNext : true }
232
233
: { errors, data, pending, hasNext : true } ;
233
234
@@ -444,8 +445,12 @@ class IncrementalPublisher {
444
445
} ;
445
446
446
447
const returnStreamIterators = async ( ) : Promise < void > => {
448
+ const cancellableStreams = this . _context . cancellableStreams ;
449
+ if ( cancellableStreams === undefined ) {
450
+ return ;
451
+ }
447
452
const promises : Array < Promise < unknown > > = [ ] ;
448
- for ( const streamRecord of this . _context . cancellableStreams ) {
453
+ for ( const streamRecord of cancellableStreams ) {
449
454
if ( streamRecord . earlyReturn !== undefined ) {
450
455
promises . push ( streamRecord . earlyReturn ( ) ) ;
451
456
}
@@ -519,9 +524,11 @@ class IncrementalPublisher {
519
524
) ;
520
525
}
521
526
522
- this . _addIncrementalDataRecords (
523
- deferredGroupedFieldSetResult . incrementalDataRecords ,
524
- ) ;
527
+ const incrementalDataRecords =
528
+ deferredGroupedFieldSetResult . incrementalDataRecords ;
529
+ if ( incrementalDataRecords !== undefined ) {
530
+ this . _addIncrementalDataRecords ( incrementalDataRecords ) ;
531
+ }
525
532
526
533
for ( const deferredFragmentRecord of deferredGroupedFieldSetResult . deferredFragmentRecords ) {
527
534
const id = deferredFragmentRecord . id ;
@@ -587,6 +594,7 @@ class IncrementalPublisher {
587
594
} ) ;
588
595
this . _pending . delete ( streamRecord ) ;
589
596
if ( isCancellableStreamRecord ( streamRecord ) ) {
597
+ invariant ( this . _context . cancellableStreams !== undefined ) ;
590
598
this . _context . cancellableStreams . delete ( streamRecord ) ;
591
599
streamRecord . earlyReturn ( ) . catch ( ( ) => {
592
600
/* c8 ignore next 1 */
@@ -597,6 +605,7 @@ class IncrementalPublisher {
597
605
this . _completed . push ( { id } ) ;
598
606
this . _pending . delete ( streamRecord ) ;
599
607
if ( isCancellableStreamRecord ( streamRecord ) ) {
608
+ invariant ( this . _context . cancellableStreams !== undefined ) ;
600
609
this . _context . cancellableStreams . delete ( streamRecord ) ;
601
610
}
602
611
} else {
@@ -607,7 +616,7 @@ class IncrementalPublisher {
607
616
608
617
this . _incremental . push ( incrementalEntry ) ;
609
618
610
- if ( streamItemsResult . incrementalDataRecords . length > 0 ) {
619
+ if ( streamItemsResult . incrementalDataRecords !== undefined ) {
611
620
this . _addIncrementalDataRecords (
612
621
streamItemsResult . incrementalDataRecords ,
613
622
) ;
@@ -675,7 +684,7 @@ interface ReconcilableDeferredGroupedFieldSetResult {
675
684
deferredFragmentRecords : ReadonlyArray < DeferredFragmentRecord > ;
676
685
path : Array < string | number > ;
677
686
result : BareDeferredGroupedFieldSetResult ;
678
- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ;
687
+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ;
679
688
sent ?: true | undefined ;
680
689
errors ?: never ;
681
690
}
@@ -743,7 +752,7 @@ function isCancellableStreamRecord(
743
752
interface ReconcilableStreamItemsResult {
744
753
streamRecord : SubsequentResultRecord ;
745
754
result : BareStreamItemsResult ;
746
- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ;
755
+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ;
747
756
errors ?: never ;
748
757
}
749
758
0 commit comments