Skip to content

Commit

Permalink
fix bug for discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Jun 19, 2024
1 parent a23da10 commit 56deb05
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
41 changes: 41 additions & 0 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,4 +1163,45 @@ describe('Validate: Overlapping fields can be merged', () => {

expectErrors(query).toDeepEqual([]);
});

it('finds conflicts in nested fragments', () => {
const n = 10000;
const fragments = Array.from(Array(n).keys()).reduce(
(acc, next) =>
acc.concat(`\n
fragment X${next + 1} on Query {
...X${next}
}
`),
'',
);

const query = `
query Test {
type: conflict
...X${n}
}
${fragments}
fragment X0 on Query {
type: conflict2
__typename
}
`;
expectErrors(query).toDeepEqual([
{
locations: [
{
column: 9,
line: 3,
},
{
column: 9,
line: 50008,
},
],
message:
'Fields "type" conflict because "conflict" and "conflict2" are different fields. Use different aliases on the fields to fetch both if this was intentional.',
},
]);
});
});
28 changes: 21 additions & 7 deletions src/validation/rules/OverlappingFieldsCanBeMergedRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function findConflictsWithinSelectionSet(
comparedFragmentPairs,
false,
fieldMap,
fragmentName,
referencedFragmentName,
discoveredFragments,
);
}
Expand Down Expand Up @@ -443,19 +443,26 @@ function findConflictsBetweenSubSelectionSets(
// and any fragment names found in the given fragment.
while (discoveredFragments.length !== 0) {
const item = discoveredFragments.pop();
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
if (
!item ||
comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)
) {
continue;
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
comparedFragmentPairs.add(
referencedFragmentName,
fragmentName,
areMutuallyExclusive,
);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap1,
fragmentName,
referencedFragmentName,
discoveredFragments,
);
}
Expand All @@ -479,19 +486,26 @@ function findConflictsBetweenSubSelectionSets(
// and any fragment names found in the given fragment.
while (discoveredFragments.length !== 0) {
const item = discoveredFragments.pop();
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
if (
!item ||
comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)
) {
continue;
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
comparedFragmentPairs.add(
referencedFragmentName,
fragmentName,
areMutuallyExclusive,
);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap2,
fragmentName,
referencedFragmentName,
discoveredFragments,
);
}
Expand Down

0 comments on commit 56deb05

Please sign in to comment.