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(query-planner): fixed missing referenced variables in the variableUsages field #3166

Merged
merged 3 commits into from
Oct 14, 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
9 changes: 9 additions & 0 deletions .changeset/stupid-geese-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@apollo/query-planner": patch
"@apollo/query-graphs": patch
"@apollo/federation-internals": patch
---

Fixed missing referenced variables in the `variableUsages` field of fetch operations

Query variables used in fetch operation should be listed in the `variableUsages` field. However, there was a bug where variables referenced by query-level directives could be missing in the field.
5 changes: 5 additions & 0 deletions internals-js/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,11 @@
}
}

collectVariables(collector: VariableCollector) {
this.selectionSet.collectVariables(collector);
this.collectVariablesInAppliedDirectives(collector);

Check warning on line 1198 in internals-js/src/operations.ts

View check run for this annotation

Codecov / codecov/patch

internals-js/src/operations.ts#L1196-L1198

Added lines #L1196 - L1198 were not covered by tests
}

toFragmentDefinitionNode() : FragmentDefinitionNode {
return {
kind: Kind.FRAGMENT_DEFINITION,
Expand Down
6 changes: 4 additions & 2 deletions query-planner-js/src/__tests__/buildPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8908,8 +8908,10 @@ describe('handles operations with directives', () => {
query testQuery__Subgraph1__0($some_var: String!) @withArgs(arg1: $some_var) {
test
}
`); // end of test
});
`);
// Make sure the `variableUsage` also captures the variable as well.
expect(fetch_nodes[0].variableUsages?.includes('some_var')).toBe(true);
}); // end of test
}); // end of `describe`

describe('@fromContext impacts on query planning', () => {
Expand Down
16 changes: 10 additions & 6 deletions query-planner-js/src/buildPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,22 +1600,26 @@ class FetchGroup {
);
}

// collect all used variables in the selection and in used Fragments
const usedVariables = new Set(selection.usedVariables().map(v => v.name));
// collect all used variables in the selection and the used directives and fragments.
const collector = new VariableCollector();
// Note: The operation's selectionSet includes `representations` variable,
// thus we use `selection` here instead.
selection.collectVariables(collector);
operation.collectVariablesInAppliedDirectives(collector);
if (operation.fragments) {
for (const namedFragment of operation.fragments.definitions()) {
namedFragment.selectionSet.usedVariables().forEach(v => {
usedVariables.add(v.name);
});
namedFragment.collectVariables(collector);
}
}
const usedVariables = collector.variables();

const operationDocument = operationToDocument(operation);
const fetchNode: FetchNode = {
kind: 'Fetch',
id: this.id,
serviceName: this.subgraphName,
requires: inputNodes ? trimSelectionNodes(inputNodes.selections) : undefined,
variableUsages: Array.from(usedVariables),
variableUsages: usedVariables.map((v) => v.name),
operation: stripIgnoredCharacters(print(operationDocument)),
operationKind: schemaRootKindToOperationKind(operation.rootKind),
operationName: operation.name,
Expand Down