-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathtest_schema_parser.py
841 lines (754 loc) · 29.6 KB
/
test_schema_parser.py
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
from __future__ import annotations
import pickle
from copy import deepcopy
from textwrap import dedent
from typing import Optional, Tuple
import pytest
from graphql.error import GraphQLSyntaxError
from graphql.language import (
ArgumentNode,
BooleanValueNode,
DirectiveDefinitionNode,
DirectiveNode,
DocumentNode,
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
FieldDefinitionNode,
InputObjectTypeDefinitionNode,
InputValueDefinitionNode,
InterfaceTypeDefinitionNode,
InterfaceTypeExtensionNode,
ListTypeNode,
NamedTypeNode,
NameNode,
NonNullTypeNode,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
OperationType,
OperationTypeDefinitionNode,
ScalarTypeDefinitionNode,
SchemaDefinitionNode,
SchemaExtensionNode,
StringValueNode,
TypeNode,
UnionTypeDefinitionNode,
ValueNode,
parse,
)
from ..fixtures import kitchen_sink_sdl # noqa: F401
try:
from typing import TypeAlias
except ImportError: # Python < 3.10
from typing_extensions import TypeAlias
Location: TypeAlias = Optional[Tuple[int, int]]
def assert_syntax_error(text: str, message: str, location: Location) -> None:
with pytest.raises(GraphQLSyntaxError) as exc_info:
parse(text)
error = exc_info.value
assert error.message == f"Syntax Error: {message}"
assert error.description == message
assert error.locations == [location]
def assert_definitions(body: str, loc: Location, num=1):
doc = parse(body)
assert isinstance(doc, DocumentNode)
assert doc.loc == loc
definitions = doc.definitions
assert isinstance(definitions, tuple)
assert len(definitions) == num
return definitions[0] if num == 1 else definitions
def type_node(name: str, loc: Location):
return NamedTypeNode(name=name_node(name, loc), loc=loc)
def name_node(name: str, loc: Location):
return NameNode(value=name, loc=loc)
def field_node(name: NameNode, type_: TypeNode, loc: Location):
return field_node_with_args(name, type_, [], loc)
def field_node_with_args(name: NameNode, type_: TypeNode, args: list, loc: Location):
return FieldDefinitionNode(
name=name, arguments=args, type=type_, directives=[], loc=loc, description=None
)
def non_null_type(type_: TypeNode, loc: Location):
return NonNullTypeNode(type=type_, loc=loc)
def enum_value_node(name: str, loc: Location):
return EnumValueDefinitionNode(
name=name_node(name, loc), directives=[], loc=loc, description=None
)
def input_value_node(
name: NameNode, type_: TypeNode, default_value: ValueNode | None, loc: Location
):
return InputValueDefinitionNode(
name=name,
type=type_,
default_value=default_value,
directives=[],
loc=loc,
description=None,
)
def boolean_value_node(value: bool, loc: Location):
return BooleanValueNode(value=value, loc=loc)
def string_value_node(value: str, block: bool | None, loc: Location):
return StringValueNode(value=value, block=block, loc=loc)
def list_type_node(type_: TypeNode, loc: Location):
return ListTypeNode(type=type_, loc=loc)
def schema_extension_node(
directives: list[DirectiveNode],
operation_types: list[OperationTypeDefinitionNode],
loc: Location,
):
return SchemaExtensionNode(
directives=directives, operation_types=operation_types, loc=loc
)
def operation_type_definition(operation: OperationType, type_: TypeNode, loc: Location):
return OperationTypeDefinitionNode(operation=operation, type=type_, loc=loc)
def directive_node(name: NameNode, arguments: list[ArgumentNode], loc: Location):
return DirectiveNode(name=name, arguments=arguments, loc=loc)
def describe_schema_parser():
def simple_type():
body = dedent(
"""
type Hello {
world: String
}
"""
)
definition = assert_definitions(body, (0, 32))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("world", (16, 21)), type_node("String", (23, 29)), (16, 29)
),
)
assert definition.loc == (1, 31)
def parses_type_with_description_string():
body = dedent(
"""
"Description"
type Hello {
world: String
}
"""
)
definition = assert_definitions(body, (0, 46))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (20, 25))
description = definition.description
assert description == string_value_node("Description", False, (1, 14))
def parses_type_with_description_multi_line_string():
body = dedent(
'''
"""
Description
"""
# Even with comments between them
type Hello {
world: String
}'''
)
definition = assert_definitions(body, (0, 85))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (60, 65))
description = definition.description
assert description == string_value_node("Description", True, (1, 20))
def parses_schema_with_description_string():
body = dedent(
"""
"Description"
schema {
query: Foo
}
"""
)
definition = assert_definitions(body, (0, 39))
assert isinstance(definition, SchemaDefinitionNode)
description = definition.description
assert description == string_value_node("Description", False, (1, 14))
def description_followed_by_something_other_than_type_system_definition_throws():
assert_syntax_error('"Description" 1', "Unexpected Int '1'.", (1, 15))
def simple_extension():
body = dedent(
"""
extend type Hello {
world: String
}
"""
)
extension = assert_definitions(body, (0, 39))
assert isinstance(extension, ObjectTypeExtensionNode)
assert extension.name == name_node("Hello", (13, 18))
assert extension.interfaces == ()
assert extension.directives == ()
assert extension.fields == (
field_node(
name_node("world", (23, 28)), type_node("String", (30, 36)), (23, 36)
),
)
assert extension.loc == (1, 38)
def object_extension_without_fields():
body = "extend type Hello implements Greeting"
extension = assert_definitions(body, (0, 37))
assert isinstance(extension, ObjectTypeExtensionNode)
assert extension.name == name_node("Hello", (12, 17))
assert extension.interfaces == (type_node("Greeting", (29, 37)),)
assert extension.directives == ()
assert extension.fields == ()
assert extension.loc == (0, 37)
def interface_extension_without_fields():
body = "extend interface Hello implements Greeting"
extension = assert_definitions(body, (0, 42))
assert isinstance(extension, InterfaceTypeExtensionNode)
assert extension.name == name_node("Hello", (17, 22))
assert extension.interfaces == (type_node("Greeting", (34, 42)),)
assert extension.directives == ()
assert extension.fields == ()
assert extension.loc == (0, 42)
def object_extension_without_fields_followed_by_extension():
body = (
"\n extend type Hello implements Greeting\n\n"
" extend type Hello implements SecondGreeting\n "
)
extensions = assert_definitions(body, (0, 100), 2)
extension = extensions[0]
assert isinstance(extension, ObjectTypeExtensionNode)
assert extension.name == name_node("Hello", (19, 24))
assert extension.interfaces == (type_node("Greeting", (36, 44)),)
assert extension.directives == ()
assert extension.fields == ()
assert extension.loc == (7, 44)
extension = extensions[1]
assert isinstance(extension, ObjectTypeExtensionNode)
assert extension.name == name_node("Hello", (64, 69))
assert extension.interfaces == (type_node("SecondGreeting", (81, 95)),)
assert extension.directives == ()
assert extension.fields == ()
assert extension.loc == (52, 95)
def extension_without_anything_throws():
assert_syntax_error("extend scalar Hello", "Unexpected <EOF>.", (1, 20))
assert_syntax_error("extend type Hello", "Unexpected <EOF>.", (1, 18))
assert_syntax_error("extend interface Hello", "Unexpected <EOF>.", (1, 23))
assert_syntax_error("extend union Hello", "Unexpected <EOF>.", (1, 19))
assert_syntax_error("extend enum Hello", "Unexpected <EOF>.", (1, 18))
assert_syntax_error("extend input Hello", "Unexpected <EOF>.", (1, 19))
def interface_extension_without_fields_followed_by_extension():
body = (
"\n extend interface Hello implements Greeting\n\n"
" extend interface Hello implements SecondGreeting\n "
)
extensions = assert_definitions(body, (0, 110), 2)
extension = extensions[0]
assert isinstance(extension, InterfaceTypeExtensionNode)
assert extension.name == name_node("Hello", (24, 29))
assert extension.interfaces == (type_node("Greeting", (41, 49)),)
assert extension.directives == ()
assert extension.fields == ()
assert extension.loc == (7, 49)
extension = extensions[1]
assert isinstance(extension, InterfaceTypeExtensionNode)
assert extension.name == name_node("Hello", (74, 79))
assert extension.interfaces == (type_node("SecondGreeting", (91, 105)),)
assert extension.directives == ()
assert extension.fields == ()
assert extension.loc == (57, 105)
def object_extension_do_not_include_descriptions():
assert_syntax_error(
"""
"Description"
extend type Hello {
world: String
}""",
"Unexpected description,"
" descriptions are supported only on type definitions.",
(2, 13),
)
assert_syntax_error(
"""
extend "Description" type Hello {
world: String
}""",
"Unexpected String 'Description'.",
(2, 20),
)
def interface_extension_do_not_include_descriptions():
assert_syntax_error(
"""
"Description"
extend interface Hello {
world: String
}""",
"Unexpected description,"
" descriptions are supported only on type definitions.",
(2, 13),
)
assert_syntax_error(
"""
extend "Description" interface Hello {
world: String
}""",
"Unexpected String 'Description'.",
(2, 20),
)
def schema_extension():
body = """
extend schema {
mutation: Mutation
}"""
doc = parse(body)
assert isinstance(doc, DocumentNode)
assert doc.loc == (0, 75)
assert doc.definitions == (
schema_extension_node(
[],
[
operation_type_definition(
OperationType.MUTATION,
type_node("Mutation", (53, 61)),
(43, 61),
)
],
(13, 75),
),
)
def schema_extension_with_only_directives():
body = "extend schema @directive"
doc = parse(body)
assert isinstance(doc, DocumentNode)
assert doc.loc == (0, 24)
assert doc.definitions == (
schema_extension_node(
[directive_node(name_node("directive", (15, 24)), [], (14, 24))],
[],
(0, 24),
),
)
def schema_extension_without_anything_throws():
assert_syntax_error("extend schema", "Unexpected <EOF>.", (1, 14))
def schema_extension_with_invalid_operation_type_throws():
assert_syntax_error(
"extend schema { unknown: SomeType }", "Unexpected Name 'unknown'.", (1, 17)
)
def simple_non_null_type():
body = dedent(
"""
type Hello {
world: String!
}
"""
)
definition = assert_definitions(body, (0, 33))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("world", (16, 21)),
non_null_type(type_node("String", (23, 29)), (23, 30)),
(16, 30),
),
)
assert definition.loc == (1, 32)
def simple_interface_inheriting_interface():
body = "interface Hello implements World { field: String }"
definition = assert_definitions(body, (0, 50))
assert isinstance(definition, InterfaceTypeDefinitionNode)
assert definition.name == name_node("Hello", (10, 15))
assert definition.description is None
assert definition.interfaces == (type_node("World", (27, 32)),)
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("field", (35, 40)), type_node("String", (42, 48)), (35, 48)
),
)
assert definition.loc == (0, 50)
def simple_type_inheriting_interface():
body = "type Hello implements World { field: String }"
definition = assert_definitions(body, (0, 45))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (5, 10))
assert definition.description is None
assert definition.interfaces == (type_node("World", (22, 27)),)
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("field", (30, 35)), type_node("String", (37, 43)), (30, 43)
),
)
assert definition.loc == (0, 45)
def simple_type_inheriting_multiple_interfaces():
body = "type Hello implements Wo & rld { field: String }"
definition = assert_definitions(body, (0, 48))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (5, 10))
assert definition.description is None
assert definition.interfaces == (
type_node("Wo", (22, 24)),
type_node("rld", (27, 30)),
)
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("field", (33, 38)), type_node("String", (40, 46)), (33, 46)
),
)
assert definition.loc == (0, 48)
def simple_interface_inheriting_multiple_interfaces():
body = "interface Hello implements Wo & rld { field: String }"
definition = assert_definitions(body, (0, 53))
assert isinstance(definition, InterfaceTypeDefinitionNode)
assert definition.name == name_node("Hello", (10, 15))
assert definition.description is None
assert definition.interfaces == (
type_node("Wo", (27, 29)),
type_node("rld", (32, 35)),
)
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("field", (38, 43)), type_node("String", (45, 51)), (38, 51)
),
)
assert definition.loc == (0, 53)
def simple_type_inheriting_multiple_interfaces_with_leading_ampersand():
body = "type Hello implements & Wo & rld { field: String }"
definition = assert_definitions(body, (0, 50))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (5, 10))
assert definition.description is None
assert definition.interfaces == (
type_node("Wo", (24, 26)),
type_node("rld", (29, 32)),
)
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("field", (35, 40)), type_node("String", (42, 48)), (35, 48)
),
)
assert definition.loc == (0, 50)
def simple_interface_inheriting_multiple_interfaces_with_leading_ampersand():
body = "interface Hello implements & Wo & rld { field: String }"
definition = assert_definitions(body, (0, 55))
assert isinstance(definition, InterfaceTypeDefinitionNode)
assert definition.name == name_node("Hello", (10, 15))
assert definition.description is None
assert definition.interfaces == (
type_node("Wo", (29, 31)),
type_node("rld", (34, 37)),
)
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("field", (40, 45)), type_node("String", (47, 53)), (40, 53)
),
)
assert definition.loc == (0, 55)
def single_value_enum():
body = "enum Hello { WORLD }"
definition = assert_definitions(body, (0, 20))
assert isinstance(definition, EnumTypeDefinitionNode)
assert definition.name == name_node("Hello", (5, 10))
assert definition.description is None
assert definition.directives == ()
assert definition.values == (enum_value_node("WORLD", (13, 18)),)
assert definition.loc == (0, 20)
def double_value_enum():
body = "enum Hello { WO, RLD }"
definition = assert_definitions(body, (0, 22))
assert isinstance(definition, EnumTypeDefinitionNode)
assert definition.name == name_node("Hello", (5, 10))
assert definition.description is None
assert definition.directives == ()
assert definition.values == (
enum_value_node("WO", (13, 15)),
enum_value_node("RLD", (17, 20)),
)
assert definition.loc == (0, 22)
def simple_interface():
body = dedent(
"""
interface Hello {
world: String
}
"""
)
definition = assert_definitions(body, (0, 37))
assert isinstance(definition, InterfaceTypeDefinitionNode)
assert definition.name == name_node("Hello", (11, 16))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node(
name_node("world", (21, 26)), type_node("String", (28, 34)), (21, 34)
),
)
assert definition.loc == (1, 36)
def simple_field_with_arg():
body = dedent(
"""
type Hello {
world(flag: Boolean): String
}
"""
)
definition = assert_definitions(body, (0, 47))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node_with_args(
name_node("world", (16, 21)),
type_node("String", (38, 44)),
[
input_value_node(
name_node("flag", (22, 26)),
type_node("Boolean", (28, 35)),
None,
(22, 35),
)
],
(16, 44),
),
)
assert definition.loc == (1, 46)
def simple_field_with_arg_with_default_value():
body = dedent(
"""
type Hello {
world(flag: Boolean = true): String
}
"""
)
definition = assert_definitions(body, (0, 54))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node_with_args(
name_node("world", (16, 21)),
type_node("String", (45, 51)),
[
input_value_node(
name_node("flag", (22, 26)),
type_node("Boolean", (28, 35)),
boolean_value_node(True, (38, 42)),
(22, 42),
)
],
(16, 51),
),
)
assert definition.loc == (1, 53)
def simple_field_with_list_arg():
body = dedent(
"""
type Hello {
world(things: [String]): String
}
"""
)
definition = assert_definitions(body, (0, 50))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node_with_args(
name_node("world", (16, 21)),
type_node("String", (41, 47)),
[
input_value_node(
name_node("things", (22, 28)),
list_type_node(type_node("String", (31, 37)), (30, 38)),
None,
(22, 38),
)
],
(16, 47),
),
)
assert definition.loc == (1, 49)
def simple_field_with_two_args():
body = dedent(
"""
type Hello {
world(argOne: Boolean, argTwo: Int): String
}
"""
)
definition = assert_definitions(body, (0, 62))
assert isinstance(definition, ObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.interfaces == ()
assert definition.directives == ()
assert definition.fields == (
field_node_with_args(
name_node("world", (16, 21)),
type_node("String", (53, 59)),
[
input_value_node(
name_node("argOne", (22, 28)),
type_node("Boolean", (30, 37)),
None,
(22, 37),
),
input_value_node(
name_node("argTwo", (39, 45)),
type_node("Int", (47, 50)),
None,
(39, 50),
),
],
(16, 59),
),
)
assert definition.loc == (1, 61)
def simple_union():
body = "union Hello = World"
definition = assert_definitions(body, (0, 19))
assert isinstance(definition, UnionTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.directives == ()
assert definition.types == (type_node("World", (14, 19)),)
assert definition.loc == (0, 19)
def union_with_two_types():
body = "union Hello = Wo | Rld"
definition = assert_definitions(body, (0, 22))
assert isinstance(definition, UnionTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.description is None
assert definition.directives == ()
assert definition.types == (
type_node("Wo", (14, 16)),
type_node("Rld", (19, 22)),
)
assert definition.loc == (0, 22)
def union_with_two_types_and_leading_pipe():
body = "union Hello = | Wo | Rld"
definition = assert_definitions(body, (0, 24))
assert isinstance(definition, UnionTypeDefinitionNode)
assert definition.name == name_node("Hello", (6, 11))
assert definition.directives == ()
assert definition.types == (
type_node("Wo", (16, 18)),
type_node("Rld", (21, 24)),
)
assert definition.loc == (0, 24)
def union_fails_with_no_types():
assert_syntax_error("union Hello = |", "Expected Name, found <EOF>.", (1, 16))
def union_fails_with_leading_double_pipe():
assert_syntax_error(
"union Hello = || Wo | Rld", "Expected Name, found '|'.", (1, 16)
)
def union_fails_with_double_pipe():
assert_syntax_error(
"union Hello = Wo || Rld", "Expected Name, found '|'.", (1, 19)
)
def union_fails_with_trailing_pipe():
assert_syntax_error(
"union Hello = | Wo | Rld |", "Expected Name, found <EOF>.", (1, 27)
)
def scalar():
body = "scalar Hello"
definition = assert_definitions(body, (0, 12))
assert isinstance(definition, ScalarTypeDefinitionNode)
assert definition.name == name_node("Hello", (7, 12))
assert definition.description is None
assert definition.directives == ()
assert definition.loc == (0, 12)
def simple_input_object():
body = "\ninput Hello {\n world: String\n}"
definition = assert_definitions(body, (0, 32))
assert isinstance(definition, InputObjectTypeDefinitionNode)
assert definition.name == name_node("Hello", (7, 12))
assert definition.description is None
assert definition.directives == ()
assert definition.fields == (
input_value_node(
name_node("world", (17, 22)),
type_node("String", (24, 30)),
None,
(17, 30),
),
)
assert definition.loc == (1, 32)
def simple_input_object_with_args_should_fail():
assert_syntax_error(
"\ninput Hello {\n world(foo : Int): String\n}",
"Expected ':', found '('.",
(3, 8),
)
def directive_definition():
body = "directive @foo on OBJECT | INTERFACE"
definition = assert_definitions(body, (0, 36))
assert isinstance(definition, DirectiveDefinitionNode)
assert definition.name == name_node("foo", (11, 14))
assert definition.description is None
assert definition.arguments == ()
assert definition.repeatable is False
assert definition.locations == (
name_node("OBJECT", (18, 24)),
name_node("INTERFACE", (27, 36)),
)
def repeatable_directive_definition():
body = "directive @foo repeatable on OBJECT | INTERFACE"
definition = assert_definitions(body, (0, 47))
assert isinstance(definition, DirectiveDefinitionNode)
assert definition.name == name_node("foo", (11, 14))
assert definition.description is None
assert definition.arguments == ()
assert definition.repeatable is True
assert definition.locations == (
name_node("OBJECT", (29, 35)),
name_node("INTERFACE", (38, 47)),
)
def directive_with_incorrect_locations():
assert_syntax_error(
"\ndirective @foo on FIELD | INCORRECT_LOCATION",
"Unexpected Name 'INCORRECT_LOCATION'.",
(2, 27),
)
def parses_kitchen_sink_schema(kitchen_sink_sdl): # noqa: F811
assert parse(kitchen_sink_sdl)
def describe_deepcopy_and_pickle():
def can_deep_copy_ast(kitchen_sink_sdl): # noqa: F811
# create a schema AST from the kitchen sink SDL
doc = parse(kitchen_sink_sdl)
# make a deepcopy of the schema AST
copied_doc = deepcopy(doc)
# check that the copied AST is equal to the original one
assert copied_doc == doc
def can_pickle_and_unpickle_ast(kitchen_sink_sdl): # noqa: F811
# create a schema AST from the kitchen sink SDL
doc = parse(kitchen_sink_sdl)
# check that the schema AST can be pickled
# (particularly, there should be no recursion error)
dumped = pickle.dumps(doc)
# check that the pickle size is reasonable
assert len(dumped) < 50 * len(kitchen_sink_sdl)
loaded = pickle.loads(dumped)
# check that the un-pickled schema AST is still the same
assert loaded == doc
# check that pickling again creates the same result
dumped_again = pickle.dumps(doc)
assert dumped_again == dumped
def can_deep_copy_pickled_ast(kitchen_sink_sdl): # noqa: F811
# create a schema AST from the kitchen sink SDL
doc = parse(kitchen_sink_sdl)
# pickle and unpickle the schema AST
loaded_doc = pickle.loads(pickle.dumps(doc))
# make a deepcopy of this
copied_doc = deepcopy(loaded_doc)
# check that the result is still equal to the original schema AST
assert copied_doc == doc