-
Notifications
You must be signed in to change notification settings - Fork 55
/
validate_test.go
727 lines (647 loc) · 17.2 KB
/
validate_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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
package bramble
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
)
type testSchema struct {
*testing.T
schema *ast.Schema
}
func withSchema(t *testing.T, schema string) *testSchema {
t.Helper()
return &testSchema{
T: t,
schema: gqlparser.MustLoadSchema(&ast.Source{Name: "fixture", Input: schema}),
}
}
func (t *testSchema) assertValid(f func(*ast.Schema) error) {
t.Helper()
assert.NoError(t.T, f(t.schema))
}
func (t *testSchema) assertInvalid(err string, f func(*ast.Schema) error) {
t.Helper()
e := f(t.schema)
assert.Error(t.T, e)
if e != nil {
assert.Equal(t.T, err, e.Error())
}
}
func TestSchemaIsValid(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT
interface Node {
id: ID!
}
type Service {
name: String!
version: String!
schema: String!
}
type Query {
node(id: ID!): Node
service: Service!
}
`).assertValid(ValidateSchema)
}
func TestBoundaryDirectiveRequirements(t *testing.T) {
// check @boundary directive matches requirements
t.Run("@boundary missing", func(t *testing.T) {
withSchema(t, `
type Filler {
other: String
}
`).assertInvalid("@boundary directive not found", validateBoundaryDirective)
})
t.Run("@boundary present", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT
`).assertValid(validateBoundaryDirective)
})
t.Run("@boundary on OBJECT", func(t *testing.T) {
withSchema(t, `
directive @boundary on FIELD
`).assertInvalid("@boundary directive should have location OBJECT", validateBoundaryDirective)
})
t.Run("@boundary has one location", func(t *testing.T) {
withSchema(t, `
directive @boundary on FIELD | OBJECT
`).assertInvalid("@boundary directive should have locations OBJECT | FIELD_DEFINITION", validateBoundaryDirective)
})
t.Run("@boundary has no arguments", func(t *testing.T) {
withSchema(t, `
directive @boundary(id: String) on OBJECT
`).assertInvalid("@boundary directive may not take arguments", validateBoundaryDirective)
})
// @boundary does not need to be present
t.Run("@boundary not required", func(t *testing.T) {
withSchema(t, `
type Filler {
other: String
}
`).assertValid(validateBoundaryObjects)
})
t.Run("@boundary is checked if it is used", func(t *testing.T) {
withSchema(t, `
directive @boundary(incorrect: String) on OBJECT
type Filler @boundary {
id: ID!
}
`).assertInvalid("@boundary directive may not take arguments", validateBoundaryObjects)
})
t.Run("@boundary is checked if it is used", func(t *testing.T) {
withSchema(t, `
directive @boundary(incorrect: String) on OBJECT
type Filler @boundary {
id: ID!
}
`).assertInvalid("@boundary directive may not take arguments", ValidateSchema)
})
}
func TestNamespaceDirectiveRequirements(t *testing.T) {
t.Run("valid namespaces", func(t *testing.T) {
withSchema(t, `
directive @namespace on OBJECT
type SubNamespace @namespace {
someField: String!
}
type RootNamespace @namespace {
sub: SubNamespace!
}
type Query {
root: RootNamespace!
}
`).assertValid(validateNamespaceObjects)
})
t.Run("invalid @namespace directive", func(t *testing.T) {
withSchema(t, `
directive @namespace(incorrect: String) on OBJECT
type NamespaceQuery @namespace {
someField: String!
}
`).assertInvalid("@namespace directive may not take arguments", ValidateSchema)
})
t.Run("nullable namespace field", func(t *testing.T) {
withSchema(t, `
directive @namespace on OBJECT
type NamespaceQuery @namespace {
someField: String!
}
type Query {
ns: NamespaceQuery
}
`).assertInvalid("namespace return type should be non nullable on Query.ns", ValidateSchema)
})
t.Run("invalid namespace ascendence", func(t *testing.T) {
withSchema(t, `
directive @namespace on OBJECT
type NamespaceQuery @namespace {
someField: String!
}
type SomeObject {
someField: String!
ns: NamespaceQuery!
}
type Query {
someObject: SomeObject!
}
`).assertInvalid(`type "NamespaceQuery" (namespace type) is used for field "ns" in non-namespace object "SomeObject"`, ValidateSchema)
})
}
func TestNodeInterface(t *testing.T) {
t.Run("Node interface missing", func(t *testing.T) {
withSchema(t, "").assertInvalid("the Node interface was not found", validateNodeInterface)
})
t.Run("Node is not interface", func(t *testing.T) {
withSchema(t, `
type Node {
id: ID!
}`).assertInvalid("the Node type must be an interface", validateNodeInterface)
})
t.Run("Node interface has extra fields", func(t *testing.T) {
withSchema(t, `
interface Node {
id: ID!
extra: String
}`).assertInvalid("the Node interface should have exactly one field", validateNodeInterface)
})
t.Run("Node interface has incorrect field", func(t *testing.T) {
withSchema(t, `
interface Node {
incorrect: String
}`).assertInvalid("the Node interface should have a field called 'id'", validateNodeInterface)
})
t.Run("Node interface has incorrect type", func(t *testing.T) {
withSchema(t, `
interface Node {
id: String
}`).assertInvalid("the Node interface should have a field called 'id' of type 'ID!'", validateNodeInterface)
})
t.Run("Node interface is correct", func(t *testing.T) {
withSchema(t, `
interface Node {
id: ID!
}`).assertValid(validateNodeInterface)
})
}
func TestNodeQuery(t *testing.T) {
t.Run("query type missing", func(t *testing.T) {
withSchema(t, "").assertInvalid("the schema is missing a Query type", validateNodeQuery)
})
t.Run("node query missing", func(t *testing.T) {
withSchema(t, `
type Query {
other: String
}
`).assertInvalid("the Query type is missing the 'node' field", validateNodeQuery)
})
t.Run("query with no arguments", func(t *testing.T) {
withSchema(t, `
type Query {
node: ID!
}
`).assertInvalid("the 'node' field of Query must take a single argument", validateNodeQuery)
})
t.Run("query with wrong argument name", func(t *testing.T) {
withSchema(t, `
type Query {
node(incorrect: ID!): ID!
}
`).assertInvalid("the 'node' field of Query must take a single argument called 'id'", validateNodeQuery)
})
t.Run("query with extra argument", func(t *testing.T) {
withSchema(t, `
type Query {
node(id: ID!, incorrect: String): ID!
}
`).assertInvalid("the 'node' field of Query must take a single argument", validateNodeQuery)
})
t.Run("query with wrong argument type", func(t *testing.T) {
withSchema(t, `
type Query {
node(id: String): ID!
}
`).assertInvalid("the 'node' field of Query must take a single argument of type 'ID!'", validateNodeQuery)
})
t.Run("query with wrong type", func(t *testing.T) {
withSchema(t, `
type Query {
node(id: ID!): ID!
}
`).assertInvalid("the 'node' field of Query must be of type 'Node'", validateNodeQuery)
})
t.Run("query is correct", func(t *testing.T) {
withSchema(t, `
interface Node {
id: ID!
}
type Query {
node(id: ID!): Node
}
`).assertValid(validateNodeQuery)
})
t.Run("Query is checked if @boundary is used", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT
type Query {
node(id: ID!): ID!
}
interface Node {
id: ID!
}
type Gizmo implements Node @boundary {
id: ID!
}`).assertInvalid("the 'node' field of Query must be of type 'Node'", validateBoundaryObjects)
})
}
func TestUnions(t *testing.T) {
t.Run("Unions are supported", func(t *testing.T) {
withSchema(t, `
type Dog { name: String! }
type Cat { name: String! }
type Snake { name: String! }
union Animal = Dog | Cat | Snake
directive @boundary on OBJECT
interface Node {
id: ID!
}
type Service {
name: String!
version: String!
schema: String!
}
type Query {
node(id: ID!): Node
service: Service!
}
`).assertValid(ValidateSchema)
})
}
func TestServiceObject(t *testing.T) {
t.Run("Service is required", func(t *testing.T) {
withSchema(t, "").assertInvalid("the Service object was not found", validateServiceObject)
})
t.Run("Service is an object", func(t *testing.T) {
withSchema(t, `
interface Service {
incorrect: String
}
`).assertInvalid("the Service type must be an object", validateServiceObject)
})
t.Run("Service has 3 fields", func(t *testing.T) {
withSchema(t, `
type Service {
incorrect: String
}
`).assertInvalid("the Service object should have exactly 3 fields", validateServiceObject)
})
t.Run("Service has correct 3 fields", func(t *testing.T) {
withSchema(t, `
type Service {
incorrect: String
other: String
wrong: String
}
`).assertInvalid("the Service object should not have a field called incorrect", validateServiceObject)
})
t.Run("Service has a name field", func(t *testing.T) {
withSchema(t, `
type Service {
name: String
version: String!
schema: String!
}
`).assertInvalid("the Service object should have a field called 'name' of type 'String!'", validateServiceObject)
})
t.Run("Service has a version field", func(t *testing.T) {
withSchema(t, `
type Service {
name: String!
version: String
schema: String!
}
`).assertInvalid("the Service object should have a field called 'version' of type 'String!'", validateServiceObject)
})
t.Run("Service has a schema field", func(t *testing.T) {
withSchema(t, `
type Service {
name: String!
version: String!
schema: String
}
`).assertInvalid("the Service object should have a field called 'schema' of type 'String!'", validateServiceObject)
})
t.Run("Service is correct", func(t *testing.T) {
withSchema(t, `
type Service {
name: String!
version: String!
schema: String!
}
`).assertValid(validateServiceObject)
})
t.Run("Service is checked", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT
interface Service {
name: String
}
type Query {
service: Service!
}
`).assertInvalid("the Service type must be an object", ValidateSchema)
})
}
func TestServiceQuery(t *testing.T) {
t.Run("query type missing", func(t *testing.T) {
withSchema(t, "").assertInvalid("the schema is missing a Query type", validateServiceQuery)
})
t.Run("service is required", func(t *testing.T) {
withSchema(t, `
type Query {
q: String
}
`).assertInvalid("the Query type is missing the 'service' field", validateServiceQuery)
})
t.Run("service takes no arguments", func(t *testing.T) {
withSchema(t, `
type Query {
service(name: String): String
}
`).assertInvalid("the 'service' field of Query must take no arguments", validateServiceQuery)
})
t.Run("service returns Service object", func(t *testing.T) {
withSchema(t, `
type Query {
service: String
}
`).assertInvalid("the 'service' field of Query must be of type 'Service!'", validateServiceQuery)
})
t.Run("service is correct", func(t *testing.T) {
withSchema(t, `
type Service {
name: String
}
type Query {
service: Service!
}
`).assertValid(validateServiceQuery)
})
t.Run("service is checked", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT
type Service {
name: String!
version: String!
schema: String!
}
type Query {
q: String
}
`).assertInvalid("the Query type is missing the 'service' field", ValidateSchema)
})
}
func TestRootObjectNaming(t *testing.T) {
t.Run("default schema definition is valid", func(t *testing.T) {
withSchema(t, `
type Gizmo {
id: ID!
name: String!
}
input GizmoInput {
id: ID!
name: String
}
type Query {
gizmo(someIdentifier: ID!): Gizmo
}
type Mutation {
updateGizmo(gizmo: GizmoInput!): Gizmo
}`).assertValid(validateRootObjectNames)
})
t.Run("overriding query is not valid", func(t *testing.T) {
withSchema(t, `
type Gizmo {
id: ID!
name: String!
}
schema {
query: QueryObj
}
type QueryObj {
gizmo(someIdentifier: ID!): Gizmo
}`).assertInvalid("the schema Query type can not be renamed to QueryObj", validateRootObjectNames)
})
t.Run("overriding mutation is not valid", func(t *testing.T) {
withSchema(t, `
type Gizmo {
id: ID!
name: String!
}
input GizmoInput {
id: ID!
name: String
}
schema {
mutation: MutObj
}
type MutObj {
updateGizmo(gizmo: GizmoInput!): Gizmo
}`).assertInvalid("the schema Mutation type can not be renamed to MutObj", validateRootObjectNames)
})
t.Run("overriding subscription is not valid", func(t *testing.T) {
withSchema(t, `
type Gizmo {
id: ID!
name: String!
}
schema {
subscription: SubObj
}
type SubObj {
gizmos: Gizmo
}`).assertInvalid("the schema Subscription type can not be renamed to SubObj", validateRootObjectNames)
})
}
func TestSchemaValidAfterMerge(t *testing.T) {
t.Run("invalid use of Servive type", func(t *testing.T) {
withSchema(t, `
type Service {
name: String!
version: String!
schema: String!
}
type Query {
service: Service!
}
type Mutation {
service: Service!
}`).assertInvalid("schema will become invalid after merge operation: merged schema:2: Undefined type Service.", validateSchemaValidAfterMerge)
})
t.Run("valid schema with empty Query type", func(t *testing.T) {
withSchema(t, `
type Service {
name: String!
version: String!
schema: String!
}
type Query {
service: Service!
}
type Mutation {
a: String!
}`).assertValid(validateSchemaValidAfterMerge)
})
}
func TestSchemaValidateBoundaryFields(t *testing.T) {
t.Run("valid boundary field", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Bar @boundary {
id: ID!
}
type Query {
foo(id: ID!): Foo @boundary
barGetter(id: ID!): Bar @boundary
}
`).assertValid(validateBoundaryFields)
})
t.Run("missing boundary field", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Bar @boundary {
id: ID!
}
type Query {
foo(id: ID!): Foo @boundary
}
`).assertInvalid("missing boundary fields for the following types: [Bar]", validateBoundaryFields)
})
t.Run("boundary field for non-boundary type", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo {
id: ID!
}
type Query {
foo(id: ID!): Foo @boundary
}
`).assertInvalid(`declared boundary query for non-boundary type "Foo"`, validateBoundaryFields)
})
t.Run("valid boundary fields", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Bar @boundary {
id: ID!
}
type Query {
foo(id: ID!): Foo @boundary
barGetter(id: ID!): Bar @boundary
}
`).assertValid(validateBoundaryFields)
})
t.Run("valid array boundary field", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Query {
foo(ids: [ID!]!): [Foo]! @boundary
}
`).assertValid(validateBoundaryFields)
})
t.Run("invalid array boundary query", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Bar @boundary {
id: ID!
}
type Query {
foo(ids: [ID!]): [Foo!] @boundary
}
`).assertInvalid(`invalid boundary query "foo": boundary list query must accept an argument of type "[ID!]!"`, validateBoundaryQueries)
})
t.Run("non-nullable boundary query result", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Bar @boundary {
id: ID!
}
type Query {
foo(id: ID!): Foo! @boundary
}
`).assertInvalid(`invalid boundary query "foo": return type of boundary query should be nullable`, validateBoundaryQueries)
})
t.Run("don't allow duplicated boundary getter", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Query {
foo(id: ID!): Foo @boundary
severalFoos(ids: [ID!]!): [Foo]! @boundary
}
`).assertInvalid(`declared duplicate query for boundary type "Foo"`, validateBoundaryFields)
})
t.Run("requires at least one argument", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Query {
foo: Foo @boundary
}
`).assertInvalid(`boundary field "foo" expects exactly one argument`, validateBoundaryFields)
})
t.Run("requires exactly one argument", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Query {
foo(id: ID!, scope: String): Foo @boundary
}
`).assertInvalid(`boundary field "foo" expects exactly one argument`, validateBoundaryFields)
})
}
func TestSchemaValidateBoundaryObjectsFormat(t *testing.T) {
t.Run("valid boundary objects", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
id: ID!
}
type Bar @boundary {
id: ID!
}
`).assertValid(validateBoundaryObjectsFormat)
})
t.Run("missing id field", func(t *testing.T) {
withSchema(t, `
directive @boundary on OBJECT | FIELD_DEFINITION
type Foo @boundary {
foo: String
}
`).assertInvalid(`missing "id: ID!" field in boundary type "Foo"`, validateBoundaryObjectsFormat)
})
}