forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_no_fragment_cycles_test.go
178 lines (171 loc) · 6.22 KB
/
rules_no_fragment_cycles_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package graphql_test
import (
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/testutil"
)
func TestValidate_NoCircularFragmentSpreads_SingleReferenceIsValid(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB }
fragment fragB on Dog { name }
`)
}
func TestValidate_NoCircularFragmentSpreads_SpreadingTwiceIsNotCircular(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB, ...fragB }
fragment fragB on Dog { name }
`)
}
func TestValidate_NoCircularFragmentSpreads_SpreadingTwiceIndirectlyIsNotCircular(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB, ...fragC }
fragment fragB on Dog { ...fragC }
fragment fragC on Dog { name }
`)
}
func TestValidate_NoCircularFragmentSpreads_DoubleSpreadWithinAbstractTypes(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.NoFragmentCyclesRule, `
fragment nameFragment on Pet {
... on Dog { name }
... on Cat { name }
}
fragment spreadsInAnon on Pet {
... on Dog { ...nameFragment }
... on Cat { ...nameFragment }
}
`)
}
func TestValidate_NoCircularFragmentSpreads_DoesNotFalsePositiveOnUnknownFragment(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.NoFragmentCyclesRule, `
fragment nameFragment on Pet {
...UnknownFragment
}
`)
}
func TestValidate_NoCircularFragmentSpreads_SpreadingRecursivelyWithinFieldFails(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Human { relatives { ...fragA } },
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself.`, 2, 45),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDirectly(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragA }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself.`, 2, 31),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDirectlyWithinInlineFragment(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Pet {
... on Dog {
...fragA
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself.`, 4, 11),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfIndirectly(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB }
fragment fragB on Dog { ...fragA }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB.`, 2, 31, 3, 31),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfIndirectlyReportsOppositeOrder(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragB on Dog { ...fragA }
fragment fragA on Dog { ...fragB }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragB" within itself via fragA.`, 2, 31, 3, 31),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfIndirectlyWithinInlineFragment(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Pet {
... on Dog {
...fragB
}
}
fragment fragB on Pet {
... on Dog {
...fragA
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB.`, 4, 11, 9, 11),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDeeply(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB }
fragment fragB on Dog { ...fragC }
fragment fragC on Dog { ...fragO }
fragment fragX on Dog { ...fragY }
fragment fragY on Dog { ...fragZ }
fragment fragZ on Dog { ...fragO }
fragment fragO on Dog { ...fragP }
fragment fragP on Dog { ...fragA, ...fragX }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB, fragC, fragO, fragP.`,
2, 31,
3, 31,
4, 31,
8, 31,
9, 31),
testutil.RuleError(`Cannot spread fragment "fragO" within itself via fragP, fragX, fragY, fragZ.`,
8, 31,
9, 41,
5, 31,
6, 31,
7, 31),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDeeplyTwoPaths(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB, ...fragC }
fragment fragB on Dog { ...fragA }
fragment fragC on Dog { ...fragA }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB.`,
2, 31,
3, 31),
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragC.`,
2, 41,
4, 31),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDeeplyTwoPaths_AltTraverseOrder(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragC }
fragment fragB on Dog { ...fragC }
fragment fragC on Dog { ...fragA, ...fragB }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragC.`,
2, 31,
4, 31),
testutil.RuleError(`Cannot spread fragment "fragC" within itself via fragB.`,
4, 41,
3, 31),
})
}
func TestValidate_NoCircularFragmentSpreads_NoSpreadingItselfDeeplyAndImmediately(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.NoFragmentCyclesRule, `
fragment fragA on Dog { ...fragB }
fragment fragB on Dog { ...fragB, ...fragC }
fragment fragC on Dog { ...fragA, ...fragB }
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot spread fragment "fragB" within itself.`, 3, 31),
testutil.RuleError(`Cannot spread fragment "fragA" within itself via fragB, fragC.`,
2, 31,
3, 41,
4, 31),
testutil.RuleError(`Cannot spread fragment "fragB" within itself via fragC.`,
3, 41,
4, 41),
})
}