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

inefficient query plans when using @context #3140

Merged
merged 17 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions .changeset/fuzzy-readers-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@apollo/query-planner": patch
"@apollo/query-graphs": patch
---

Fixes edge case where contextual arguments can yield inefficient query plans. Also fixes naming of query plan arguments which can be a problem when using contextual variables in multiple subgraphs
27 changes: 16 additions & 11 deletions query-graphs-js/src/querygraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,17 +934,6 @@ function federateSubgraphs(supergraph: Schema, subgraphs: QueryGraph[]): QueryGr
}
}

for (const [subgraphName, args] of subgraphToArgs) {
args.sort();
const argToIndex = new Map();
for (let idx=0; idx < args.length; idx++) {
argToIndex.set(args[idx], `contextualArgument_${i}_${idx}`);
}
subgraphToArgIndices.set(subgraphName, argToIndex);
}

builder.setContextMaps(subgraphToArgs, subgraphToArgIndices);

simpleTraversal(
subgraph,
_v => { return undefined; },
Expand All @@ -965,6 +954,22 @@ function federateSubgraphs(supergraph: Schema, subgraphs: QueryGraph[]): QueryGr

}

// add contextual argument maps to builder
for (const [i, subgraph] of subgraphs.entries()) {
Copy link
Member

@dariuszkuc dariuszkuc Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously this was calculated in a single loop above -> any reason to move it down here? I think we only need the builder.setContextMaps(...) outside of the loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's like that because we only need to do the calculations once at the end. The fact that it was inside the loop was causing naming colisions.

const subgraphName = subgraph.name;
const args = subgraphToArgs.get(subgraph.name);
if (args) {
args.sort();
const argToIndex = new Map();
for (let idx=0; idx < args.length; idx++) {
argToIndex.set(args[idx], `contextualArgument_${i+1}_${idx}`);
}
subgraphToArgIndices.set(subgraphName, argToIndex);
}
}

builder.setContextMaps(subgraphToArgs, subgraphToArgIndices);

// Now we handle @provides
let provideId = 0;
for (const [i, subgraph] of subgraphs.entries()) {
Expand Down
Loading