Skip to content

Commit 18f81bd

Browse files
committed
fix bug for discovery
1 parent a23da10 commit 18f81bd

File tree

2 files changed

+81
-39
lines changed

2 files changed

+81
-39
lines changed

src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,4 +1163,46 @@ describe('Validate: Overlapping fields can be merged', () => {
11631163

11641164
expectErrors(query).toDeepEqual([]);
11651165
});
1166+
1167+
it('finds conflicts in nested fragments', () => {
1168+
const n = 10000;
1169+
const fragments = Array.from(Array(n).keys()).reduce(
1170+
(acc, next) =>
1171+
acc.concat(`\n
1172+
fragment X${next + 1} on Query {
1173+
...X${next}
1174+
}
1175+
`),
1176+
'',
1177+
);
1178+
1179+
const query = `
1180+
query Test {
1181+
type: conflict
1182+
...X${n}
1183+
}
1184+
${fragments}
1185+
fragment X0 on Query {
1186+
type: conflict2
1187+
__typename
1188+
}
1189+
`;
1190+
expectErrors(query).toDeepEqual(
1191+
[
1192+
{
1193+
"locations": [
1194+
{
1195+
"column": 9,
1196+
"line": 3
1197+
},
1198+
{
1199+
"column": 9,
1200+
"line": 50008
1201+
}
1202+
],
1203+
"message": "Fields \"type\" conflict because \"conflict\" and \"conflict2\" are different fields. Use different aliases on the fields to fetch both if this was intentional."
1204+
}
1205+
]
1206+
);
1207+
});
11661208
});

src/validation/rules/OverlappingFieldsCanBeMergedRule.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function findConflictsWithinSelectionSet(
223223
comparedFragmentPairs,
224224
false,
225225
fieldMap,
226-
fragmentName,
226+
referencedFragmentName,
227227
discoveredFragments,
228228
);
229229
}
@@ -439,26 +439,26 @@ function findConflictsBetweenSubSelectionSets(
439439
);
440440
}
441441

442-
// (E) Then collect any conflicts between the provided collection of fields
443-
// and any fragment names found in the given fragment.
444-
while (discoveredFragments.length !== 0) {
445-
const item = discoveredFragments.pop();
446-
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
447-
continue;
442+
// (E) Then collect any conflicts between the provided collection of fields
443+
// and any fragment names found in the given fragment.
444+
while (discoveredFragments.length !== 0) {
445+
const item = discoveredFragments.pop();
446+
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
447+
continue;
448+
}
449+
const [fragmentName, referencedFragmentName] = item;
450+
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
451+
collectConflictsBetweenFieldsAndFragment(
452+
context,
453+
conflicts,
454+
cachedFieldsAndFragmentNames,
455+
comparedFragmentPairs,
456+
areMutuallyExclusive,
457+
fieldMap1,
458+
referencedFragmentName,
459+
discoveredFragments,
460+
);
448461
}
449-
const [fragmentName, referencedFragmentName] = item;
450-
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
451-
collectConflictsBetweenFieldsAndFragment(
452-
context,
453-
conflicts,
454-
cachedFieldsAndFragmentNames,
455-
comparedFragmentPairs,
456-
areMutuallyExclusive,
457-
fieldMap1,
458-
fragmentName,
459-
discoveredFragments,
460-
);
461-
}
462462

463463
// (I) Then collect conflicts between the second collection of fields and
464464
// those referenced by each fragment name associated with the first.
@@ -475,26 +475,26 @@ function findConflictsBetweenSubSelectionSets(
475475
);
476476
}
477477

478-
// (E) Then collect any conflicts between the provided collection of fields
479-
// and any fragment names found in the given fragment.
480-
while (discoveredFragments.length !== 0) {
481-
const item = discoveredFragments.pop();
482-
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
483-
continue;
478+
// (E) Then collect any conflicts between the provided collection of fields
479+
// and any fragment names found in the given fragment.
480+
while (discoveredFragments.length !== 0) {
481+
const item = discoveredFragments.pop();
482+
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
483+
continue;
484+
}
485+
const [fragmentName, referencedFragmentName] = item;
486+
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
487+
collectConflictsBetweenFieldsAndFragment(
488+
context,
489+
conflicts,
490+
cachedFieldsAndFragmentNames,
491+
comparedFragmentPairs,
492+
areMutuallyExclusive,
493+
fieldMap2,
494+
referencedFragmentName,
495+
discoveredFragments,
496+
);
484497
}
485-
const [fragmentName, referencedFragmentName] = item;
486-
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
487-
collectConflictsBetweenFieldsAndFragment(
488-
context,
489-
conflicts,
490-
cachedFieldsAndFragmentNames,
491-
comparedFragmentPairs,
492-
areMutuallyExclusive,
493-
fieldMap2,
494-
fragmentName,
495-
discoveredFragments,
496-
);
497-
}
498498

499499
// (J) Also collect conflicts between any fragment names by the first and
500500
// fragment names by the second. This compares each item in the first set of

0 commit comments

Comments
 (0)