Skip to content
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

fix: ensure we are removing ALL useless groups #3163

Merged
merged 2 commits into from
Oct 10, 2024
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
7 changes: 7 additions & 0 deletions .changeset/quiet-rings-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@apollo/query-planner": patch
---

Ensure all useless fetch groups are removed

When removing "useless" fetch nodes/groups we remove them in-place while still iterating over the same list. This leads to potentially skipping processing of some the children fetch nodes, as when we remove nodes we left shift all remaining children but the iterator keeps the old position unchanged effectively skipping next child.
125 changes: 125 additions & 0 deletions query-planner-js/src/__tests__/buildPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10124,3 +10124,128 @@ describe('@fromContext impacts on query planning', () => {
]);
});
});

test('ensure we are removing all useless groups', () => {
const subgraph1 = {
name: 'Subgraph1',
typeDefs: gql`
type Query {
foo: Foo!
}

type Foo {
item: I
}

type I @interfaceObject @key(fields: "id") {
id: ID!
}
`,
};

const subgraph2 = {
name: 'Subgraph2',
typeDefs: gql`
interface I @key(fields: "id") {
id: ID!
name: String!
}

type A implements I @key(fields: "id") {
id: ID!
name: String!
x: X!
}

type X {
id: ID!
}

type B implements I @key(fields: "id") {
id: ID!
name: String!
y: Y!
}

type Y {
id: ID!
}
`,
};

const [api, queryPlanner] = composeAndCreatePlanner(subgraph1, subgraph2);
const operation = operationFromDocument(
api,
gql`
{
foo {
item {
__typename
id
... on A {
__typename
id
x {
__typename
id
}
}
... on B {
__typename
id
y {
__typename
id
}
}
}
}
}
`,
);

const plan = queryPlanner.buildQueryPlan(operation);
expect(plan).toMatchInlineSnapshot(`
QueryPlan {
Sequence {
Fetch(service: "Subgraph1") {
{
foo {
item {
__typename
id
}
}
}
},
Flatten(path: "foo.item") {
Fetch(service: "Subgraph2") {
{
... on I {
__typename
id
}
} =>
{
... on I {
__typename
... on A {
x {
__typename
id
}
}
... on B {
y {
__typename
id
}
}
}
}
},
},
},
}
`);
});
6 changes: 5 additions & 1 deletion query-planner-js/src/buildPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2612,8 +2612,12 @@ class FetchDependencyGraph {
}

private removeUselessGroups(group: FetchGroup) {
// Since we can potentially remove child in place, we need to
// explicitly copy the list of all children to ensure that we
// process ALL children
const children = group.children().concat();
// Recursing first, this makes it a bit easier to reason about.
for (const child of group.children()) {
for (const child of children) {
this.removeUselessGroups(child);
}

Expand Down