forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_fields_on_correct_type_test.go
211 lines (206 loc) · 7.46 KB
/
rules_fields_on_correct_type_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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package graphql_test
import (
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/testutil"
)
func TestValidate_FieldsOnCorrectType_ObjectFieldSelection(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment objectFieldSelection on Dog {
__typename
name
}
`)
}
func TestValidate_FieldsOnCorrectType_AliasedObjectFieldSelection(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment aliasedObjectFieldSelection on Dog {
tn : __typename
otherName : name
}
`)
}
func TestValidate_FieldsOnCorrectType_InterfaceFieldSelection(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment interfaceFieldSelection on Pet {
__typename
name
}
`)
}
func TestValidate_FieldsOnCorrectType_AliasedInterfaceFieldSelection(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment interfaceFieldSelection on Pet {
otherName : name
}
`)
}
func TestValidate_FieldsOnCorrectType_LyingAliasSelection(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment lyingAliasSelection on Dog {
name : nickname
}
`)
}
func TestValidate_FieldsOnCorrectType_IgnoresFieldsOnUnknownType(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment unknownSelection on UnknownType {
unknownField
}
`)
}
func TestValidate_FieldsOnCorrectType_ReportErrorsWhenTheTypeIsKnownAgain(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment typeKnownAgain on Pet {
unknown_pet_field {
... on Cat {
unknown_cat_field
}
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "unknown_pet_field" on type "Pet".`, 3, 9),
testutil.RuleError(`Cannot query field "unknown_cat_field" on type "Cat".`, 5, 13),
})
}
func TestValidate_FieldsOnCorrectType_FieldNotDefinedOnFragment(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment fieldNotDefined on Dog {
meowVolume
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "meowVolume" on type "Dog".`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_IgnoreDeeplyUnknownField(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment deepFieldNotDefined on Dog {
unknown_field {
deeper_unknown_field
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "unknown_field" on type "Dog".`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_SubFieldNotDefined(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment subFieldNotDefined on Human {
pets {
unknown_field
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "unknown_field" on type "Pet".`, 4, 11),
})
}
func TestValidate_FieldsOnCorrectType_FieldNotDefinedOnInlineFragment(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment fieldNotDefined on Pet {
... on Dog {
meowVolume
}
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "meowVolume" on type "Dog".`, 4, 11),
})
}
func TestValidate_FieldsOnCorrectType_AliasedFieldTargetNotDefined(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment aliasedFieldTargetNotDefined on Dog {
volume : mooVolume
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "mooVolume" on type "Dog".`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_AliasedLyingFieldTargetNotDefined(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment aliasedLyingFieldTargetNotDefined on Dog {
barkVolume : kawVolume
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "kawVolume" on type "Dog".`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_NotDefinedOnInterface(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment notDefinedOnInterface on Pet {
tailLength
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "tailLength" on type "Pet".`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_DefinedOnImplementorsButNotOnInterface(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment definedOnImplementorsButNotInterface on Pet {
nickname
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "nickname" on type "Pet". However, this field exists on "Cat", "Dog". Perhaps you meant to use an inline fragment?`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_MetaFieldSelectionOnUnion(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment directFieldSelectionOnUnion on CatOrDog {
__typename
}
`)
}
func TestValidate_FieldsOnCorrectType_DirectFieldSelectionOnUnion(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment directFieldSelectionOnUnion on CatOrDog {
directField
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "directField" on type "CatOrDog".`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_DefinedImplementorsQueriedOnUnion(t *testing.T) {
testutil.ExpectFailsRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment definedOnImplementorsQueriedOnUnion on CatOrDog {
name
}
`, []gqlerrors.FormattedError{
testutil.RuleError(`Cannot query field "name" on type "CatOrDog". However, this field exists on "Being", "Pet", "Canine", "Cat", "Dog". Perhaps you meant to use an inline fragment?`, 3, 9),
})
}
func TestValidate_FieldsOnCorrectType_ValidFieldInInlineFragment(t *testing.T) {
testutil.ExpectPassesRule(t, graphql.FieldsOnCorrectTypeRule, `
fragment objectFieldSelection on Pet {
... on Dog {
name
}
... {
name
}
}
`)
}
func TestValidate_FieldsOnCorrectTypeErrorMessage_WorksWithNoSuggestions(t *testing.T) {
message := graphql.UndefinedFieldMessage("T", "f", []string{})
expected := `Cannot query field "T" on type "f".`
if message != expected {
t.Fatalf("Unexpected message, expected: %v, got %v", expected, message)
}
}
func TestValidate_FieldsOnCorrectTypeErrorMessage_WorksWithNoSmallNumbersOfSuggestions(t *testing.T) {
message := graphql.UndefinedFieldMessage("T", "f", []string{"A", "B"})
expected := `Cannot query field "T" on type "f". ` +
`However, this field exists on "A", "B". ` +
`Perhaps you meant to use an inline fragment?`
if message != expected {
t.Fatalf("Unexpected message, expected: %v, got %v", expected, message)
}
}
func TestValidate_FieldsOnCorrectTypeErrorMessage_WorksWithLotsOfSuggestions(t *testing.T) {
message := graphql.UndefinedFieldMessage("T", "f", []string{"A", "B", "C", "D", "E", "F"})
expected := `Cannot query field "T" on type "f". ` +
`However, this field exists on "A", "B", "C", "D", "E", and 1 other types. ` +
`Perhaps you meant to use an inline fragment?`
if message != expected {
t.Fatalf("Unexpected message, expected: %v, got %v", expected, message)
}
}