Skip to content

Commit

Permalink
fix: ensure we are removing ALL useless groups (#3163)
Browse files Browse the repository at this point in the history
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.

<!-- FED-386 --->
  • Loading branch information
dariuszkuc authored Oct 10, 2024
1 parent cc45734 commit 7fe1465
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 1 deletion.
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

0 comments on commit 7fe1465

Please sign in to comment.