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

generateQueryFragments error with nested selection set #3043

Merged
merged 3 commits into from
Jun 20, 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
5 changes: 5 additions & 0 deletions .changeset/calm-candles-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/federation-internals": patch
---

generateQueryFragments() could generate fragments with naming collisions when nested
60 changes: 60 additions & 0 deletions internals-js/src/__tests__/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,66 @@ function astSSet(...selections: SelectionNode[]): SelectionSetNode {
};
}

describe('generate query fragments', () => {
test('generateQueryFragments handles repeated fragment spreads', () => {
const schema = parseSchema(`
type Query {
entities: [Entity]
}

union Entity = A | B

type A {
a: Int
}

type B {
b: Int
}
`);

const operation = parseOperation(schema, `
query {
entities {
... on B {
... on B {
... on B {
... on B {
b
}
}
}
}
}
}
`);

const withGeneratedFragments = operation.generateQueryFragments();
console.log(withGeneratedFragments.toString());
expect(withGeneratedFragments.toString()).toMatchString(`
fragment _generated_onB1_0 on B {
... on B {
b
}
}

fragment _generated_onB1_1 on B {
..._generated_onB1_0
}

fragment _generated_onB1_2 on B {
..._generated_onB1_1
}

{
entities {
..._generated_onB1_2
}
}
`);
});
});

describe('fragments optimization', () => {
// Takes a query with fragments as inputs, expand all those fragments, and ensures that all the
// fragments gets optimized back, and that we get back the exact same query.
Expand Down
7 changes: 4 additions & 3 deletions internals-js/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1594,16 +1594,17 @@ export class SelectionSet {
// No match, so we need to create a new fragment. First, we minimize the
// selection set before creating the fragment with it.
const [minimizedSelectionSet] = selection.selectionSet.minimizeSelectionSet(namedFragments, seenSelections);
const updatedEquivalentSelectionSetCandidates = seenSelections.get(mockHashCode); // may have changed after previous statement
const fragmentDefinition = new NamedFragmentDefinition(
this.parentType.schema(),
`_generated_${mockHashCode}_${equivalentSelectionSetCandidates?.length ?? 0}`,
`_generated_${mockHashCode}_${updatedEquivalentSelectionSetCandidates?.length ?? 0}`,
selection.element.typeCondition
).setSelectionSet(minimizedSelectionSet);
namedFragments.add(fragmentDefinition);

// Create a new "hash code" bucket or add to the existing one.
if (equivalentSelectionSetCandidates) {
equivalentSelectionSetCandidates.push([selection.selectionSet, fragmentDefinition]);
if (updatedEquivalentSelectionSetCandidates) {
updatedEquivalentSelectionSetCandidates.push([selection.selectionSet, fragmentDefinition]);
} else {
seenSelections.set(mockHashCode, [[selection.selectionSet, fragmentDefinition]]);
}
Expand Down