-
-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade executor to non-duplicating incremental delivery format
- Loading branch information
Showing
28 changed files
with
6,053 additions
and
1,607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
'@graphql-tools/executor': major | ||
'@graphql-tools/utils': minor | ||
--- | ||
|
||
Upgrade to non-duplicating Incremental Delivery format | ||
|
||
## Description | ||
|
||
GraphQL Incremental Delivery is moving to a [new response format without duplication](https://github.com/graphql/defer-stream-wg/discussions/69). | ||
|
||
This PR updates the executor within graphql-tools to follow the new format, a BREAKING CHANGE. | ||
|
||
Incremental Delivery has now been disabled for subscriptions, also a BREAKING CHANGE. The GraphQL Working Group has decided to disable incremental delivery support for subscriptions (1) to gather more information about use cases and (2) explore how to interleaving the incremental response streams generated from different source events into one overall subscription response stream. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* ES6 Map with additional `add` method to accumulate items. | ||
*/ | ||
export class AccumulatorMap<K, T> extends Map<K, Array<T>> { | ||
get [Symbol.toStringTag]() { | ||
return 'AccumulatorMap'; | ||
} | ||
|
||
add(key: K, item: T): void { | ||
const group = this.get(key); | ||
if (group === undefined) { | ||
this.set(key, [item]); | ||
} else { | ||
group.push(item); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isPromise } from '@graphql-tools/utils'; | ||
import type { MaybePromise } from '@graphql-tools/utils'; | ||
|
||
/** | ||
* A BoxedPromiseOrValue is a container for a value or promise where the value | ||
* will be updated when the promise resolves. | ||
* | ||
* A BoxedPromiseOrValue may only be used with promises whose possible | ||
* rejection has already been handled, otherwise this will lead to unhandled | ||
* promise rejections. | ||
* | ||
* @internal | ||
* */ | ||
export class BoxedPromiseOrValue<T> { | ||
value: MaybePromise<T>; | ||
|
||
constructor(value: MaybePromise<T>) { | ||
this.value = value; | ||
if (isPromise(value)) { | ||
value.then(resolved => { | ||
this.value = resolved; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,314 @@ | ||
import type { GraphQLError } from 'graphql'; | ||
import { isPromise } from '@graphql-tools/utils'; | ||
import { BoxedPromiseOrValue } from './BoxedPromiseOrValue.js'; | ||
import { invariant } from './invariant.js'; | ||
import { promiseWithResolvers } from './promiseWithResolvers.js'; | ||
import type { | ||
DeferredFragmentRecord, | ||
DeferredGroupedFieldSetRecord, | ||
DeferredGroupedFieldSetResult, | ||
IncrementalDataRecord, | ||
IncrementalDataRecordResult, | ||
ReconcilableDeferredGroupedFieldSetResult, | ||
StreamItemRecord, | ||
StreamRecord, | ||
SubsequentResultRecord, | ||
} from './types.js'; | ||
import { isDeferredFragmentRecord, isDeferredGroupedFieldSetRecord } from './types.js'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export class IncrementalGraph { | ||
private _rootNodes: Set<SubsequentResultRecord>; | ||
|
||
private _completedQueue: Array<IncrementalDataRecordResult>; | ||
private _nextQueue: Array< | ||
(iterable: IteratorResult<Iterable<IncrementalDataRecordResult>>) => void | ||
>; | ||
|
||
constructor() { | ||
this._rootNodes = new Set(); | ||
this._completedQueue = []; | ||
this._nextQueue = []; | ||
} | ||
|
||
getNewPending( | ||
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>, | ||
): ReadonlyArray<SubsequentResultRecord> { | ||
const initialResultChildren = new Set<SubsequentResultRecord>(); | ||
this._addIncrementalDataRecords(incrementalDataRecords, undefined, initialResultChildren); | ||
return this._promoteNonEmptyToRoot(initialResultChildren); | ||
} | ||
|
||
addCompletedReconcilableDeferredGroupedFieldSet( | ||
reconcilableResult: ReconcilableDeferredGroupedFieldSetResult, | ||
): void { | ||
for (const deferredFragmentRecord of reconcilableResult.deferredGroupedFieldSetRecord | ||
.deferredFragmentRecords) { | ||
deferredFragmentRecord.deferredGroupedFieldSetRecords.delete( | ||
reconcilableResult.deferredGroupedFieldSetRecord, | ||
); | ||
deferredFragmentRecord.reconcilableResults.add(reconcilableResult); | ||
} | ||
|
||
const incrementalDataRecords = reconcilableResult.incrementalDataRecords; | ||
if (incrementalDataRecords !== undefined) { | ||
this._addIncrementalDataRecords( | ||
incrementalDataRecords, | ||
reconcilableResult.deferredGroupedFieldSetRecord.deferredFragmentRecords, | ||
); | ||
} | ||
} | ||
|
||
completedIncrementalData() { | ||
return { | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
}, | ||
next: (): Promise<IteratorResult<Iterable<IncrementalDataRecordResult>>> => { | ||
const firstResult = this._completedQueue.shift(); | ||
if (firstResult !== undefined) { | ||
return Promise.resolve({ | ||
value: this._yieldCurrentCompletedIncrementalData(firstResult), | ||
done: false, | ||
}); | ||
} | ||
const { promise, resolve } = | ||
promiseWithResolvers<IteratorResult<Iterable<IncrementalDataRecordResult>>>(); | ||
this._nextQueue.push(resolve); | ||
return promise; | ||
}, | ||
return: (): Promise<IteratorResult<Iterable<IncrementalDataRecordResult>>> => { | ||
for (const resolve of this._nextQueue) { | ||
resolve({ value: undefined, done: true }); | ||
} | ||
return Promise.resolve({ value: undefined, done: true }); | ||
}, | ||
}; | ||
} | ||
|
||
hasNext(): boolean { | ||
return this._rootNodes.size > 0; | ||
} | ||
|
||
completeDeferredFragment(deferredFragmentRecord: DeferredFragmentRecord): | ||
| { | ||
newPending: ReadonlyArray<SubsequentResultRecord>; | ||
reconcilableResults: ReadonlyArray<ReconcilableDeferredGroupedFieldSetResult>; | ||
} | ||
| undefined { | ||
if ( | ||
!this._rootNodes.has(deferredFragmentRecord) || | ||
deferredFragmentRecord.deferredGroupedFieldSetRecords.size > 0 | ||
) { | ||
return; | ||
} | ||
const reconcilableResults = Array.from(deferredFragmentRecord.reconcilableResults); | ||
this._removePending(deferredFragmentRecord); | ||
for (const reconcilableResult of reconcilableResults) { | ||
for (const otherDeferredFragmentRecord of reconcilableResult.deferredGroupedFieldSetRecord | ||
.deferredFragmentRecords) { | ||
otherDeferredFragmentRecord.reconcilableResults.delete(reconcilableResult); | ||
} | ||
} | ||
const newPending = this._promoteNonEmptyToRoot(deferredFragmentRecord.children); | ||
return { newPending, reconcilableResults }; | ||
} | ||
|
||
removeDeferredFragment(deferredFragmentRecord: DeferredFragmentRecord): boolean { | ||
if (!this._rootNodes.has(deferredFragmentRecord)) { | ||
return false; | ||
} | ||
this._removePending(deferredFragmentRecord); | ||
return true; | ||
} | ||
|
||
removeStream(streamRecord: StreamRecord): void { | ||
this._removePending(streamRecord); | ||
} | ||
|
||
private _removePending(subsequentResultRecord: SubsequentResultRecord): void { | ||
this._rootNodes.delete(subsequentResultRecord); | ||
if (this._rootNodes.size === 0) { | ||
for (const resolve of this._nextQueue) { | ||
resolve({ value: undefined, done: true }); | ||
} | ||
} | ||
} | ||
|
||
private _addIncrementalDataRecords( | ||
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>, | ||
parents: ReadonlyArray<DeferredFragmentRecord> | undefined, | ||
initialResultChildren?: Set<SubsequentResultRecord> | undefined, | ||
): void { | ||
for (const incrementalDataRecord of incrementalDataRecords) { | ||
if (isDeferredGroupedFieldSetRecord(incrementalDataRecord)) { | ||
for (const deferredFragmentRecord of incrementalDataRecord.deferredFragmentRecords) { | ||
this._addDeferredFragment(deferredFragmentRecord, initialResultChildren); | ||
deferredFragmentRecord.deferredGroupedFieldSetRecords.add(incrementalDataRecord); | ||
} | ||
if (this._hasPendingFragment(incrementalDataRecord)) { | ||
this._onDeferredGroupedFieldSet(incrementalDataRecord); | ||
} | ||
} else if (parents === undefined) { | ||
invariant(initialResultChildren !== undefined); | ||
initialResultChildren.add(incrementalDataRecord); | ||
} else { | ||
for (const parent of parents) { | ||
this._addDeferredFragment(parent, initialResultChildren); | ||
parent.children.add(incrementalDataRecord); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private _promoteNonEmptyToRoot( | ||
maybeEmptyNewPending: Set<SubsequentResultRecord>, | ||
): ReadonlyArray<SubsequentResultRecord> { | ||
const newPending: Array<SubsequentResultRecord> = []; | ||
for (const subsequentResultRecord of maybeEmptyNewPending) { | ||
if (isDeferredFragmentRecord(subsequentResultRecord)) { | ||
if (subsequentResultRecord.deferredGroupedFieldSetRecords.size > 0) { | ||
subsequentResultRecord.setAsPending(); | ||
for (const deferredGroupedFieldSetRecord of subsequentResultRecord.deferredGroupedFieldSetRecords) { | ||
if (!this._hasPendingFragment(deferredGroupedFieldSetRecord)) { | ||
this._onDeferredGroupedFieldSet(deferredGroupedFieldSetRecord); | ||
} | ||
} | ||
this._rootNodes.add(subsequentResultRecord); | ||
newPending.push(subsequentResultRecord); | ||
continue; | ||
} | ||
for (const child of subsequentResultRecord.children) { | ||
maybeEmptyNewPending.add(child); | ||
} | ||
} else { | ||
this._rootNodes.add(subsequentResultRecord); | ||
newPending.push(subsequentResultRecord); | ||
|
||
this._onStreamItems(subsequentResultRecord); | ||
} | ||
} | ||
return newPending; | ||
} | ||
|
||
private _hasPendingFragment( | ||
deferredGroupedFieldSetRecord: DeferredGroupedFieldSetRecord, | ||
): boolean { | ||
return deferredGroupedFieldSetRecord.deferredFragmentRecords.some(deferredFragmentRecord => | ||
this._rootNodes.has(deferredFragmentRecord), | ||
); | ||
} | ||
|
||
private _addDeferredFragment( | ||
deferredFragmentRecord: DeferredFragmentRecord, | ||
subsequentResultRecords: Set<SubsequentResultRecord> | undefined, | ||
): void { | ||
if (this._rootNodes.has(deferredFragmentRecord)) { | ||
return; | ||
} | ||
const parent = deferredFragmentRecord.parent; | ||
if (parent === undefined) { | ||
invariant(subsequentResultRecords !== undefined); | ||
subsequentResultRecords.add(deferredFragmentRecord); | ||
return; | ||
} | ||
parent.children.add(deferredFragmentRecord); | ||
this._addDeferredFragment(parent, subsequentResultRecords); | ||
} | ||
|
||
private _onDeferredGroupedFieldSet( | ||
deferredGroupedFieldSetRecord: DeferredGroupedFieldSetRecord, | ||
): void { | ||
const result = ( | ||
deferredGroupedFieldSetRecord.result as BoxedPromiseOrValue<DeferredGroupedFieldSetResult> | ||
).value; | ||
if (isPromise(result)) { | ||
result.then(resolved => this._enqueue(resolved)); | ||
} else { | ||
this._enqueue(result); | ||
} | ||
} | ||
|
||
private async _onStreamItems(streamRecord: StreamRecord): Promise<void> { | ||
let items: Array<unknown> = []; | ||
let errors: Array<GraphQLError> = []; | ||
let incrementalDataRecords: Array<IncrementalDataRecord> = []; | ||
const streamItemQueue = streamRecord.streamItemQueue; | ||
let streamItemRecord: StreamItemRecord | undefined; | ||
while ((streamItemRecord = streamItemQueue.shift()) !== undefined) { | ||
let result = | ||
streamItemRecord instanceof BoxedPromiseOrValue | ||
? streamItemRecord.value | ||
: streamItemRecord().value; | ||
if (isPromise(result)) { | ||
if (items.length > 0) { | ||
this._enqueue({ | ||
streamRecord, | ||
result: | ||
// TODO add additional test case or rework for coverage | ||
errors.length > 0 /* c8 ignore start */ | ||
? { items, errors } /* c8 ignore stop */ | ||
: { items }, | ||
incrementalDataRecords, | ||
}); | ||
items = []; | ||
errors = []; | ||
incrementalDataRecords = []; | ||
} | ||
result = await result; | ||
// wait an additional tick to coalesce resolving additional promises | ||
// within the queue | ||
await Promise.resolve(); | ||
} | ||
if (result.item === undefined) { | ||
if (items.length > 0) { | ||
this._enqueue({ | ||
streamRecord, | ||
result: errors.length > 0 ? { items, errors } : { items }, | ||
incrementalDataRecords, | ||
}); | ||
} | ||
this._enqueue( | ||
result.errors === undefined | ||
? { streamRecord } | ||
: { | ||
streamRecord, | ||
errors: result.errors, | ||
}, | ||
); | ||
return; | ||
} | ||
items.push(result.item); | ||
if (result.errors !== undefined) { | ||
errors.push(...result.errors); | ||
} | ||
if (result.incrementalDataRecords !== undefined) { | ||
incrementalDataRecords.push(...result.incrementalDataRecords); | ||
} | ||
} | ||
} | ||
|
||
private *_yieldCurrentCompletedIncrementalData( | ||
first: IncrementalDataRecordResult, | ||
): Generator<IncrementalDataRecordResult> { | ||
yield first; | ||
let completed; | ||
while ((completed = this._completedQueue.shift()) !== undefined) { | ||
yield completed; | ||
} | ||
} | ||
|
||
private _enqueue(completed: IncrementalDataRecordResult): void { | ||
const next = this._nextQueue.shift(); | ||
if (next !== undefined) { | ||
next({ | ||
value: this._yieldCurrentCompletedIncrementalData(completed), | ||
done: false, | ||
}); | ||
return; | ||
} | ||
this._completedQueue.push(completed); | ||
} | ||
} |
Oops, something went wrong.