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