Skip to content

fix(incremental): failed fragments should never be re-added to the graph #4357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions src/execution/IncrementalGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,8 @@ export class IncrementalGraph {
return { newRootNodes, successfulExecutionGroups };
}

removeDeferredFragment(
deferredFragmentRecord: DeferredFragmentRecord,
): boolean {
if (!this._rootNodes.has(deferredFragmentRecord)) {
return false;
}
removeDeferredFragment(deferredFragmentRecord: DeferredFragmentRecord): void {
this._rootNodes.delete(deferredFragmentRecord);
return true;
}

removeStream(streamRecord: StreamRecord): void {
Expand Down Expand Up @@ -219,7 +213,10 @@ export class IncrementalGraph {
deferredFragmentRecord: DeferredFragmentRecord,
initialResultChildren: Set<DeliveryGroup> | undefined,
): void {
if (this._rootNodes.has(deferredFragmentRecord)) {
if (
deferredFragmentRecord.failed ||
this._rootNodes.has(deferredFragmentRecord)
) {
return;
}
const parent = deferredFragmentRecord.parent;
Expand Down
10 changes: 5 additions & 5 deletions src/execution/IncrementalPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ class IncrementalPublisher {
if (isFailedExecutionGroup(completedExecutionGroup)) {
for (const deferredFragmentRecord of completedExecutionGroup
.pendingExecutionGroup.deferredFragmentRecords) {
const id = deferredFragmentRecord.id;
if (
!this._incrementalGraph.removeDeferredFragment(deferredFragmentRecord)
) {
// This can occur if multiple deferred grouped field sets error for a fragment.
const failed = deferredFragmentRecord.failed;
if (failed) {
continue;
}
deferredFragmentRecord.failed = true;
const id = deferredFragmentRecord.id;
this._incrementalGraph.removeDeferredFragment(deferredFragmentRecord);
invariant(id !== undefined);
context.completed.push({
id,
Expand Down
78 changes: 78 additions & 0 deletions src/execution/__tests__/defer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const a = new GraphQLObjectType({
fields: {
b: { type: b },
someField: { type: GraphQLString },
nonNullErrorField: { type: new GraphQLNonNull(GraphQLString) },
},
name: 'a',
});
Expand Down Expand Up @@ -1688,6 +1689,83 @@ describe('Execute: defer directive', () => {
]);
});

it('Nulls cross defer boundaries, failed fragment with slower shared child execution groups', async () => {
const document = parse(`
query {
... @defer {
a {
someField
nonNullErrorField
b {
c {
d
}
}
}
}
a {
... @defer {
someField
b {
e {
f
}
}
}
}
}
`);
const result = await complete(document, {
a: {
b: { c: { d: 'd' }, e: { f: 'f' } },
someField: Promise.resolve('someField'),
},
});
expectJSON(result).toDeepEqual([
{
data: {
a: {},
},
pending: [
{ id: '0', path: [] },
{ id: '1', path: ['a'] },
],
hasNext: true,
},
{
completed: [
{
id: '0',
errors: [
{
message:
'Cannot return null for non-nullable field a.nonNullErrorField.',
locations: [{ line: 6, column: 13 }],
path: ['a', 'nonNullErrorField'],
},
],
},
],
hasNext: true,
},
{
incremental: [
{
data: { b: {}, someField: 'someField' },
id: '1',
},
{
data: { e: { f: 'f' } },
id: '1',
subPath: ['b'],
},
],
completed: [{ id: '1' }],
hasNext: false,
},
]);
});

it('Handles multiple erroring deferred grouped field sets', async () => {
const document = parse(`
query {
Expand Down
1 change: 1 addition & 0 deletions src/execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export class DeferredFragmentRecord {
pendingExecutionGroups: Set<PendingExecutionGroup>;
successfulExecutionGroups: Set<SuccessfulExecutionGroup>;
children: Set<DeliveryGroup>;
failed: true | undefined;

constructor(
path: Path | undefined,
Expand Down
Loading