1+ import { invariant } from '../jsutils/invariant.js' ;
12import { isPromise } from '../jsutils/isPromise.js' ;
23import type { ObjMap } from '../jsutils/ObjMap.js' ;
34import type { Path } from '../jsutils/Path.js' ;
@@ -172,7 +173,7 @@ export interface FormattedCompletedResult {
172173export function buildIncrementalResponse (
173174 context : IncrementalPublisherContext ,
174175 result : ObjMap < unknown > ,
175- errors : ReadonlyArray < GraphQLError > ,
176+ errors : ReadonlyArray < GraphQLError > | undefined ,
176177 incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
177178) : ExperimentalIncrementalExecutionResults {
178179 const incrementalPublisher = new IncrementalPublisher ( context ) ;
@@ -184,7 +185,7 @@ export function buildIncrementalResponse(
184185}
185186
186187interface IncrementalPublisherContext {
187- cancellableStreams : Set < CancellableStreamRecord > ;
188+ cancellableStreams : Set < CancellableStreamRecord > | undefined ;
188189}
189190
190191/**
@@ -218,7 +219,7 @@ class IncrementalPublisher {
218219
219220 buildResponse (
220221 data : ObjMap < unknown > ,
221- errors : ReadonlyArray < GraphQLError > ,
222+ errors : ReadonlyArray < GraphQLError > | undefined ,
222223 incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ,
223224 ) : ExperimentalIncrementalExecutionResults {
224225 this . _addIncrementalDataRecords ( incrementalDataRecords ) ;
@@ -227,7 +228,7 @@ class IncrementalPublisher {
227228 const pending = this . _pendingSourcesToResults ( ) ;
228229
229230 const initialResult : InitialIncrementalExecutionResult =
230- errors . length === 0
231+ errors === undefined
231232 ? { data, pending, hasNext : true }
232233 : { errors, data, pending, hasNext : true } ;
233234
@@ -444,8 +445,12 @@ class IncrementalPublisher {
444445 } ;
445446
446447 const returnStreamIterators = async ( ) : Promise < void > => {
448+ const cancellableStreams = this . _context . cancellableStreams ;
449+ if ( cancellableStreams === undefined ) {
450+ return ;
451+ }
447452 const promises : Array < Promise < unknown > > = [ ] ;
448- for ( const streamRecord of this . _context . cancellableStreams ) {
453+ for ( const streamRecord of cancellableStreams ) {
449454 if ( streamRecord . earlyReturn !== undefined ) {
450455 promises . push ( streamRecord . earlyReturn ( ) ) ;
451456 }
@@ -519,9 +524,11 @@ class IncrementalPublisher {
519524 ) ;
520525 }
521526
522- this . _addIncrementalDataRecords (
523- deferredGroupedFieldSetResult . incrementalDataRecords ,
524- ) ;
527+ const incrementalDataRecords =
528+ deferredGroupedFieldSetResult . incrementalDataRecords ;
529+ if ( incrementalDataRecords !== undefined ) {
530+ this . _addIncrementalDataRecords ( incrementalDataRecords ) ;
531+ }
525532
526533 for ( const deferredFragmentRecord of deferredGroupedFieldSetResult . deferredFragmentRecords ) {
527534 const id = deferredFragmentRecord . id ;
@@ -587,6 +594,7 @@ class IncrementalPublisher {
587594 } ) ;
588595 this . _pending . delete ( streamRecord ) ;
589596 if ( isCancellableStreamRecord ( streamRecord ) ) {
597+ invariant ( this . _context . cancellableStreams !== undefined ) ;
590598 this . _context . cancellableStreams . delete ( streamRecord ) ;
591599 streamRecord . earlyReturn ( ) . catch ( ( ) => {
592600 /* c8 ignore next 1 */
@@ -597,6 +605,7 @@ class IncrementalPublisher {
597605 this . _completed . push ( { id } ) ;
598606 this . _pending . delete ( streamRecord ) ;
599607 if ( isCancellableStreamRecord ( streamRecord ) ) {
608+ invariant ( this . _context . cancellableStreams !== undefined ) ;
600609 this . _context . cancellableStreams . delete ( streamRecord ) ;
601610 }
602611 } else {
@@ -607,7 +616,7 @@ class IncrementalPublisher {
607616
608617 this . _incremental . push ( incrementalEntry ) ;
609618
610- if ( streamItemsResult . incrementalDataRecords . length > 0 ) {
619+ if ( streamItemsResult . incrementalDataRecords !== undefined ) {
611620 this . _addIncrementalDataRecords (
612621 streamItemsResult . incrementalDataRecords ,
613622 ) ;
@@ -675,7 +684,7 @@ interface ReconcilableDeferredGroupedFieldSetResult {
675684 deferredFragmentRecords : ReadonlyArray < DeferredFragmentRecord > ;
676685 path : Array < string | number > ;
677686 result : BareDeferredGroupedFieldSetResult ;
678- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ;
687+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ;
679688 sent ?: true | undefined ;
680689 errors ?: never ;
681690}
@@ -743,7 +752,7 @@ function isCancellableStreamRecord(
743752interface ReconcilableStreamItemsResult {
744753 streamRecord : SubsequentResultRecord ;
745754 result : BareStreamItemsResult ;
746- incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > ;
755+ incrementalDataRecords : ReadonlyArray < IncrementalDataRecord > | undefined ;
747756 errors ?: never ;
748757}
749758
0 commit comments