Skip to content

Commit

Permalink
Split into separate PR
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jun 11, 2018
1 parent 50f5efa commit 72f63b7
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/validation/rules/NoFragmentCycles.js
Expand Up @@ -34,7 +34,9 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
return {
OperationDefinition: () => false,
FragmentDefinition(node) {
detectCycleRecursive(node);
if (!visitedFrags[node.name.value]) {
detectCycleRecursive(node);
}
return false;
},
};
Expand All @@ -43,10 +45,6 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
// It does not terminate when a cycle was found but continues to explore
// the graph to find all possible cycles.
function detectCycleRecursive(fragment: FragmentDefinitionNode) {
if (visitedFrags[fragment.name.value]) {
return;
}

const fragmentName = fragment.name.value;
visitedFrags[fragmentName] = true;

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

spreadPath.push(spreadNode);
if (cycleIndex === undefined) {
const spreadFragment = context.getFragment(spreadName);
if (spreadFragment) {
detectCycleRecursive(spreadFragment);
spreadPath.push(spreadNode);
if (!visitedFrags[spreadName]) {
const spreadFragment = context.getFragment(spreadName);
if (spreadFragment) {
detectCycleRecursive(spreadFragment);
}
}
spreadPath.pop();
} else {
const cyclePath = spreadPath.slice(cycleIndex);
const fragmentNames = cyclePath.slice(0, -1).map(s => s.name.value);
context.reportError(
new GraphQLError(
cycleErrorMessage(spreadName, fragmentNames),
cyclePath,
cycleErrorMessage(spreadName, cyclePath.map(s => s.name.value)),
cyclePath.concat(spreadNode),
),
);
}
spreadPath.pop();
}

spreadPathIndexByName[fragmentName] = undefined;
Expand Down

0 comments on commit 72f63b7

Please sign in to comment.