Skip to content

Commit 8c62789

Browse files
committed
Fix false positive validation error from fragment cycle when unknown fragment is used
1 parent b160c58 commit 8c62789

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/validation/__tests__/NoFragmentCycles.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ describe('Validate: No circular fragment spreads', () => {
5050
`);
5151
});
5252

53+
it('does not false positive on unknown fragment', () => {
54+
expectPassesRule(NoFragmentCycles, `
55+
fragment nameFragment on Pet {
56+
...UnknownFragment
57+
}
58+
`);
59+
});
60+
5361
it('spreading recursively within field fails', () => {
5462
expectFailsRule(NoFragmentCycles, `
5563
fragment fragA on Human { relatives { ...fragA } },

src/validation/rules/NoFragmentCycles.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,32 @@ export function NoFragmentCycles(context: ValidationContext): any {
5252
// the graph to find all possible cycles.
5353
function detectCycleRecursive(fragmentName) {
5454
var spreadNodes = spreadsInFragment[fragmentName];
55-
for (var i = 0; i < spreadNodes.length; ++i) {
56-
var spreadNode = spreadNodes[i];
57-
if (knownToLeadToCycle.has(spreadNode)) {
58-
continue;
59-
}
60-
if (spreadNode.name.value === initialName) {
61-
var cyclePath = spreadPath.concat(spreadNode);
62-
cyclePath.forEach(spread => knownToLeadToCycle.add(spread));
63-
errors.push(new GraphQLError(
64-
cycleErrorMessage(initialName, spreadPath.map(s => s.name.value)),
65-
cyclePath
66-
));
67-
continue;
68-
}
69-
if (spreadPath.some(spread => spread === spreadNode)) {
70-
continue;
71-
}
55+
if (spreadNodes) {
56+
for (var i = 0; i < spreadNodes.length; ++i) {
57+
var spreadNode = spreadNodes[i];
58+
if (knownToLeadToCycle.has(spreadNode)) {
59+
continue;
60+
}
61+
if (spreadNode.name.value === initialName) {
62+
var cyclePath = spreadPath.concat(spreadNode);
63+
cyclePath.forEach(spread => knownToLeadToCycle.add(spread));
64+
errors.push(new GraphQLError(
65+
cycleErrorMessage(
66+
initialName,
67+
spreadPath.map(s => s.name.value)
68+
),
69+
cyclePath
70+
));
71+
continue;
72+
}
73+
if (spreadPath.some(spread => spread === spreadNode)) {
74+
continue;
75+
}
7276

73-
spreadPath.push(spreadNode);
74-
detectCycleRecursive(spreadNode.name.value);
75-
spreadPath.pop();
77+
spreadPath.push(spreadNode);
78+
detectCycleRecursive(spreadNode.name.value);
79+
spreadPath.pop();
80+
}
7681
}
7782
}
7883

0 commit comments

Comments
 (0)