Skip to content

Commit

Permalink
fix(query-planner): fixed missing referenced variables in the `variab…
Browse files Browse the repository at this point in the history
…le_usages` field

Related PR: #2986
  • Loading branch information
duckki committed Oct 12, 2024
1 parent 8334c2a commit 4c83b03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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 @@ export class NamedFragmentDefinition extends DirectiveTargetElement<NamedFragmen
}
}

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

toFragmentDefinitionNode() : FragmentDefinitionNode {
return {
kind: Kind.FRAGMENT_DEFINITION,
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

0 comments on commit 4c83b03

Please sign in to comment.