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

Prevent Infinite Loop in OverlappingFieldsCanBeMergedRule #3442

Merged
merged 5 commits into from Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Expand Up @@ -1021,6 +1021,49 @@ describe('Validate: Overlapping fields can be merged', () => {
`);
});

it('does not infinite loop on immediately recursive fragment mentioned in queries', () => {
expectValid(`
{
...fragA
}

fragment fragA on Query { ...fragA }
`);
});

it('does not infinite loop on recursive fragment with a field named after fragment', () => {
expectValid(`
{
...fragA
fragA
}

fragment fragA on Query { ...fragA }
`);
});

it('finds invalid cases even with field named after fragment', () => {
expectErrors(`
{
fragA
...fragA
}

fragment fragA on Type {
fragA: b
}
`).toDeepEqual([
{
message:
'Fields "fragA" conflict because "fragA" and "b" are different fields. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 8, column: 9 },
],
},
]);
});

it('does not infinite loop on transitively recursive fragment', () => {
expectValid(`
fragment fragA on Human { name, ...fragB }
Expand All @@ -1029,6 +1072,19 @@ describe('Validate: Overlapping fields can be merged', () => {
`);
});

it('does not infinite loop on transitively recursive fragment mentioned in queries', () => {
expectValid(`
{
...fragA
fragB
}

fragment fragA on Human { name, ...fragB }
fragment fragB on Human { name, ...fragC }
fragment fragC on Human { name, ...fragA }
`);
});

it('finds invalid case even with immediately recursive fragment', () => {
expectErrors(`
fragment sameAliasesWithDifferentFieldTargets on Dog {
Expand Down
16 changes: 16 additions & 0 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Expand Up @@ -267,6 +267,22 @@ function collectConflictsBetweenFieldsAndFragment(
// (E) Then collect any conflicts between the provided collection of fields
// and any fragment names found in the given fragment.
for (const referencedFragmentName of referencedFragmentNames) {
// Memoize so two fragments are not compared for conflicts more than once.
if (
comparedFragmentPairs.has(
referencedFragmentName,
fragmentName,
areMutuallyExclusive,
)
) {
continue;
}
comparedFragmentPairs.add(
referencedFragmentName,
fragmentName,
areMutuallyExclusive,
);

collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
Expand Down