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 18f81bd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 39 deletions.
42 changes: 42 additions & 0 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,4 +1163,46 @@ 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": [

Check failure on line 1193 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote
{
"column": 9,

Check failure on line 1195 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote
"line": 3

Check failure on line 1196 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote
},
{
"column": 9,

Check failure on line 1199 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote
"line": 50008

Check failure on line 1200 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote
}
],
"message": "Fields \"type\" conflict because \"conflict\" and \"conflict2\" are different fields. Use different aliases on the fields to fetch both if this was intentional."

Check failure on line 1203 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote

Check failure on line 1203 in src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts

View workflow job for this annotation

GitHub Actions / ci / Lint source files

Strings must use singlequote
}
]
);
});
});
78 changes: 39 additions & 39 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 @@ -439,26 +439,26 @@ function findConflictsBetweenSubSelectionSets(
);
}

// (E) Then collect any conflicts between the provided collection of fields
// 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)) {
continue;
// (E) Then collect any conflicts between the provided collection of fields
// 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)) {
continue;
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap1,
referencedFragmentName,
discoveredFragments,
);
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap1,
fragmentName,
discoveredFragments,
);
}

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

// (E) Then collect any conflicts between the provided collection of fields
// 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)) {
continue;
// (E) Then collect any conflicts between the provided collection of fields
// 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)) {
continue;
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap2,
referencedFragmentName,
discoveredFragments,
);
}
const [fragmentName, referencedFragmentName] = item;
comparedFragmentPairs.add(referencedFragmentName, fragmentName, areMutuallyExclusive);
collectConflictsBetweenFieldsAndFragment(
context,
conflicts,
cachedFieldsAndFragmentNames,
comparedFragmentPairs,
areMutuallyExclusive,
fieldMap2,
fragmentName,
discoveredFragments,
);
}

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

0 comments on commit 18f81bd

Please sign in to comment.