Skip to content

Commit 56deb05

Browse files
committed
fix bug for discovery
1 parent a23da10 commit 56deb05

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -1163,4 +1163,45 @@ 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+
locations: [
1193+
{
1194+
column: 9,
1195+
line: 3,
1196+
},
1197+
{
1198+
column: 9,
1199+
line: 50008,
1200+
},
1201+
],
1202+
message:
1203+
'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+
});
11661207
});

src/validation/rules/OverlappingFieldsCanBeMergedRule.ts

+21-7
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
}
@@ -443,19 +443,26 @@ function findConflictsBetweenSubSelectionSets(
443443
// and any fragment names found in the given fragment.
444444
while (discoveredFragments.length !== 0) {
445445
const item = discoveredFragments.pop();
446-
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
446+
if (
447+
!item ||
448+
comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)
449+
) {
447450
continue;
448451
}
449452
const [fragmentName, referencedFragmentName] = item;
450-
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
453+
comparedFragmentPairs.add(
454+
referencedFragmentName,
455+
fragmentName,
456+
areMutuallyExclusive,
457+
);
451458
collectConflictsBetweenFieldsAndFragment(
452459
context,
453460
conflicts,
454461
cachedFieldsAndFragmentNames,
455462
comparedFragmentPairs,
456463
areMutuallyExclusive,
457464
fieldMap1,
458-
fragmentName,
465+
referencedFragmentName,
459466
discoveredFragments,
460467
);
461468
}
@@ -479,19 +486,26 @@ function findConflictsBetweenSubSelectionSets(
479486
// and any fragment names found in the given fragment.
480487
while (discoveredFragments.length !== 0) {
481488
const item = discoveredFragments.pop();
482-
if (!item || comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)) {
489+
if (
490+
!item ||
491+
comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)
492+
) {
483493
continue;
484494
}
485495
const [fragmentName, referencedFragmentName] = item;
486-
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
496+
comparedFragmentPairs.add(
497+
referencedFragmentName,
498+
fragmentName,
499+
areMutuallyExclusive,
500+
);
487501
collectConflictsBetweenFieldsAndFragment(
488502
context,
489503
conflicts,
490504
cachedFieldsAndFragmentNames,
491505
comparedFragmentPairs,
492506
areMutuallyExclusive,
493507
fieldMap2,
494-
fragmentName,
508+
referencedFragmentName,
495509
discoveredFragments,
496510
);
497511
}

0 commit comments

Comments
 (0)