Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 44 additions & 41 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,25 +301,11 @@ export class IncrementalPublisher {
initialResultRecord: InitialResultRecord,
data: ObjMap<unknown> | null,
): ExecutionResult | ExperimentalIncrementalExecutionResults {
for (const child of initialResultRecord.children) {
if (child.filtered) {
continue;
}
this._publish(child);
}
const pendingSources = this._publish(initialResultRecord.children);

const errors = initialResultRecord.errors;
const initialResult = errors.length === 0 ? { data } : { errors, data };
const pending = this._pending;
if (pending.size > 0) {
const pendingSources = new Set<DeferredFragmentRecord | StreamRecord>();
for (const subsequentResultRecord of pending) {
const pendingSource = isStreamItemsRecord(subsequentResultRecord)
? subsequentResultRecord.streamRecord
: subsequentResultRecord;
pendingSources.add(pendingSource);
}

if (pendingSources.size > 0) {
return {
initialResult: {
...initialResult,
Expand Down Expand Up @@ -538,18 +524,7 @@ export class IncrementalPublisher {
const incrementalResults: Array<IncrementalResult> = [];
const completedResults: Array<CompletedResult> = [];
for (const subsequentResultRecord of completedRecords) {
for (const child of subsequentResultRecord.children) {
if (child.filtered) {
continue;
}
const pendingSource = isStreamItemsRecord(child)
? child.streamRecord
: child;
if (!pendingSource.pendingSent) {
newPendingSources.add(pendingSource);
}
this._publish(child);
}
this._publish(subsequentResultRecord.children, newPendingSources);
if (isStreamItemsRecord(subsequentResultRecord)) {
if (subsequentResultRecord.isFinalRecord) {
newPendingSources.delete(subsequentResultRecord.streamRecord);
Expand Down Expand Up @@ -613,6 +588,8 @@ export class IncrementalPublisher {
let idWithLongestPath: string | undefined;
for (const deferredFragmentRecord of deferredFragmentRecords) {
const id = deferredFragmentRecord.id;
// TODO: add test
/* c8 ignore next 3 */
if (id === undefined) {
continue;
}
Expand Down Expand Up @@ -655,25 +632,51 @@ export class IncrementalPublisher {
return result;
}

private _publish(subsequentResultRecord: SubsequentResultRecord): void {
if (isStreamItemsRecord(subsequentResultRecord)) {
if (subsequentResultRecord.isCompleted) {
private _publish(
subsequentResultRecords: ReadonlySet<SubsequentResultRecord>,
pendingSources = new Set<DeferredFragmentRecord | StreamRecord>(),
): Set<DeferredFragmentRecord | StreamRecord> {
const emptyRecords: Array<SubsequentResultRecord> = [];

for (const subsequentResultRecord of subsequentResultRecords) {
if (subsequentResultRecord.filtered) {
continue;
}
if (isStreamItemsRecord(subsequentResultRecord)) {
if (subsequentResultRecord.isCompleted) {
this._push(subsequentResultRecord);
} else {
this._introduce(subsequentResultRecord);
}

const stream = subsequentResultRecord.streamRecord;
if (!stream.pendingSent) {
pendingSources.add(stream);
}
continue;
}

if (subsequentResultRecord._pending.size > 0) {
this._introduce(subsequentResultRecord);
} else if (
subsequentResultRecord.deferredGroupedFieldSetRecords.size === 0
) {
emptyRecords.push(subsequentResultRecord);
continue;
} else {
this._push(subsequentResultRecord);
return;
}

this._introduce(subsequentResultRecord);
return;
if (!subsequentResultRecord.pendingSent) {
pendingSources.add(subsequentResultRecord);
}
}

if (subsequentResultRecord._pending.size > 0) {
this._introduce(subsequentResultRecord);
} else if (
subsequentResultRecord.deferredGroupedFieldSetRecords.size > 0 ||
subsequentResultRecord.children.size > 0
) {
this._push(subsequentResultRecord);
for (const emptyRecord of emptyRecords) {
this._publish(emptyRecord.children, pendingSources);
}

return pendingSources;
}

private _getChildren(
Expand Down
15 changes: 4 additions & 11 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,34 +1023,27 @@ describe('Execute: defer directive', () => {
},
},
pending: [
{ id: '0', path: ['hero'] },
{ id: '0', path: ['hero', 'nestedObject', 'deeperObject'] },
{ id: '1', path: ['hero', 'nestedObject', 'deeperObject'] },
],
hasNext: true,
},
{
pending: [{ id: '2', path: ['hero', 'nestedObject', 'deeperObject'] }],
incremental: [
{
data: {
foo: 'foo',
},
id: '1',
id: '0',
},
],
completed: [{ id: '0' }, { id: '1' }],
hasNext: true,
},
{
incremental: [
{
data: {
bar: 'bar',
},
id: '2',
id: '1',
},
],
completed: [{ id: '2' }],
completed: [{ id: '0' }, { id: '1' }],
hasNext: false,
},
]);
Expand Down