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

Fragment variable definitions erased in subgraph queries #3119

Merged
merged 7 commits into from
Aug 22, 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
6 changes: 6 additions & 0 deletions .changeset/poor-seahorses-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@apollo/query-planner": patch
"@apollo/federation-internals": patch
---

Fix issue where variable was not passed into subgraph when embedded in a fragment
45 changes: 41 additions & 4 deletions internals-js/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ import { assert, mapKeys, mapValues, MapWithCachedArrays, MultiMap, SetMultiMap
import { argumentsEquals, argumentsFromAST, isValidValue, valueToAST, valueToString } from "./values";
import { v1 as uuidv1 } from 'uuid';

export const DEFAULT_MIN_USAGES_TO_OPTIMIZE = 2;

function validate(condition: any, message: () => string, sourceAST?: ASTNode): asserts condition {
if (!condition) {
throw ERRORS.INVALID_GRAPHQL.err(message(), { nodes: sourceAST });
Expand Down Expand Up @@ -934,25 +936,55 @@ export class Operation extends DirectiveTargetElement<Operation> {
this.appliedDirectives,
);
}

private collectUndefinedVariablesFromFragments(fragments: NamedFragments): Variable[] {
const collector = new VariableCollector();
for (const namedFragment of fragments.definitions()) {
namedFragment.selectionSet.usedVariables().forEach(v => {
if (!this.variableDefinitions.definition(v)) {
collector.add(v);
}
});
}
return collector.variables();
}

// Returns a copy of this operation with the provided updated selection set and fragments.
private withUpdatedSelectionSetAndFragments(newSelectionSet: SelectionSet, newFragments: NamedFragments | undefined): Operation {
private withUpdatedSelectionSetAndFragments(
newSelectionSet: SelectionSet,
newFragments: NamedFragments | undefined,
allAvailableVariables?: VariableDefinitions,
): Operation {
if (this.selectionSet === newSelectionSet && newFragments === this.fragments) {
return this;
}

let newVariableDefinitions = this.variableDefinitions;
if (allAvailableVariables && newFragments) {
const undefinedVariables = this.collectUndefinedVariablesFromFragments(newFragments);
if (undefinedVariables.length > 0) {
newVariableDefinitions = new VariableDefinitions();
newVariableDefinitions.addAll(this.variableDefinitions);
newVariableDefinitions.addAll(allAvailableVariables.filter(undefinedVariables));
}
}

return new Operation(
this.schema(),
this.rootKind,
newSelectionSet,
this.variableDefinitions,
newVariableDefinitions,
newFragments,
this.name,
this.appliedDirectives,
);
}

optimize(fragments?: NamedFragments, minUsagesToOptimize: number = 2): Operation {
optimize(
fragments?: NamedFragments,
minUsagesToOptimize: number = DEFAULT_MIN_USAGES_TO_OPTIMIZE,
allAvailableVariables?: VariableDefinitions,
): Operation {
assert(minUsagesToOptimize >= 1, `Expected 'minUsagesToOptimize' to be at least 1, but got ${minUsagesToOptimize}`)
if (!fragments || fragments.isEmpty()) {
return this;
Expand Down Expand Up @@ -1001,11 +1033,16 @@ export class Operation extends DirectiveTargetElement<Operation> {
}
}

return this.withUpdatedSelectionSetAndFragments(optimizedSelection, finalFragments ?? undefined);
return this.withUpdatedSelectionSetAndFragments(
optimizedSelection,
finalFragments ?? undefined,
allAvailableVariables,
);
}

generateQueryFragments(): Operation {
const [minimizedSelectionSet, fragments] = this.selectionSet.minimizeSelectionSet();

return new Operation(
this.schema(),
this.rootKind,
Expand Down
20 changes: 17 additions & 3 deletions query-planner-js/src/buildPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
Variable,
VariableDefinition,
VariableDefinitions,
VariableCollector,
newDebugLogger,
selectionOfElement,
selectionSetOfElement,
Expand Down Expand Up @@ -64,6 +63,8 @@ import {
isInputType,
possibleRuntimeTypes,
NamedType,
VariableCollector,
DEFAULT_MIN_USAGES_TO_OPTIMIZE,
} from "@apollo/federation-internals";
import {
advanceSimultaneousPathsWithOperation,
Expand Down Expand Up @@ -1591,16 +1592,29 @@ class FetchGroup {
if (this.generateQueryFragments) {
operation = operation.generateQueryFragments();
} else {
operation = operation.optimize(fragments?.forSubgraph(this.subgraphName, subgraphSchema));
operation = operation.optimize(
fragments?.forSubgraph(this.subgraphName, subgraphSchema),
DEFAULT_MIN_USAGES_TO_OPTIMIZE,
variableDefinitions,
);
}

// collect all used variables in the selection and in used Fragments
const usedVariables = new Set(selection.usedVariables().map(v => v.name));
if (operation.fragments) {
for (const namedFragment of operation.fragments.definitions()) {
namedFragment.selectionSet.usedVariables().forEach(v => {
usedVariables.add(v.name);
});
}
}
const operationDocument = operationToDocument(operation);
const fetchNode: FetchNode = {
kind: 'Fetch',
id: this.id,
serviceName: this.subgraphName,
requires: inputNodes ? trimSelectionNodes(inputNodes.selections) : undefined,
variableUsages: selection.usedVariables().map(v => v.name),
variableUsages: Array.from(usedVariables),
operation: stripIgnoredCharacters(print(operationDocument)),
operationKind: schemaRootKindToOperationKind(operation.rootKind),
operationName: operation.name,
Expand Down