-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathIncrementalGraph.ts
317 lines (292 loc) · 9.84 KB
/
IncrementalGraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { BoxedPromiseOrValue } from '../jsutils/BoxedPromiseOrValue.js';
import { invariant } from '../jsutils/invariant.js';
import { isPromise } from '../jsutils/isPromise.js';
import { promiseWithResolvers } from '../jsutils/promiseWithResolvers.js';
import type { GraphQLError } from '../error/GraphQLError.js';
import type {
DeferredFragmentRecord,
DeliveryGroup,
IncrementalDataRecord,
IncrementalDataRecordResult,
PendingExecutionGroup,
StreamItemRecord,
StreamRecord,
SuccessfulExecutionGroup,
} from './types.js';
import { isDeferredFragmentRecord, isPendingExecutionGroup } from './types.js';
/**
* @internal
*/
export class IncrementalGraph {
private _rootNodes: Set<DeliveryGroup>;
private _completedQueue: Array<IncrementalDataRecordResult>;
private _nextQueue: Array<
(iterable: Iterable<IncrementalDataRecordResult> | undefined) => void
>;
constructor() {
this._rootNodes = new Set();
this._completedQueue = [];
this._nextQueue = [];
}
getNewRootNodes(
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
): ReadonlyArray<DeliveryGroup> {
const initialResultChildren = new Set<DeliveryGroup>();
this._addIncrementalDataRecords(
incrementalDataRecords,
undefined,
initialResultChildren,
);
return this._promoteNonEmptyToRoot(initialResultChildren);
}
addCompletedSuccessfulExecutionGroup(
successfulExecutionGroup: SuccessfulExecutionGroup,
): void {
const { pendingExecutionGroup, incrementalDataRecords } =
successfulExecutionGroup;
const deferredFragmentRecords =
pendingExecutionGroup.deferredFragmentRecords;
for (const deferredFragmentRecord of deferredFragmentRecords) {
const { pendingExecutionGroups, successfulExecutionGroups } =
deferredFragmentRecord;
pendingExecutionGroups.delete(pendingExecutionGroup);
successfulExecutionGroups.add(successfulExecutionGroup);
}
if (incrementalDataRecords !== undefined) {
this._addIncrementalDataRecords(
incrementalDataRecords,
deferredFragmentRecords,
);
}
}
*currentCompletedBatch(): Generator<IncrementalDataRecordResult> {
let completed;
while ((completed = this._completedQueue.shift()) !== undefined) {
yield completed;
}
if (this._rootNodes.size === 0) {
for (const resolve of this._nextQueue) {
resolve(undefined);
}
}
}
nextCompletedBatch(): Promise<
Iterable<IncrementalDataRecordResult> | undefined
> {
const { promise, resolve } = promiseWithResolvers<
Iterable<IncrementalDataRecordResult> | undefined
>();
this._nextQueue.push(resolve);
return promise;
}
abort(): void {
for (const resolve of this._nextQueue) {
resolve(undefined);
}
}
hasNext(): boolean {
return this._rootNodes.size > 0;
}
completeDeferredFragment(deferredFragmentRecord: DeferredFragmentRecord):
| {
newRootNodes: ReadonlyArray<DeliveryGroup>;
successfulExecutionGroups: ReadonlyArray<SuccessfulExecutionGroup>;
}
| undefined {
if (
!this._rootNodes.has(deferredFragmentRecord) ||
deferredFragmentRecord.pendingExecutionGroups.size > 0
) {
return;
}
const successfulExecutionGroups = Array.from(
deferredFragmentRecord.successfulExecutionGroups,
);
this._rootNodes.delete(deferredFragmentRecord);
for (const successfulExecutionGroup of successfulExecutionGroups) {
for (const otherDeferredFragmentRecord of successfulExecutionGroup
.pendingExecutionGroup.deferredFragmentRecords) {
otherDeferredFragmentRecord.successfulExecutionGroups.delete(
successfulExecutionGroup,
);
}
}
const newRootNodes = this._promoteNonEmptyToRoot(
deferredFragmentRecord.children,
);
return { newRootNodes, successfulExecutionGroups };
}
removeDeferredFragment(deferredFragmentRecord: DeferredFragmentRecord): void {
this._rootNodes.delete(deferredFragmentRecord);
}
removeStream(streamRecord: StreamRecord): void {
this._rootNodes.delete(streamRecord);
}
private _addIncrementalDataRecords(
incrementalDataRecords: ReadonlyArray<IncrementalDataRecord>,
parents: ReadonlyArray<DeferredFragmentRecord> | undefined,
initialResultChildren?: Set<DeliveryGroup>,
): void {
for (const incrementalDataRecord of incrementalDataRecords) {
if (isPendingExecutionGroup(incrementalDataRecord)) {
for (const deferredFragmentRecord of incrementalDataRecord.deferredFragmentRecords) {
this._addDeferredFragment(
deferredFragmentRecord,
initialResultChildren,
);
deferredFragmentRecord.pendingExecutionGroups.add(
incrementalDataRecord,
);
}
if (this._completesRootNode(incrementalDataRecord)) {
this._onExecutionGroup(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(
maybeEmptyNewRootNodes: Set<DeliveryGroup>,
): ReadonlyArray<DeliveryGroup> {
const newRootNodes: Array<DeliveryGroup> = [];
for (const node of maybeEmptyNewRootNodes) {
if (isDeferredFragmentRecord(node)) {
if (node.pendingExecutionGroups.size > 0) {
for (const pendingExecutionGroup of node.pendingExecutionGroups) {
if (!this._completesRootNode(pendingExecutionGroup)) {
this._onExecutionGroup(pendingExecutionGroup);
}
}
this._rootNodes.add(node);
newRootNodes.push(node);
continue;
}
for (const child of node.children) {
maybeEmptyNewRootNodes.add(child);
}
} else {
this._rootNodes.add(node);
newRootNodes.push(node);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this._onStreamItems(node);
}
}
return newRootNodes;
}
private _completesRootNode(
pendingExecutionGroup: PendingExecutionGroup,
): boolean {
return pendingExecutionGroup.deferredFragmentRecords.some(
(deferredFragmentRecord) => this._rootNodes.has(deferredFragmentRecord),
);
}
private _addDeferredFragment(
deferredFragmentRecord: DeferredFragmentRecord,
initialResultChildren: Set<DeliveryGroup> | undefined,
): void {
if (
deferredFragmentRecord.failed ||
this._rootNodes.has(deferredFragmentRecord)
) {
return;
}
const parent = deferredFragmentRecord.parent;
if (parent === undefined) {
invariant(initialResultChildren !== undefined);
initialResultChildren.add(deferredFragmentRecord);
return;
}
parent.children.add(deferredFragmentRecord);
this._addDeferredFragment(parent, initialResultChildren);
}
private _onExecutionGroup(
pendingExecutionGroup: PendingExecutionGroup,
): void {
let completedExecutionGroup = pendingExecutionGroup.result;
if (!(completedExecutionGroup instanceof BoxedPromiseOrValue)) {
completedExecutionGroup = completedExecutionGroup();
}
const value = completedExecutionGroup.value;
if (isPromise(value)) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
value.then((resolved) => this._enqueue(resolved));
} else {
this._enqueue(value);
}
}
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 = [];
}
// eslint-disable-next-line no-await-in-loop
result = await result;
// wait an additional tick to coalesce resolving additional promises
// within the queue
// eslint-disable-next-line no-await-in-loop
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 _enqueue(completed: IncrementalDataRecordResult): void {
this._completedQueue.push(completed);
const next = this._nextQueue.shift();
if (next === undefined) {
return;
}
next(this.currentCompletedBatch());
}
}