-
-
Notifications
You must be signed in to change notification settings - Fork 132
/
test_definition.py
1904 lines (1685 loc) · 77.8 KB
/
test_definition.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from enum import Enum
from math import isnan, nan
from typing import cast, Dict
from pytest import mark, raises
from graphql.error import GraphQLError
from graphql.language import (
parse_value,
EnumTypeDefinitionNode,
EnumTypeExtensionNode,
EnumValueNode,
Node,
InputObjectTypeDefinitionNode,
InputObjectTypeExtensionNode,
InputValueDefinitionNode,
InterfaceTypeDefinitionNode,
InterfaceTypeExtensionNode,
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
ScalarTypeDefinitionNode,
ScalarTypeExtensionNode,
StringValueNode,
TypeDefinitionNode,
TypeExtensionNode,
ValueNode,
UnionTypeDefinitionNode,
UnionTypeExtensionNode,
)
from graphql.pyutils import Undefined
from graphql.type import (
GraphQLArgument,
GraphQLEnumValue,
GraphQLEnumType,
GraphQLField,
GraphQLInputField,
GraphQLInputObjectType,
GraphQLInt,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLScalarType,
GraphQLString,
GraphQLUnionType,
)
ScalarType = GraphQLScalarType("Scalar")
ObjectType = GraphQLObjectType("Object", {})
InterfaceType = GraphQLInterfaceType("Interface", {})
UnionType = GraphQLUnionType(
"Union",
[ObjectType],
resolve_type=lambda _obj, _info, _type: None, # pragma: no cover
)
EnumType = GraphQLEnumType("Enum", {"foo": GraphQLEnumValue()})
InputObjectType = GraphQLInputObjectType("InputObject", {})
ListOfScalarsType = GraphQLList(ScalarType)
NonNullScalarType = GraphQLNonNull(ScalarType)
ListOfNonNullScalarsType = GraphQLList(NonNullScalarType)
NonNullListOfScalars = GraphQLNonNull(ListOfScalarsType)
def describe_type_system_scalars():
def defines_a_scalar_type():
scalar = GraphQLScalarType("SomeScalar")
assert scalar.name == "SomeScalar"
kwargs = scalar.to_kwargs()
assert kwargs == {
"name": "SomeScalar",
"description": None,
"specified_by_url": None,
"serialize": None,
"parse_value": None,
"parse_literal": None,
"extensions": {},
"ast_node": None,
"extension_ast_nodes": (),
}
def accepts_a_scalar_type_defining_serialize():
def serialize(value):
pass
scalar = GraphQLScalarType("SomeScalar", serialize)
assert scalar.serialize is serialize
assert scalar.to_kwargs()["serialize"] is serialize
def defines_a_scalar_type_with_a_description():
description = "nice scalar"
scalar = GraphQLScalarType("SomeScalar", description=description)
assert scalar.description is description
assert scalar.to_kwargs()["description"] is description
def accepts_a_scalar_type_defining_specified_by_url():
url = "https://example.com/foo_spec"
scalar = GraphQLScalarType("SomeScalar", specified_by_url=url)
assert scalar.specified_by_url == url
assert scalar.to_kwargs()["specified_by_url"] == url
def accepts_a_scalar_type_defining_parse_value_and_parse_literal():
def parse_value(_value):
pass
def parse_literal(_value_node, _variables):
pass
scalar = GraphQLScalarType(
"SomeScalar", parse_value=parse_value, parse_literal=parse_literal
)
assert scalar.parse_value is parse_value
assert scalar.parse_literal is parse_literal
kwargs = scalar.to_kwargs()
assert kwargs["parse_value"] is parse_value
assert kwargs["parse_literal"] is parse_literal
def provides_default_methods_if_omitted():
scalar = GraphQLScalarType("Foo")
assert scalar.serialize is GraphQLScalarType.serialize
assert scalar.parse_value is GraphQLScalarType.parse_value
assert (
scalar.parse_literal.__func__ # type: ignore
is GraphQLScalarType.parse_literal
)
kwargs = scalar.to_kwargs()
assert kwargs["serialize"] is None
assert kwargs["parse_value"] is None
assert kwargs["parse_literal"] is None
def use_parse_value_for_parsing_literals_if_parse_literal_omitted():
scalar = GraphQLScalarType(
"Foo", parse_value=lambda value: f"parse_value: {value!r}"
)
assert scalar.parse_literal(parse_value("null")) == "parse_value: None"
assert (
scalar.parse_literal(parse_value('{foo: "bar"}'))
== "parse_value: {'foo': 'bar'}"
)
assert (
scalar.parse_literal(parse_value("{foo: { bar: $var } }"), {"var": "baz"})
== "parse_value: {'foo': {'bar': 'baz'}}"
)
def accepts_a_scalar_type_with_ast_node_and_extension_ast_nodes():
ast_node = ScalarTypeDefinitionNode()
extension_ast_nodes = [ScalarTypeExtensionNode()]
scalar = GraphQLScalarType(
"SomeScalar", ast_node=ast_node, extension_ast_nodes=extension_ast_nodes
)
assert scalar.ast_node is ast_node
assert scalar.extension_ast_nodes == tuple(extension_ast_nodes)
def rejects_a_scalar_type_with_incorrectly_typed_name():
with raises(TypeError, match="missing .* required .* 'name'"):
# noinspection PyArgumentList
GraphQLScalarType() # type: ignore
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType(None) # type: ignore
assert str(exc_info.value) == "Must provide name."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType(42, {}) # type: ignore
assert str(exc_info.value) == "Expected name to be a string."
def rejects_a_scalar_type_with_invalid_name():
with raises(GraphQLError) as exc_info:
GraphQLScalarType("")
assert str(exc_info.value) == "Expected name to be a non-empty string."
with raises(GraphQLError) as exc_info:
GraphQLScalarType("bad-name")
assert str(exc_info.value) == (
"Names must only contain [_a-zA-Z0-9] but 'bad-name' does not."
)
def rejects_a_scalar_type_with_incorrectly_typed_description():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType("SomeScalar", description=[]) # type: ignore
assert str(exc_info.value) == "The description must be a string."
def rejects_a_scalar_type_defining_specified_by_url_with_an_incorrect_type():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType("SomeScalar", specified_by_url={}) # type: ignore
assert (
str(exc_info.value)
== "SomeScalar must provide 'specified_by_url' as a string, but got: {}."
)
def rejects_a_scalar_type_defining_serialize_with_incorrect_type():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType("SomeScalar", {}) # type: ignore
assert str(exc_info.value) == (
"SomeScalar must provide 'serialize' as a function."
" If this custom Scalar is also used as an input type,"
" ensure 'parse_value' and 'parse_literal' functions"
" are also provided."
)
def rejects_a_scalar_type_defining_parse_literal_but_not_parse_value():
def parse_literal(_node: ValueNode, _vars=None):
return Undefined # pragma: no cover
with raises(TypeError) as exc_info:
GraphQLScalarType("SomeScalar", parse_literal=parse_literal)
assert str(exc_info.value) == (
"SomeScalar must provide both"
" 'parse_value' and 'parse_literal' as functions."
)
def rejects_a_scalar_type_incorrectly_defining_parse_literal_and_value():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType(
"SomeScalar", parse_value={}, parse_literal={} # type: ignore
)
assert str(exc_info.value) == (
"SomeScalar must provide both"
" 'parse_value' and 'parse_literal' as functions."
)
def rejects_a_scalar_type_with_an_incorrect_ast_node():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType("SomeScalar", ast_node=Node()) # type: ignore
msg = str(exc_info.value)
assert msg == "SomeScalar AST node must be a TypeDefinitionNode."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType(
"SomeScalar", ast_node=TypeDefinitionNode() # type: ignore
)
msg = str(exc_info.value)
assert msg == "SomeScalar AST node must be a ScalarTypeDefinitionNode."
def rejects_a_scalar_type_with_incorrect_extension_ast_nodes():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType(
"SomeScalar", extension_ast_nodes=[Node()] # type: ignore
)
assert str(exc_info.value) == (
"SomeScalar extension AST nodes must be specified"
" as a collection of TypeExtensionNode instances."
)
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLScalarType(
"SomeScalar", extension_ast_nodes=[TypeExtensionNode()] # type: ignore
)
assert str(exc_info.value) == (
"SomeScalar extension AST nodes must be specified"
" as a collection of ScalarTypeExtensionNode instances."
)
def describe_type_system_fields():
def defines_a_field():
field = GraphQLField(GraphQLString)
assert field.type is GraphQLString
kwargs = field.to_kwargs()
assert kwargs == {
"type_": GraphQLString,
"args": None,
"resolve": None,
"subscribe": None,
"description": None,
"deprecation_reason": None,
"extensions": {},
"ast_node": None,
}
def defines_a_field_with_args():
arg = GraphQLArgument(GraphQLInt)
field = GraphQLField(GraphQLString, {"arg": arg})
assert isinstance(field.args, dict)
assert list(field.args) == ["arg"]
assert field.args["arg"] is arg
assert field.to_kwargs()["args"] == {"arg": arg}
def defines_a_field_with_input_types_as_args():
field = GraphQLField(GraphQLString, {"arg": GraphQLString}) # type: ignore
assert isinstance(field.args, dict)
assert list(field.args) == ["arg"]
arg = field.args["arg"]
assert isinstance(arg, GraphQLArgument)
assert arg.type is GraphQLString
def defines_a_scalar_type_with_a_description():
description = "nice field"
field = GraphQLField(GraphQLString, description=description)
assert field.description is description
assert field.to_kwargs()["description"] is description
def defines_a_scalar_type_with_a_deprecation_reason():
deprecation_reason = "field is redundant"
field = GraphQLField(GraphQLString, deprecation_reason=deprecation_reason)
assert field.deprecation_reason is deprecation_reason
assert field.to_kwargs()["deprecation_reason"] is deprecation_reason
def rejects_a_field_with_incorrect_type():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLField(InputObjectType) # type: ignore
assert str(exc_info.value) == "Field type must be an output type."
def rejects_a_field_with_incorrect_args():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLField(GraphQLString, args=[]) # type: ignore
assert str(exc_info.value) == (
"Field args must be a dict with argument names as keys."
)
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLField(GraphQLString, args={"arg": GraphQLObjectType}) # type: ignore
assert str(exc_info.value) == (
"Field args must be GraphQLArguments or input type objects."
)
def rejects_a_field_with_an_incorrectly_typed_description():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLField(GraphQLString, description=[]) # type: ignore
assert str(exc_info.value) == "The description must be a string."
def rejects_a_field_with_an_incorrectly_typed_deprecation_reason():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLField(GraphQLString, deprecation_reason=[]) # type: ignore
assert str(exc_info.value) == "The deprecation reason must be a string."
def rejects_a_field_with_an_incorrect_ast_node():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLField(GraphQLString, ast_node=Node()) # type: ignore
assert str(exc_info.value) == "Field AST node must be a FieldDefinitionNode."
def describe_type_system_objects():
def defines_an_object_type():
fields = {"f": GraphQLField(ScalarType)}
interfaces = (InterfaceType,)
type_ = GraphQLObjectType("AnotherObjectType", fields, interfaces)
assert type_.name == "AnotherObjectType"
assert type_.fields == fields
assert type_.fields is not fields
assert type_.interfaces == interfaces
assert type_.interfaces is interfaces
assert type_.extensions == {}
kwargs = type_.to_kwargs()
assert kwargs == {
"name": "AnotherObjectType",
"description": None,
"fields": fields,
"interfaces": interfaces,
"is_type_of": None,
"extensions": {},
"ast_node": None,
"extension_ast_nodes": (),
}
def does_not_mutate_passed_field_definitions():
output_fields = {
"field1": GraphQLField(ScalarType),
"field2": GraphQLField(
ScalarType, args={"id": GraphQLArgument(ScalarType)}
),
}
test_object_1 = GraphQLObjectType("Test1", output_fields)
test_object_2 = GraphQLObjectType("Test2", output_fields)
assert test_object_1.fields == test_object_2.fields
assert output_fields == {
"field1": GraphQLField(ScalarType),
"field2": GraphQLField(
ScalarType, args={"id": GraphQLArgument(ScalarType)}
),
}
input_fields = {
"field1": GraphQLInputField(ScalarType),
"field2": GraphQLInputField(ScalarType),
}
test_input_object_1 = GraphQLInputObjectType("Test1", input_fields)
test_input_object_2 = GraphQLInputObjectType("Test2", input_fields)
assert test_input_object_1.fields == test_input_object_2.fields
assert input_fields == {
"field1": GraphQLInputField(ScalarType),
"field2": GraphQLInputField(ScalarType),
}
def defines_an_object_type_with_deprecated_field():
TypeWithDeprecatedField = GraphQLObjectType(
"foo",
{
"bar": GraphQLField(ScalarType, deprecation_reason="A terrible reason"),
"baz": GraphQLField(ScalarType, deprecation_reason=""),
},
)
deprecated_field = TypeWithDeprecatedField.fields["bar"]
assert deprecated_field == GraphQLField(
ScalarType, deprecation_reason="A terrible reason"
)
assert deprecated_field.deprecation_reason == "A terrible reason"
deprecated_field = TypeWithDeprecatedField.fields["baz"]
assert deprecated_field == GraphQLField(ScalarType, deprecation_reason="")
assert deprecated_field.deprecation_reason == ""
def accepts_an_object_type_with_output_type_as_field():
# this is a shortcut syntax for simple fields
obj_type = GraphQLObjectType("SomeObject", {"f": ScalarType}) # type: ignore
assert list(obj_type.fields) == ["f"]
field = obj_type.fields["f"]
assert isinstance(field, GraphQLField)
assert field.type is ScalarType
assert field.args == {}
assert field.deprecation_reason is None
def accepts_an_object_type_with_a_field_function():
obj_type = GraphQLObjectType(
"SomeObject", lambda: {"f": GraphQLField(ScalarType)}
)
assert list(obj_type.fields) == ["f"]
field = obj_type.fields["f"]
assert isinstance(field, GraphQLField)
assert field.description is None
assert field.type is ScalarType
assert field.args == {}
assert field.resolve is None
assert field.subscribe is None
assert field.deprecation_reason is None
assert field.extensions == {}
assert field.ast_node is None
def thunk_for_fields_of_object_type_is_resolved_only_once():
calls = 0
def fields():
nonlocal calls
calls += 1
return {"f": GraphQLField(ScalarType)}
obj_type = GraphQLObjectType("SomeObject", fields)
assert "f" in obj_type.fields
assert calls == 1
assert "f" in obj_type.fields
assert calls == 1
def accepts_an_object_type_with_field_args():
obj_type = GraphQLObjectType(
"SomeObject",
{"f": GraphQLField(ScalarType, args={"arg": GraphQLArgument(ScalarType)})},
)
field = obj_type.fields["f"]
assert isinstance(field, GraphQLField)
assert field.description is None
assert field.type is ScalarType
assert isinstance(field.args, dict)
assert list(field.args) == ["arg"]
arg = field.args["arg"]
assert isinstance(arg, GraphQLArgument)
assert arg.description is None
assert arg.type is ScalarType
assert arg.default_value is Undefined
assert arg.deprecation_reason is None
assert arg.extensions == {}
assert arg.ast_node is None
assert field.resolve is None
assert field.subscribe is None
assert field.deprecation_reason is None
assert field.extensions == {}
assert field.ast_node is None
def accepts_an_object_type_with_list_interfaces():
obj_type = GraphQLObjectType("SomeObject", {}, [InterfaceType])
assert obj_type.interfaces == (InterfaceType,)
def accepts_object_type_with_interfaces_as_a_function_returning_a_list():
obj_type = GraphQLObjectType("SomeObject", {}, lambda: [InterfaceType])
assert obj_type.interfaces == (InterfaceType,)
def thunk_for_interfaces_of_object_type_is_resolved_only_once():
calls = 0
def interfaces():
nonlocal calls
calls += 1
return [InterfaceType]
obj_type = GraphQLObjectType("SomeObject", {}, interfaces)
assert obj_type.interfaces == (InterfaceType,)
assert calls == 1
assert obj_type.interfaces == (InterfaceType,)
assert calls == 1
def accepts_a_lambda_as_an_object_field_resolver():
obj_type = GraphQLObjectType(
"SomeObject",
{
"f": GraphQLField(
ScalarType, resolve=lambda _obj, _info: {} # pragma: no cover
)
},
)
assert obj_type.fields
def accepts_an_object_type_with_ast_node_and_extension_ast_nodes():
ast_node = ObjectTypeDefinitionNode()
extension_ast_nodes = [ObjectTypeExtensionNode()]
object_type = GraphQLObjectType(
"SomeObject",
{"f": GraphQLField(ScalarType)},
ast_node=ast_node,
extension_ast_nodes=extension_ast_nodes,
)
assert object_type.ast_node is ast_node
assert object_type.extension_ast_nodes == tuple(extension_ast_nodes)
def rejects_an_object_type_with_incorrectly_typed_name():
with raises(TypeError, match="missing .* required .* 'name'"):
# noinspection PyArgumentList
GraphQLObjectType() # type: ignore
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(None, {}) # type: ignore
assert str(exc_info.value) == "Must provide name."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(42, {}) # type: ignore
assert str(exc_info.value) == "Expected name to be a string."
def rejects_an_object_type_with_invalid_name():
with raises(GraphQLError) as exc_info:
GraphQLObjectType("", {})
assert str(exc_info.value) == "Expected name to be a non-empty string."
with raises(GraphQLError) as exc_info:
GraphQLObjectType("bad-name", {})
assert str(exc_info.value) == (
"Names must only contain [_a-zA-Z0-9] but 'bad-name' does not."
)
def rejects_an_object_type_field_with_undefined_config():
undefined_field = cast(GraphQLField, None)
obj_type = GraphQLObjectType("SomeObject", {"f": undefined_field})
with raises(TypeError) as exc_info:
assert not obj_type.fields
msg = str(exc_info.value)
assert msg == "SomeObject fields must be GraphQLField or output type objects."
def rejects_an_object_type_with_incorrectly_typed_fields():
invalid_field = cast(GraphQLField, [GraphQLField(ScalarType)])
obj_type = GraphQLObjectType("SomeObject", {"f": invalid_field})
with raises(TypeError) as exc_info:
assert not obj_type.fields
msg = str(exc_info.value)
assert msg == "SomeObject fields must be GraphQLField or output type objects."
def rejects_an_object_type_with_incorrectly_named_fields():
obj_type = GraphQLObjectType(
"SomeObject", {"bad-name": GraphQLField(ScalarType)}
)
with raises(GraphQLError) as exc_info:
assert not obj_type.fields
msg = str(exc_info.value)
assert msg == "Names must only contain [_a-zA-Z0-9] but 'bad-name' does not."
def rejects_an_object_type_field_function_that_returns_incorrect_type():
obj_type = GraphQLObjectType(
"SomeObject", lambda: [GraphQLField(ScalarType)] # type: ignore
)
with raises(TypeError) as exc_info:
assert not obj_type.fields
assert str(exc_info.value) == (
"SomeObject fields must be specified as a mapping with field names as keys."
)
def rejects_an_object_type_field_function_that_raises_an_error():
def fields():
raise RuntimeError("Oops!")
obj_type = GraphQLObjectType("SomeObject", fields)
with raises(TypeError) as exc_info:
assert not obj_type.fields
assert str(exc_info.value) == "SomeObject fields cannot be resolved. Oops!"
def rejects_an_object_type_with_incorrectly_typed_field_args():
invalid_args = [{"bad_args": GraphQLArgument(ScalarType)}]
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(
"SomeObject",
{
"badField": GraphQLField(
ScalarType, args=invalid_args # type: ignore
)
},
)
msg = str(exc_info.value)
assert msg == "Field args must be a dict with argument names as keys."
def rejects_an_object_type_with_incorrectly_named_field_args():
obj_type = GraphQLObjectType(
"SomeObject",
lambda: {
"badField": GraphQLField(
ScalarType, args={"bad-name": GraphQLArgument(ScalarType)}
)
},
)
with raises(GraphQLError) as exc_info:
assert not obj_type.fields
msg = str(exc_info.value)
assert msg == (
"SomeObject fields cannot be resolved."
" Names must only contain [_a-zA-Z0-9] but 'bad-name' does not."
)
def rejects_an_object_type_with_incorrectly_typed_interfaces():
obj_type = GraphQLObjectType("SomeObject", {}, interfaces={})
with raises(TypeError) as exc_info:
assert not obj_type.interfaces
assert str(exc_info.value) == (
"SomeObject interfaces must be specified"
" as a collection of GraphQLInterfaceType instances."
)
def rejects_object_type_with_incorrectly_typed_interfaces_as_a_function():
obj_type = GraphQLObjectType("SomeObject", {}, interfaces=lambda: {})
with raises(TypeError) as exc_info:
assert not obj_type.interfaces
assert str(exc_info.value) == (
"SomeObject interfaces must be specified"
" as a collection of GraphQLInterfaceType instances."
)
def rejects_object_type_with_interfaces_as_function_that_raises_an_error():
def interfaces():
raise RuntimeError("Oops!")
obj_type = GraphQLObjectType("SomeObject", {}, interfaces=interfaces)
with raises(TypeError) as exc_info:
assert not obj_type.interfaces
assert str(exc_info.value) == "SomeObject interfaces cannot be resolved. Oops!"
def rejects_an_empty_object_field_resolver():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(
"SomeObject",
{"field": GraphQLField(ScalarType, resolve={})}, # type: ignore
)
msg = str(exc_info.value)
assert msg == "Field resolver must be a function if provided, but got: {}."
def rejects_a_constant_scalar_value_resolver():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(
"SomeObject",
{"field": GraphQLField(ScalarType, resolve=0)}, # type: ignore
)
msg = str(exc_info.value)
assert msg == "Field resolver must be a function if provided, but got: 0."
def rejects_an_object_type_with_an_incorrect_type_for_is_type_of():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType("AnotherObject", {}, is_type_of={}) # type: ignore
assert str(exc_info.value) == (
"AnotherObject must provide 'is_type_of' as a function, but got: {}."
)
def rejects_an_object_type_with_an_incorrect_ast_node():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType("SomeObject", {}, ast_node=Node()) # type: ignore
msg = str(exc_info.value)
assert msg == "SomeObject AST node must be a TypeDefinitionNode."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(
"SomeObject", {}, ast_node=TypeDefinitionNode() # type: ignore
)
msg = str(exc_info.value)
assert msg == "SomeObject AST node must be an ObjectTypeDefinitionNode."
def rejects_an_object_type_with_incorrect_extension_ast_nodes():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(
"SomeObject", {}, extension_ast_nodes=[Node()] # type: ignore
)
assert str(exc_info.value) == (
"SomeObject extension AST nodes must be specified"
" as a collection of TypeExtensionNode instances."
)
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLObjectType(
"SomeObject",
{},
extension_ast_nodes=[TypeExtensionNode()], # type: ignore
)
assert str(exc_info.value) == (
"SomeObject extension AST nodes must be specified"
" as a collection of ObjectTypeExtensionNode instances."
)
def describe_type_system_interfaces():
def defines_an_interface_type():
fields = {"f": GraphQLField(ScalarType)}
interface = GraphQLInterfaceType("AnotherInterface", fields)
assert interface.name == "AnotherInterface"
assert interface.fields == fields
assert interface.fields is not fields
assert interface.resolve_type is None
assert interface.extensions == {}
kwargs = interface.to_kwargs()
assert kwargs == {
"name": "AnotherInterface",
"description": None,
"fields": fields,
"interfaces": (),
"resolve_type": None,
"extensions": {},
"ast_node": None,
"extension_ast_nodes": (),
}
def accepts_an_interface_type_defining_resolve_type():
def resolve_type(_obj, _info, _type):
pass
interface = GraphQLInterfaceType(
"AnotherInterface", {}, resolve_type=resolve_type
)
assert interface.resolve_type is resolve_type
def accepts_an_interface_type_with_output_types_as_fields():
interface = GraphQLInterfaceType(
"AnotherInterface", {"someField": ScalarType} # type: ignore
)
fields = interface.fields
assert isinstance(fields, dict)
assert list(fields) == ["someField"]
field = fields["someField"]
assert isinstance(field, GraphQLField)
assert field.type is ScalarType
def accepts_an_interface_type_with_a_field_function():
fields = {"f": GraphQLField(ScalarType)}
interface = GraphQLInterfaceType("AnotherInterface", lambda: fields)
assert interface.fields == fields
def thunk_for_fields_of_interface_type_is_resolved_only_once():
calls = 0
def fields():
nonlocal calls
calls += 1
return {"f": GraphQLField(ScalarType)}
interface = GraphQLInterfaceType("AnotherInterface", fields)
assert "f" in interface.fields
assert calls == 1
assert "f" in interface.fields
assert calls == 1
def accepts_an_interface_type_with_a_list_of_interfaces():
implementing = GraphQLInterfaceType(
"AnotherInterface", {}, interfaces=[InterfaceType]
)
assert implementing.interfaces == (InterfaceType,)
def accepts_an_interface_type_with_an_interfaces_function():
implementing = GraphQLInterfaceType(
"AnotherInterface", {}, interfaces=lambda: [InterfaceType]
)
assert implementing.interfaces == (InterfaceType,)
def thunk_for_interfaces_of_interface_type_is_resolved_only_once():
calls = 0
def interfaces():
nonlocal calls
calls += 1
return [InterfaceType]
implementing = GraphQLInterfaceType(
"AnotherInterface", {}, interfaces=interfaces
)
assert implementing.interfaces == (InterfaceType,)
assert calls == 1
assert implementing.interfaces == (InterfaceType,)
assert calls == 1
def accepts_an_interface_type_with_ast_node_and_extension_ast_nodes():
ast_node = InterfaceTypeDefinitionNode()
extension_ast_nodes = [InterfaceTypeExtensionNode()]
interface_type = GraphQLInterfaceType(
"SomeInterface",
{"f": GraphQLField(ScalarType)},
ast_node=ast_node,
extension_ast_nodes=extension_ast_nodes,
)
assert interface_type.ast_node is ast_node
assert interface_type.extension_ast_nodes == tuple(extension_ast_nodes)
def rejects_an_interface_type_with_incorrectly_typed_fields():
interface = GraphQLInterfaceType("SomeInterface", []) # type: ignore
with raises(TypeError) as exc_info:
assert not interface.fields
assert str(exc_info.value) == (
"SomeInterface fields must be specified"
" as a mapping with field names as keys."
)
interface = GraphQLInterfaceType(
"SomeInterface", {"f": InputObjectType} # type: ignore
)
with raises(TypeError) as exc_info:
assert not interface.fields
assert str(exc_info.value) == (
"SomeInterface fields must be GraphQLField or output type objects."
)
def rejects_an_interface_type_with_unresolvable_fields():
def fields():
raise RuntimeError("Oops!")
interface = GraphQLInterfaceType("SomeInterface", fields)
with raises(TypeError) as exc_info:
assert not interface.fields
assert str(exc_info.value) == "SomeInterface fields cannot be resolved. Oops!"
def rejects_an_interface_type_with_incorrectly_typed_name():
with raises(TypeError, match="missing .* required .* 'name'"):
# noinspection PyArgumentList
GraphQLInterfaceType() # type: ignore
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType(None, {}) # type: ignore
assert str(exc_info.value) == "Must provide name."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType(42, {}) # type: ignore
assert str(exc_info.value) == "Expected name to be a string."
def rejects_an_interface_type_with_invalid_name():
with raises(GraphQLError) as exc_info:
GraphQLInterfaceType("", {})
assert str(exc_info.value) == "Expected name to be a non-empty string."
with raises(GraphQLError) as exc_info:
GraphQLInterfaceType("bad-name", {})
assert str(exc_info.value) == (
"Names must only contain [_a-zA-Z0-9] but 'bad-name' does not."
)
def rejects_an_interface_type_with_incorrectly_typed_interfaces():
interface = GraphQLInterfaceType("AnotherInterface", {}, lambda: {})
with raises(TypeError) as exc_info:
assert not interface.interfaces
assert str(exc_info.value) == (
"AnotherInterface interfaces must be specified"
" as a collection of GraphQLInterfaceType instances."
)
def rejects_an_interface_type_with_unresolvable_interfaces():
def interfaces():
raise RuntimeError("Oops!")
interface = GraphQLInterfaceType("AnotherInterface", {}, interfaces)
with raises(TypeError) as exc_info:
assert not interface.interfaces
assert (
str(exc_info.value)
== "AnotherInterface interfaces cannot be resolved. Oops!"
)
def rejects_an_interface_type_with_an_incorrect_type_for_resolve_type():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType(
"AnotherInterface", {}, resolve_type={} # type: ignore
)
assert str(exc_info.value) == (
"AnotherInterface must provide 'resolve_type' as a function,"
" but got: {}."
)
def rejects_an_interface_type_with_an_incorrect_ast_node():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType("SomeInterface", {}, ast_node=Node()) # type: ignore
msg = str(exc_info.value)
assert msg == "SomeInterface AST node must be a TypeDefinitionNode."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType(
"SomeInterface", {}, ast_node=TypeDefinitionNode() # type: ignore
)
msg = str(exc_info.value)
assert msg == "SomeInterface AST node must be an InterfaceTypeDefinitionNode."
def rejects_an_interface_type_with_incorrect_extension_ast_nodes():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType(
"SomeInterface", {}, extension_ast_nodes=[Node()] # type: ignore
)
assert str(exc_info.value) == (
"SomeInterface extension AST nodes must be specified"
" as a collection of TypeExtensionNode instances."
)
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLInterfaceType(
"SomeInterface",
{},
extension_ast_nodes=[TypeExtensionNode()], # type: ignore
)
assert str(exc_info.value) == (
"SomeInterface extension AST nodes must be specified"
" as a collection of InterfaceTypeExtensionNode instances."
)
def describe_type_system_unions():
def accepts_a_union_type_defining_resolve_type():
assert GraphQLUnionType("SomeUnion", [ObjectType])
def accepts_a_union_type_with_list_types():
union_type = GraphQLUnionType("SomeUnion", [ObjectType])
assert union_type.types == (ObjectType,)
def accepts_a_union_type_with_function_returning_a_list_of_types():
union_type = GraphQLUnionType("SomeUnion", lambda: [ObjectType])
assert union_type.types == (ObjectType,)
def accepts_a_union_type_without_types():
with raises(TypeError, match="missing 1 required positional argument: 'types'"):
# noinspection PyArgumentList
GraphQLUnionType("SomeUnion") # type: ignore
union_type = GraphQLUnionType("SomeUnion", None) # type: ignore
assert union_type.types == ()
union_type = GraphQLUnionType("SomeUnion", [])
assert union_type.types == ()
def accepts_a_union_type_with_ast_node_and_extension_ast_nodes():
ast_node = UnionTypeDefinitionNode()
extension_ast_nodes = [UnionTypeExtensionNode()]
union_type = GraphQLUnionType(
"SomeUnion",
[ObjectType],
ast_node=ast_node,
extension_ast_nodes=extension_ast_nodes,
)
assert union_type.ast_node is ast_node
assert union_type.extension_ast_nodes == tuple(extension_ast_nodes)
def rejects_a_union_type_with_incorrectly_typed__name():
with raises(TypeError, match="missing .* required .* 'name'"):
# noinspection PyArgumentList
GraphQLUnionType() # type: ignore
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLUnionType(None, []) # type: ignore
assert str(exc_info.value) == "Must provide name."
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLUnionType(42, []) # type: ignore
assert str(exc_info.value) == "Expected name to be a string."
def rejects_a_union_type_with_invalid_name():
with raises(GraphQLError) as exc_info:
GraphQLUnionType("", [])
assert str(exc_info.value) == "Expected name to be a non-empty string."
with raises(GraphQLError) as exc_info:
GraphQLUnionType("bad-name", [])
assert str(exc_info.value) == (
"Names must only contain [_a-zA-Z0-9] but 'bad-name' does not."
)
def rejects_a_union_type_with_an_incorrect_type_for_resolve_type():
with raises(TypeError) as exc_info:
# noinspection PyTypeChecker
GraphQLUnionType("SomeUnion", [], resolve_type={}) # type: ignore