/
schema_validate.go
268 lines (233 loc) · 7.25 KB
/
schema_validate.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package validation
import (
"github.com/bucketd/go-graphqlparser/ast"
"github.com/bucketd/go-graphqlparser/graphql"
)
// ValidateSchema ...
func ValidateSchema(ctx *Context, schema *graphql.Schema) *graphql.Errors {
validateRootTypes(ctx, schema)
validateDirectives(ctx, schema)
validateTypes(ctx, schema)
if ctx.Errors.Len() > 0 {
return ctx.Errors
}
return nil
}
// validateRootTypes ...
func validateRootTypes(ctx *Context, schema *graphql.Schema) {
if schema.QueryType == nil {
ctx.AddError(graphql.NewError(
"Query root type must be provided.",
// TODO: Location.
))
} else {
// Query has a special case compared to Mutation and Subscription types in that the Query
// type may automatically be assigned if not SchemaDefinition is provided. In that case,
// it's possible for the Query type itself to not exist, despite QueryType being set on the
// schema itself, so we must also check that.
queryType, ok := schema.Types[schema.QueryType.NamedType]
if ok && !ast.IsObjectTypeDefinition(queryType) {
ctx.AddError(graphql.NewError(
"Query root type must be Object type.",
// TODO: Location.
))
} else if !ok {
ctx.AddError(graphql.NewError(
"Query root type must be provided.",
// TODO: Location.
))
}
}
if schema.MutationType != nil {
mutationType, ok := schema.Types[schema.MutationType.NamedType]
if ok && !ast.IsObjectTypeDefinition(mutationType) {
ctx.AddError(graphql.NewError(
"Mutation root type must be Object type if provided.",
// TODO: Location.
))
}
}
if schema.SubscriptionType != nil {
subscriptionType, ok := schema.Types[schema.SubscriptionType.NamedType]
if ok && !ast.IsObjectTypeDefinition(subscriptionType) {
ctx.AddError(graphql.NewError(
"Subscription root type must be Object type if provided.",
// TODO: Location.
))
}
}
}
// validateDirectives ...
func validateDirectives(ctx *Context, schema *graphql.Schema) {
for directiveName, directiveDef := range schema.Directives {
validateName(ctx, directiveName, 0, 0) // TODO: Location
argNames := make(map[string]struct{})
directiveDef.ArgumentsDefinition.ForEach(func(ivd ast.InputValueDefinition, i int) {
if _, ok := argNames[ivd.Name]; ok {
ctx.AddError(graphql.NewError(
"Argument @" + directiveName + "(" + ivd.Name + ":) can only be defined once.",
// TODO: Location.
))
return
}
validateName(ctx, ivd.Name, 0, 0) // TODO: Location
argNames[ivd.Name] = struct{}{}
})
}
}
// validateTypes ...
func validateTypes(ctx *Context, schema *graphql.Schema) {
for typeName, typeDef := range schema.Types {
// If the name exactly matches one of the built-in introspection type names, don't bother
// validating any further.
if !isIntrospectionTypeName(typeName) {
validateName(ctx, typeName, 0, 0) // TODO: Location
}
switch {
case ast.IsObjectTypeDefinition(typeDef):
validateFields(ctx, schema, typeDef)
validateObjectInterfaces(ctx, schema, typeDef)
case ast.IsInterfaceTypeDefinition(typeDef):
validateFields(ctx, schema, typeDef)
// TODO: ...
case ast.IsUnionTypeDefinition(typeDef):
// TODO: ...
case ast.IsEnumTypeDefinition(typeDef):
// TODO: ...
case ast.IsInputObjectTypeDefinition(typeDef):
// TODO: ...
}
}
}
// validateFields ...
func validateFields(ctx *Context, schema *graphql.Schema, typeDef *ast.TypeDefinition) {
if typeDef.FieldsDefinition.Len() == 0 {
ctx.AddError(graphql.NewError(
"Type " + typeDef.Name + " must define one or more fields.",
// TODO: Location.
))
}
typeDef.FieldsDefinition.ForEach(func(field ast.FieldDefinition, i int) {
validateName(ctx, field.Name, 0, 0) // TODO: Location.
if !IsOutputType(schema, field.Type) {
ctx.AddError(graphql.NewError(
"The type of " + typeDef.Name + "." + field.Name + " must be Output Type.",
// TODO: Location.
))
}
argNames := make(map[string]struct{}, field.ArgumentsDefinition.Len())
field.ArgumentsDefinition.ForEach(func(ivd ast.InputValueDefinition, i int) {
validateName(ctx, ivd.Name, 0, 0) // TODO: Location.
if _, ok := argNames[ivd.Name]; ok {
ctx.AddError(graphql.NewError(
"Field argument " + typeDef.Name + "." + field.Name + "(" + ivd.Name + ":) can only be defined " +
"once.",
// TODO: Location.
))
}
argNames[ivd.Name] = struct{}{}
if !IsInputType(schema, ivd.Type) {
ctx.AddError(graphql.NewError(
"The type of " + typeDef.Name + "." + field.Name + "(" + ivd.Name + ":) must be Input Type.",
// TODO: Location.
))
}
})
})
}
// validateObjectInterfaces ...
func validateObjectInterfaces(ctx *Context, schema *graphql.Schema, typeDef *ast.TypeDefinition) {
implementedTypeNames := make(map[string]struct{}, typeDef.ImplementsInterface.Len())
typeDef.ImplementsInterface.ForEach(func(t ast.Type, i int) {
if !IsInterfaceType(schema, t) {
ctx.AddError(graphql.NewError(
"Type " + typeDef.Name + " must only implement Interface types, it cannot " +
"implement " + t.NamedType,
// TODO: Location.
))
return
}
if _, ok := implementedTypeNames[t.NamedType]; ok {
ctx.AddError(graphql.NewError(
"Type " + typeDef.Name + " can only implement " + t.NamedType + " once.",
// TODO: Location.
))
return
}
implementedTypeNames[t.NamedType] = struct{}{}
// It's safe to assume this exists at this point, thanks to IsInterfaceType.
ifaceDef := schema.Types[t.NamedType]
validateObjectImplementsInterface(ctx, schema, typeDef, ifaceDef)
})
}
// validateObjectImplementsInterface ...
func validateObjectImplementsInterface(
ctx *Context,
schema *graphql.Schema,
typeDef *ast.TypeDefinition,
ifaceDef *ast.TypeDefinition,
) {
ifaceDef.FieldsDefinition.ForEach(func(ifaceField ast.FieldDefinition, i int) {
typeField, ok := typeDef.FieldDefinitionByName(ifaceField.Name)
if !ok {
ctx.AddError(graphql.NewError(
"Interface field " + ifaceDef.Name + "." + ifaceField.Name + " expected but " +
typeDef.Name + " does not provide it.",
// TODO: Location.
))
return
}
if !IsTypeSubTypeOf(schema, typeField.Type, ifaceField.Type) {
ctx.AddError(graphql.NewError(
"Interface field " + ifaceDef.Name + "." + ifaceField.Name + " expects type " +
ifaceField.Type.String() + " but " + typeDef.Name + "." + typeField.Name +
" is type " + typeField.Type.String(),
// TODO: Location.
))
}
})
}
// validateName ...
func validateName(ctx *Context, name string, line, col int) {
nameLen := len(name)
if nameLen == 0 {
ctx.AddError(InvalidNameError(name, line, col))
return
}
if len(name) > 1 && name[0] == '_' && name[1] == '_' {
ctx.AddError(ReservedNameError(name, line, col))
return
}
for i, r := range name {
switch {
case (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == '_':
case i > 0 && (r >= '0' && r <= '9'):
default:
ctx.AddError(InvalidNameError(name, line, col))
return
}
}
}
// isIntrospectionTypeName ...
func isIntrospectionTypeName(typeName string) bool {
switch typeName {
case "__Schema":
return true
case "__Directive":
return true
case "__DirectiveLocation":
return true
case "__Type":
return true
case "__Field":
return true
case "__InputValue":
return true
case "__EnumValue":
return true
case "__TypeKind":
return true
default:
return false
}
}