@@ -34,7 +34,9 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
34
34
return {
35
35
OperationDefinition : ( ) => false ,
36
36
FragmentDefinition ( node ) {
37
- detectCycleRecursive ( node ) ;
37
+ if ( ! visitedFrags [ node . name . value ] ) {
38
+ detectCycleRecursive ( node ) ;
39
+ }
38
40
return false ;
39
41
} ,
40
42
} ;
@@ -43,10 +45,6 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
43
45
// It does not terminate when a cycle was found but continues to explore
44
46
// the graph to find all possible cycles.
45
47
function detectCycleRecursive ( fragment : FragmentDefinitionNode ) {
46
- if ( visitedFrags [ fragment . name . value ] ) {
47
- return ;
48
- }
49
-
50
48
const fragmentName = fragment . name . value ;
51
49
visitedFrags [ fragmentName ] = true ;
52
50
@@ -62,23 +60,24 @@ export function NoFragmentCycles(context: ValidationContext): ASTVisitor {
62
60
const spreadName = spreadNode . name . value ;
63
61
const cycleIndex = spreadPathIndexByName [ spreadName ] ;
64
62
65
- spreadPath . push ( spreadNode ) ;
66
63
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
+ }
70
70
}
71
+ spreadPath . pop ( ) ;
71
72
} else {
72
73
const cyclePath = spreadPath . slice ( cycleIndex ) ;
73
- const fragmentNames = cyclePath . slice ( 0 , - 1 ) . map ( s => s . name . value ) ;
74
74
context . reportError (
75
75
new GraphQLError (
76
- cycleErrorMessage ( spreadName , fragmentNames ) ,
77
- cyclePath ,
76
+ cycleErrorMessage ( spreadName , cyclePath . map ( s => s . name . value ) ) ,
77
+ cyclePath . concat ( spreadNode ) ,
78
78
) ,
79
79
) ;
80
80
}
81
- spreadPath . pop ( ) ;
82
81
}
83
82
84
83
spreadPathIndexByName [ fragmentName ] = undefined ;
0 commit comments