@@ -97,34 +97,17 @@ export type FormattedIncrementalResult<
97
97
* parents have completed so that they can no longer be filtered. This includes all Incremental
98
98
* Data records in `released`, as well as Incremental Data records that have not yet completed.
99
99
*
100
- * `_initialResult`: a record containing the state of the initial result, as follows:
101
- * `isCompleted`: indicates whether the initial result has completed.
102
- * `children`: the set of Incremental Data records that can be be published when the initial
103
- * result is completed.
104
- *
105
- * Each Incremental Data record also contains similar metadata, i.e. these records also contain
106
- * similar `isCompleted` and `children` properties.
107
- *
108
100
* @internal
109
101
*/
110
102
export class IncrementalPublisher {
111
- private _initialResult : {
112
- children : Set < IncrementalDataRecord > ;
113
- isCompleted : boolean ;
114
- } ;
115
-
116
- private _released : Set < IncrementalDataRecord > ;
117
- private _pending : Set < IncrementalDataRecord > ;
103
+ private _released : Set < SubsequentDataRecord > ;
104
+ private _pending : Set < SubsequentDataRecord > ;
118
105
119
106
// these are assigned within the Promise executor called synchronously within the constructor
120
107
private _signalled ! : Promise < unknown > ;
121
108
private _resolve ! : ( ) => void ;
122
109
123
110
constructor ( ) {
124
- this . _initialResult = {
125
- children : new Set ( ) ,
126
- isCompleted : false ,
127
- } ;
128
111
this . _released = new Set ( ) ;
129
112
this . _pending = new Set ( ) ;
130
113
this . _reset ( ) ;
@@ -210,19 +193,22 @@ export class IncrementalPublisher {
210
193
} ;
211
194
}
212
195
196
+ prepareInitialResultRecord ( ) : InitialResultRecord {
197
+ return {
198
+ errors : [ ] ,
199
+ children : new Set ( ) ,
200
+ } ;
201
+ }
202
+
213
203
prepareNewDeferredFragmentRecord ( opts : {
214
204
label : string | undefined ;
215
205
path : Path | undefined ;
216
- parentContext : IncrementalDataRecord | undefined ;
206
+ parentContext : IncrementalDataRecord ;
217
207
} ) : DeferredFragmentRecord {
218
208
const deferredFragmentRecord = new DeferredFragmentRecord ( opts ) ;
219
209
220
210
const parentContext = opts . parentContext ;
221
- if ( parentContext ) {
222
- parentContext . children . add ( deferredFragmentRecord ) ;
223
- } else {
224
- this . _initialResult . children . add ( deferredFragmentRecord ) ;
225
- }
211
+ parentContext . children . add ( deferredFragmentRecord ) ;
226
212
227
213
return deferredFragmentRecord ;
228
214
}
@@ -231,16 +217,12 @@ export class IncrementalPublisher {
231
217
label : string | undefined ;
232
218
path : Path | undefined ;
233
219
asyncIterator ?: AsyncIterator < unknown > ;
234
- parentContext : IncrementalDataRecord | undefined ;
220
+ parentContext : IncrementalDataRecord ;
235
221
} ) : StreamItemsRecord {
236
222
const streamItemsRecord = new StreamItemsRecord ( opts ) ;
237
223
238
224
const parentContext = opts . parentContext ;
239
- if ( parentContext ) {
240
- parentContext . children . add ( streamItemsRecord ) ;
241
- } else {
242
- this . _initialResult . children . add ( streamItemsRecord ) ;
243
- }
225
+ parentContext . children . add ( streamItemsRecord ) ;
244
226
245
227
return streamItemsRecord ;
246
228
}
@@ -274,36 +256,36 @@ export class IncrementalPublisher {
274
256
incrementalDataRecord . errors . push ( error ) ;
275
257
}
276
258
277
- publishInitial ( ) {
278
- for ( const child of this . _initialResult . children ) {
259
+ publishInitial ( initialResult : InitialResultRecord ) {
260
+ for ( const child of initialResult . children ) {
261
+ if ( child . filtered ) {
262
+ continue ;
263
+ }
279
264
this . _publish ( child ) ;
280
265
}
281
266
}
282
267
283
- filter (
284
- nullPath : Path ,
285
- erroringIncrementalDataRecord : IncrementalDataRecord | undefined ,
286
- ) {
268
+ getInitialErrors (
269
+ initialResult : InitialResultRecord ,
270
+ ) : ReadonlyArray < GraphQLError > {
271
+ return initialResult . errors ;
272
+ }
273
+
274
+ filter ( nullPath : Path , erroringIncrementalDataRecord : IncrementalDataRecord ) {
287
275
const nullPathArray = pathToArray ( nullPath ) ;
288
276
289
277
const asyncIterators = new Set < AsyncIterator < unknown > > ( ) ;
290
278
291
- const children =
292
- erroringIncrementalDataRecord === undefined
293
- ? this . _initialResult . children
294
- : erroringIncrementalDataRecord . children ;
279
+ const descendants = this . _getDescendants (
280
+ erroringIncrementalDataRecord . children ,
281
+ ) ;
295
282
296
- for ( const child of this . _getDescendants ( children ) ) {
283
+ for ( const child of descendants ) {
297
284
if ( ! this . _matchesPath ( child . path , nullPathArray ) ) {
298
285
continue ;
299
286
}
300
287
301
- this . _delete ( child ) ;
302
- const parent =
303
- child . parentContext === undefined
304
- ? this . _initialResult
305
- : child . parentContext ;
306
- parent . children . delete ( child ) ;
288
+ child . filtered = true ;
307
289
308
290
if ( isStreamItemsRecord ( child ) ) {
309
291
if ( child . asyncIterator !== undefined ) {
@@ -333,37 +315,34 @@ export class IncrementalPublisher {
333
315
this . _signalled = signalled ;
334
316
}
335
317
336
- private _introduce ( item : IncrementalDataRecord ) {
318
+ private _introduce ( item : SubsequentDataRecord ) {
337
319
this . _pending . add ( item ) ;
338
320
}
339
321
340
- private _release ( item : IncrementalDataRecord ) : void {
322
+ private _release ( item : SubsequentDataRecord ) : void {
341
323
if ( this . _pending . has ( item ) ) {
342
324
this . _released . add ( item ) ;
343
325
this . _trigger ( ) ;
344
326
}
345
327
}
346
328
347
- private _push ( item : IncrementalDataRecord ) : void {
329
+ private _push ( item : SubsequentDataRecord ) : void {
348
330
this . _released . add ( item ) ;
349
331
this . _pending . add ( item ) ;
350
332
this . _trigger ( ) ;
351
333
}
352
334
353
- private _delete ( item : IncrementalDataRecord ) {
354
- this . _released . delete ( item ) ;
355
- this . _pending . delete ( item ) ;
356
- this . _trigger ( ) ;
357
- }
358
-
359
335
private _getIncrementalResult (
360
- completedRecords : ReadonlySet < IncrementalDataRecord > ,
336
+ completedRecords : ReadonlySet < SubsequentDataRecord > ,
361
337
) : SubsequentIncrementalExecutionResult | undefined {
362
338
const incrementalResults : Array < IncrementalResult > = [ ] ;
363
339
let encounteredCompletedAsyncIterator = false ;
364
340
for ( const incrementalDataRecord of completedRecords ) {
365
341
const incrementalResult : IncrementalResult = { } ;
366
342
for ( const child of incrementalDataRecord . children ) {
343
+ if ( child . filtered ) {
344
+ continue ;
345
+ }
367
346
this . _publish ( child ) ;
368
347
}
369
348
if ( isStreamItemsRecord ( incrementalDataRecord ) ) {
@@ -396,18 +375,18 @@ export class IncrementalPublisher {
396
375
: undefined ;
397
376
}
398
377
399
- private _publish ( incrementalDataRecord : IncrementalDataRecord ) {
400
- if ( incrementalDataRecord . isCompleted ) {
401
- this . _push ( incrementalDataRecord ) ;
378
+ private _publish ( subsequentResultRecord : SubsequentDataRecord ) {
379
+ if ( subsequentResultRecord . isCompleted ) {
380
+ this . _push ( subsequentResultRecord ) ;
402
381
} else {
403
- this . _introduce ( incrementalDataRecord ) ;
382
+ this . _introduce ( subsequentResultRecord ) ;
404
383
}
405
384
}
406
385
407
386
private _getDescendants (
408
- children : ReadonlySet < IncrementalDataRecord > ,
409
- descendants = new Set < IncrementalDataRecord > ( ) ,
410
- ) : ReadonlySet < IncrementalDataRecord > {
387
+ children : ReadonlySet < SubsequentDataRecord > ,
388
+ descendants = new Set < SubsequentDataRecord > ( ) ,
389
+ ) : ReadonlySet < SubsequentDataRecord > {
411
390
for ( const child of children ) {
412
391
descendants . add ( child ) ;
413
392
this . _getDescendants ( child . children , descendants ) ;
@@ -429,26 +408,27 @@ export class IncrementalPublisher {
429
408
}
430
409
}
431
410
411
+ export interface InitialResultRecord {
412
+ errors : Array < GraphQLError > ;
413
+ children : Set < SubsequentDataRecord > ;
414
+ }
415
+
432
416
/** @internal */
433
417
export class DeferredFragmentRecord {
434
418
errors : Array < GraphQLError > ;
435
419
label : string | undefined ;
436
420
path : Array < string | number > ;
437
421
data : ObjMap < unknown > | null ;
438
- parentContext : IncrementalDataRecord | undefined ;
439
- children : Set < IncrementalDataRecord > ;
422
+ children : Set < SubsequentDataRecord > ;
440
423
isCompleted : boolean ;
441
- constructor ( opts : {
442
- label : string | undefined ;
443
- path : Path | undefined ;
444
- parentContext : IncrementalDataRecord | undefined ;
445
- } ) {
424
+ filtered : boolean ;
425
+ constructor ( opts : { label : string | undefined ; path : Path | undefined } ) {
446
426
this . label = opts . label ;
447
427
this . path = pathToArray ( opts . path ) ;
448
- this . parentContext = opts . parentContext ;
449
428
this . errors = [ ] ;
450
429
this . children = new Set ( ) ;
451
430
this . isCompleted = false ;
431
+ this . filtered = false ;
452
432
this . data = null ;
453
433
}
454
434
}
@@ -459,33 +439,34 @@ export class StreamItemsRecord {
459
439
label : string | undefined ;
460
440
path : Array < string | number > ;
461
441
items : Array < unknown > | null ;
462
- parentContext : IncrementalDataRecord | undefined ;
463
- children : Set < IncrementalDataRecord > ;
442
+ children : Set < SubsequentDataRecord > ;
464
443
asyncIterator : AsyncIterator < unknown > | undefined ;
465
444
isCompletedAsyncIterator ?: boolean ;
466
445
isCompleted : boolean ;
446
+ filtered : boolean ;
467
447
constructor ( opts : {
468
448
label : string | undefined ;
469
449
path : Path | undefined ;
470
450
asyncIterator ?: AsyncIterator < unknown > ;
471
- parentContext : IncrementalDataRecord | undefined ;
472
451
} ) {
473
452
this . items = null ;
474
453
this . label = opts . label ;
475
454
this . path = pathToArray ( opts . path ) ;
476
- this . parentContext = opts . parentContext ;
477
455
this . asyncIterator = opts . asyncIterator ;
478
456
this . errors = [ ] ;
479
457
this . children = new Set ( ) ;
480
458
this . isCompleted = false ;
459
+ this . filtered = false ;
481
460
this . items = null ;
482
461
}
483
462
}
484
463
485
- export type IncrementalDataRecord = DeferredFragmentRecord | StreamItemsRecord ;
464
+ export type SubsequentDataRecord = DeferredFragmentRecord | StreamItemsRecord ;
465
+
466
+ export type IncrementalDataRecord = InitialResultRecord | SubsequentDataRecord ;
486
467
487
468
function isStreamItemsRecord (
488
- incrementalDataRecord : IncrementalDataRecord ,
489
- ) : incrementalDataRecord is StreamItemsRecord {
490
- return incrementalDataRecord instanceof StreamItemsRecord ;
469
+ subsequentResultRecord : SubsequentDataRecord ,
470
+ ) : subsequentResultRecord is StreamItemsRecord {
471
+ return subsequentResultRecord instanceof StreamItemsRecord ;
491
472
}
0 commit comments