forked from movio/bramble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
476 lines (420 loc) · 12.8 KB
/
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package bramble
import (
"fmt"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
)
// ValidateSchema validates that the schema respects the Bramble specs
func ValidateSchema(schema *ast.Schema) error {
if err := validateRootObjectNames(schema); err != nil {
return err
}
if err := validateBoundaryObjects(schema); err != nil {
return err
}
if err := validateNamespaceObjects(schema); err != nil {
return err
}
if err := validateServiceQuery(schema); err != nil {
return err
}
if err := validateServiceObject(schema); err != nil {
return err
}
if err := validateSchemaValidAfterMerge(schema); err != nil {
return err
}
return nil
}
func validateBoundaryObjects(schema *ast.Schema) error {
if !usesBoundaryDirective(schema) {
return nil
}
if err := validateBoundaryDirective(schema); err != nil {
return err
}
if err := validateBoundaryObjectsFormat(schema); err != nil {
return err
}
if usesFieldsBoundaryDirective(schema) {
if err := validateBoundaryQueries(schema); err != nil {
return err
}
// node compatibility
if !hasNodeQuery(schema) {
if err := validateBoundaryFields(schema); err != nil {
return err
}
}
} else {
if err := validateNodeInterface(schema); err != nil {
return err
}
if err := validateImplementsNode(schema); err != nil {
return err
}
}
if hasNodeQuery(schema) {
if err := validateNodeQuery(schema); err != nil {
return err
}
}
return nil
}
func validateNamespaceObjects(schema *ast.Schema) error {
if usesNamespaceDirective(schema) {
if err := validateNamespaceDirective(schema); err != nil {
return err
}
if err := validateNamespaceTypesAscendence(schema); err != nil {
return err
}
if err := validateNamespacesFields(schema, schema.Query, "Query"); err != nil {
return err
}
if err := validateNamespacesFields(schema, schema.Mutation, "Mutation"); err != nil {
return err
}
if err := validateNamespacesFields(schema, schema.Subscription, "Subscription"); err != nil {
return err
}
}
return nil
}
func validateServiceObject(schema *ast.Schema) error {
for _, t := range schema.Types {
if t.Name != serviceObjectName {
continue
}
if t.Kind != ast.Object {
return fmt.Errorf("the Service type must be an object")
}
if len(t.Fields) != 3 {
return fmt.Errorf("the Service object should have exactly 3 fields")
}
for _, field := range t.Fields {
switch field.Name {
case "name", "version", "schema":
if !isNonNullableTypeNamed(field.Type, "String") {
return fmt.Errorf("the Service object should have a field called '%s' of type 'String!'", field.Name)
}
default:
return fmt.Errorf("the Service object should not have a field called %s", field.Name)
}
}
return nil
}
return fmt.Errorf("the Service object was not found")
}
func validateServiceQuery(schema *ast.Schema) error {
if schema.Query == nil {
return fmt.Errorf("the schema is missing a Query type")
}
for _, f := range schema.Query.Fields {
if f.Name != serviceRootFieldName {
continue
}
if len(f.Arguments) != 0 {
return fmt.Errorf("the 'service' field of Query must take no arguments")
}
if !isNonNullableTypeNamed(f.Type, serviceObjectName) {
return fmt.Errorf("the 'service' field of Query must be of type 'Service!'")
}
return nil
}
return fmt.Errorf("the Query type is missing the 'service' field")
}
func validateNodeQuery(schema *ast.Schema) error {
if schema.Query == nil {
return fmt.Errorf("the schema is missing a Query type")
}
for _, f := range schema.Query.Fields {
if f.Name != nodeRootFieldName {
continue
}
if len(f.Arguments) != 1 {
return fmt.Errorf("the 'node' field of Query must take a single argument")
}
arg := f.Arguments[0]
if arg.Name != IdFieldName {
return fmt.Errorf("the 'node' field of Query must take a single argument called 'id'")
}
if !isIDType(arg.Type) {
return fmt.Errorf("the 'node' field of Query must take a single argument of type 'ID!'")
}
if !isNullableTypeNamed(f.Type, nodeInterfaceName) {
return fmt.Errorf("the 'node' field of Query must be of type 'Node'")
}
return nil
}
return fmt.Errorf("the Query type is missing the 'node' field")
}
func validateNodeInterface(schema *ast.Schema) error {
for _, t := range schema.Types {
if t.Name != nodeInterfaceName {
continue
}
if t.Kind != ast.Interface {
return fmt.Errorf("the Node type must be an interface")
}
if len(t.Fields) != 1 {
return fmt.Errorf("the Node interface should have exactly one field")
}
field := t.Fields[0]
if field.Name != IdFieldName {
return fmt.Errorf("the Node interface should have a field called 'id'")
}
if !isIDType(field.Type) {
return fmt.Errorf("the Node interface should have a field called 'id' of type 'ID!'")
}
return nil
}
return fmt.Errorf("the Node interface was not found")
}
func validateImplementsNode(schema *ast.Schema) error {
for _, t := range schema.Types {
if t.Kind != ast.Object {
continue
}
if t.Directives.ForName(boundaryDirectiveName) == nil {
continue
}
if implementsNode(schema, t) {
continue
}
return fmt.Errorf("object '%s' has the boundary directive but doesn't implement Node", t.Name)
}
return nil
}
func implementsNode(schema *ast.Schema, def *ast.Definition) bool {
for _, i := range schema.GetImplements(def) {
if i.Name == nodeInterfaceName {
return true
}
}
return false
}
func hasNodeQuery(schema *ast.Schema) bool {
return schema.Query.Fields.ForName(nodeRootFieldName) != nil
}
func usesNamespaceDirective(schema *ast.Schema) bool {
for _, t := range schema.Types {
if t.Kind != ast.Object {
continue
}
if t.Directives.ForName(namespaceDirectiveName) != nil {
return true
}
}
return false
}
func validateNamespaceDirective(schema *ast.Schema) error {
for _, d := range schema.Directives {
if d.Name != namespaceDirectiveName {
continue
}
if len(d.Arguments) != 0 {
return fmt.Errorf("@namespace directive may not take arguments")
}
if len(d.Locations) != 1 {
return fmt.Errorf("@namespace directive should have 1 location")
}
if d.Locations[0] != ast.LocationObject {
return fmt.Errorf("@namespace directive should have location OBJECT")
}
return nil
}
return fmt.Errorf("@namespace directive not found")
}
func validateNamespacesFields(schema *ast.Schema, currentType *ast.Definition, rootType string) error {
if currentType == nil {
return nil
}
for _, f := range currentType.Fields {
ft := schema.Types[f.Type.Name()]
if isNamespaceObject(ft) {
if !f.Type.NonNull {
return fmt.Errorf("namespace return type should be non nullable on %s.%s", currentType.Name, f.Name)
}
err := validateNamespacesFields(schema, ft, rootType)
if err != nil {
return err
}
}
}
return nil
}
// validateNamespaceTypesAscendence validates that namespace types are only used in other namespaces type or Query/Mutation/Subscription
func validateNamespaceTypesAscendence(schema *ast.Schema) error {
for _, t := range schema.Types {
if isNamespaceObject(t) || t.Name == queryObjectName || t.Name == mutationObjectName || t.Name == subscriptionObjectName {
continue
}
for _, f := range t.Fields {
ft := schema.Types[f.Type.Name()]
if isNamespaceObject(ft) {
return fmt.Errorf("type %q (namespace type) is used for field %q in non-namespace object %q", ft.Name, f.Name, t.Name)
}
}
}
return nil
}
func usesBoundaryDirective(schema *ast.Schema) bool {
for _, t := range schema.Types {
if t.Kind != ast.Object {
continue
}
if t.Directives.ForName(boundaryDirectiveName) != nil {
return true
}
}
return false
}
func validateBoundaryDirective(schema *ast.Schema) error {
for _, d := range schema.Directives {
if d.Name != boundaryDirectiveName {
continue
}
if len(d.Arguments) != 0 {
return fmt.Errorf("@boundary directive may not take arguments")
}
if len(d.Locations) == 1 {
// compatibility with existing @boundary directives
if d.Locations[0] != ast.LocationObject {
return fmt.Errorf("@boundary directive should have location OBJECT")
}
} else if len(d.Locations) == 2 {
if (d.Locations[0] != ast.LocationObject && d.Locations[0] != ast.LocationFieldDefinition) ||
(d.Locations[1] != ast.LocationObject && d.Locations[1] != ast.LocationFieldDefinition) ||
(d.Locations[0] == d.Locations[1]) {
return fmt.Errorf("@boundary directive should have locations OBJECT | FIELD_DEFINITION")
}
} else {
return fmt.Errorf("@boundary directive should have locations OBJECT | FIELD_DEFINITION")
}
return nil
}
return fmt.Errorf("@boundary directive not found")
}
func usesFieldsBoundaryDirective(schema *ast.Schema) bool {
d, ok := schema.Directives[boundaryDirectiveName]
if !ok {
return false
}
return len(d.Locations) == 2
}
// validateBoundaryFields checks that all boundary types have a getter and all getters are matching with a boundary type
func validateBoundaryFields(schema *ast.Schema) error {
boundaryTypes := make(map[string]bool)
for _, t := range schema.Types {
if t.Kind == ast.Object && isBoundaryObject(t) {
boundaryTypes[t.Name] = false
}
}
for _, f := range schema.Query.Fields {
if hasBoundaryDirective(f) {
hasBoundaryType, ok := boundaryTypes[f.Type.Name()]
if !ok {
return fmt.Errorf("declared boundary query for non-boundary type %q", f.Type.Name())
}
if hasBoundaryType {
return fmt.Errorf("declared duplicate query for boundary type %q", f.Type.Name())
}
if len(f.Arguments) != 1 {
return fmt.Errorf("boundary field %q expects exactly one argument", f.Name)
}
boundaryTypes[f.Type.Name()] = true
}
}
var missingBoundaryQueries []string
for k, hasBoundaryType := range boundaryTypes {
if !hasBoundaryType {
missingBoundaryQueries = append(missingBoundaryQueries, k)
}
}
if len(missingBoundaryQueries) > 0 {
return fmt.Errorf("missing boundary fields for the following types: %v", missingBoundaryQueries)
}
return nil
}
func validateBoundaryObjectsFormat(schema *ast.Schema) error {
for _, t := range schema.Types {
if t.Directives.ForName(boundaryDirectiveName) == nil {
continue
}
idField := t.Fields.ForName(IdFieldName)
if idField == nil {
return fmt.Errorf(`missing "%s: ID!" field in boundary type %q`, IdFieldName, t.Name)
}
if idField.Type.String() != "ID!" {
return fmt.Errorf(`%q field should have type "ID!" in boundary type %q`, IdFieldName, t.Name)
}
}
return nil
}
func validateBoundaryQueries(schema *ast.Schema) error {
for _, f := range schema.Query.Fields {
if hasBoundaryDirective(f) {
if err := validateBoundaryQuery(f); err != nil {
return fmt.Errorf("invalid boundary query %q: %w", f.Name, err)
}
}
}
return nil
}
func validateBoundaryQuery(f *ast.FieldDefinition) error {
if len(f.Arguments) != 1 {
return fmt.Errorf(`boundary query must have exactly one argument`)
}
if f.Arguments[0].Type.Elem != nil {
// array type check
if f.Arguments[0].Type.String() != "[ID!]!" {
return fmt.Errorf(`boundary list query must accept an argument of type "[ID!]!"`)
}
if !f.Type.NonNull || f.Type.Elem == nil {
return fmt.Errorf("return type should be a non-null array of nullable elements")
}
return nil
}
// regular type check
if f.Arguments[0].Type.String() != "ID!" {
return fmt.Errorf(`boundary query must accept an argument of type "ID!"`)
}
if f.Type.NonNull {
return fmt.Errorf("return type of boundary query should be nullable")
}
return nil
}
func validateRootObjectNames(schema *ast.Schema) error {
if q := schema.Query; q != nil && q.Name != queryObjectName {
return fmt.Errorf("the schema Query type can not be renamed to %s", q.Name)
}
if m := schema.Mutation; m != nil && m.Name != mutationObjectName {
return fmt.Errorf("the schema Mutation type can not be renamed to %s", m.Name)
}
if s := schema.Subscription; s != nil && s.Name != subscriptionObjectName {
return fmt.Errorf("the schema Subscription type can not be renamed to %s", s.Name)
}
return nil
}
// validateSchemaValidAfterMerge validates that the schema is still going to be
// valid once it gets merged with another schema and special types are removed.
// For example the Service type should not be used outside of the Query type.
func validateSchemaValidAfterMerge(schema *ast.Schema) error {
mergedSchema, err := MergeSchemas(schema)
if err != nil {
return fmt.Errorf("merge schema error: %w", err)
}
// If resulting Query type is empty, remove it from schema to avoid
// generating an invalid schema when formatting (empty Query type: `type Query {}`)
if len(filterBuiltinFields(mergedSchema.Query.Fields)) == 0 {
delete(mergedSchema.Types, "Query")
}
// format and reload the schema to ensure it is valid
res := formatSchema(mergedSchema)
_, gqlErr := gqlparser.LoadSchema(&ast.Source{Name: "merged schema", Input: res})
if gqlErr != nil {
return fmt.Errorf("schema will become invalid after merge operation: %w", gqlErr)
}
return nil
}