Skip to content

Commit 72f63b7

Browse files
committed
Split into separate PR
1 parent 50f5efa commit 72f63b7

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/validation/rules/NoFragmentCycles.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
3434
return {
3535
OperationDefinition: () => false,
3636
FragmentDefinition(node) {
37-
detectCycleRecursive(node);
37+
if (!visitedFrags[node.name.value]) {
38+
detectCycleRecursive(node);
39+
}
3840
return false;
3941
},
4042
};
@@ -43,10 +45,6 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
4345
// It does not terminate when a cycle was found but continues to explore
4446
// the graph to find all possible cycles.
4547
function detectCycleRecursive(fragment: FragmentDefinitionNode) {
46-
if (visitedFrags[fragment.name.value]) {
47-
return;
48-
}
49-
5048
const fragmentName = fragment.name.value;
5149
visitedFrags[fragmentName] = true;
5250

@@ -62,23 +60,24 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
6260
const spreadName = spreadNode.name.value;
6361
const cycleIndex = spreadPathIndexByName[spreadName];
6462

65-
spreadPath.push(spreadNode);
6663
if (cycleIndex === undefined) {
67-
const spreadFragment = context.getFragment(spreadName);
68-
if (spreadFragment) {
69-
detectCycleRecursive(spreadFragment);
64+
spreadPath.push(spreadNode);
65+
if (!visitedFrags[spreadName]) {
66+
const spreadFragment = context.getFragment(spreadName);
67+
if (spreadFragment) {
68+
detectCycleRecursive(spreadFragment);
69+
}
7070
}
71+
spreadPath.pop();
7172
} else {
7273
const cyclePath = spreadPath.slice(cycleIndex);
73-
const fragmentNames = cyclePath.slice(0, -1).map(s => s.name.value);
7474
context.reportError(
7575
new GraphQLError(
76-
cycleErrorMessage(spreadName, fragmentNames),
77-
cyclePath,
76+
cycleErrorMessage(spreadName, cyclePath.map(s => s.name.value)),
77+
cyclePath.concat(spreadNode),
7878
),
7979
);
8080
}
81-
spreadPath.pop();
8281
}
8382

8483
spreadPathIndexByName[fragmentName] = undefined;

0 commit comments

Comments
 (0)