Skip to content

Commit e1e2605

Browse files
authored
Fixes issue where there can be naming collisions in contextual parameter names (#3155)
Subgraph number set for a contextual paramter was colliding if used in multiple subgraphs
1 parent 8520c7d commit e1e2605

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

.changeset/tame-paws-return.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@apollo/query-planner": patch
3+
"@apollo/query-graphs": patch
4+
---
5+
6+
Fixes issue where contextual parameters can have naming collisions if used in multiple subgraphs

query-graphs-js/src/querygraph.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -934,17 +934,6 @@ function federateSubgraphs(supergraph: Schema, subgraphs: QueryGraph[]): QueryGr
934934
}
935935
}
936936

937-
for (const [subgraphName, args] of subgraphToArgs) {
938-
args.sort();
939-
const argToIndex = new Map();
940-
for (let idx=0; idx < args.length; idx++) {
941-
argToIndex.set(args[idx], `contextualArgument_${i}_${idx}`);
942-
}
943-
subgraphToArgIndices.set(subgraphName, argToIndex);
944-
}
945-
946-
builder.setContextMaps(subgraphToArgs, subgraphToArgIndices);
947-
948937
simpleTraversal(
949938
subgraph,
950939
_v => { return undefined; },
@@ -965,6 +954,22 @@ function federateSubgraphs(supergraph: Schema, subgraphs: QueryGraph[]): QueryGr
965954

966955
}
967956

957+
// add contextual argument maps to builder
958+
for (const [i, subgraph] of subgraphs.entries()) {
959+
const subgraphName = subgraph.name;
960+
const args = subgraphToArgs.get(subgraph.name);
961+
if (args) {
962+
args.sort();
963+
const argToIndex = new Map();
964+
for (let idx=0; idx < args.length; idx++) {
965+
argToIndex.set(args[idx], `contextualArgument_${i+1}_${idx}`);
966+
}
967+
subgraphToArgIndices.set(subgraphName, argToIndex);
968+
}
969+
}
970+
971+
builder.setContextMaps(subgraphToArgs, subgraphToArgIndices);
972+
968973
// Now we handle @provides
969974
let provideId = 0;
970975
for (const [i, subgraph] of subgraphs.entries()) {

query-planner-js/src/__tests__/buildPlan.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9164,7 +9164,7 @@ describe('@fromContext impacts on query planning', () => {
91649164
} =>
91659165
{
91669166
... on U {
9167-
field(a: $contextualArgument_1_0)
9167+
field(a: $contextualArgument_2_0)
91689168
}
91699169
}
91709170
},
@@ -9176,7 +9176,7 @@ describe('@fromContext impacts on query planning', () => {
91769176
{
91779177
kind: 'KeyRenamer',
91789178
path: ['..', '... on T', 'prop'],
9179-
renameKeyTo: 'contextualArgument_1_0',
9179+
renameKeyTo: 'contextualArgument_2_0',
91809180
},
91819181
]);
91829182
});

0 commit comments

Comments
 (0)