Skip to content

Commit

Permalink
Prevent Infinite Loop in OverlappingFieldsCanBeMergedRule (#3442)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Goncharov <ivan.goncharov.ua@gmail.com>
  • Loading branch information
nicolaslt and IvanGoncharov committed Jan 5, 2022
1 parent 29b811f commit 4175b26
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Expand Up @@ -1011,18 +1011,64 @@ describe('Validate: Overlapping fields can be merged', () => {

it('does not infinite loop on recursive fragment', () => {
expectValid(`
{
...fragA
}
fragment fragA on Human { name, relatives { name, ...fragA } }
`);
});

it('does not infinite loop on immediately recursive fragment', () => {
expectValid(`
{
...fragA
}
fragment fragA on Human { name, ...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(`
{
...fragA
fragB
}
fragment fragA on Human { name, ...fragB }
fragment fragB on Human { name, ...fragC }
fragment fragC on Human { name, ...fragA }
Expand Down
16 changes: 16 additions & 0 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Expand Up @@ -266,6 +266,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

0 comments on commit 4175b26

Please sign in to comment.