Debian Codegraph @

tests/utilities/test_extend_schema.py

raw
1 from typing import Union
2
3 from pytest import raises
4
5 from graphql import graphql_sync
6 from graphql.error import GraphQLSyntaxError
7 from graphql.language import parse, print_ast
8 from graphql.type import (
9 GraphQLArgument,
10 GraphQLBoolean,
11 GraphQLDirective,
12 GraphQLEnumValue,
13 GraphQLField,
14 GraphQLFloat,
15 GraphQLID,
16 GraphQLInputField,
17 GraphQLInt,
18 GraphQLNamedType,
19 GraphQLSchema,
20 GraphQLString,
21 assert_directive,
22 assert_enum_type,
23 assert_input_object_type,
24 assert_interface_type,
25 assert_object_type,
26 assert_scalar_type,
27 assert_union_type,
28 validate_schema,
29 )
30 from graphql.utilities import (
31 build_ast_schema,
32 build_schema,
33 concat_ast,
34 extend_schema,
35 print_schema,
36 )
37
38 from ..utils import dedent
39
40 TypeWithAstNode = Union[
41 GraphQLArgument,
42 GraphQLEnumValue,
43 GraphQLField,
44 GraphQLInputField,
45 GraphQLNamedType,
46 GraphQLSchema,
47 ]
48
49 TypeWithExtensionAstNodes = Union[
50 GraphQLDirective,
51 GraphQLNamedType,
52 GraphQLSchema,
53 ]
54
55
56 def expect_extension_ast_nodes(obj: TypeWithExtensionAstNodes, expected: str) -> None:
57 assert obj is not None and obj.extension_ast_nodes is not None
58 assert "\n\n".join(print_ast(node) for node in obj.extension_ast_nodes) == expected
59
60
61 def expect_ast_node(obj: TypeWithAstNode, expected: str) -> None:
62 assert obj is not None and obj.ast_node is not None
63 assert print_ast(obj.ast_node) == expected
64
65
66 def expect_schema_changes(
67 schema: GraphQLSchema, extended_schema: GraphQLSchema, expected: str
68 ) -> None:
69 schema_definitions = {
70 print_ast(node) for node in parse(print_schema(schema)).definitions
71 }
72 assert (
73 "\n\n".join(
74 schema_def
75 for schema_def in (
76 print_ast(node)
77 for node in parse(print_schema(extended_schema)).definitions
78 )
79 if schema_def not in schema_definitions
80 )
81 == expected
82 )
83
84
85 def describe_extend_schema():
86 def returns_the_original_schema_when_there_are_no_type_definitions():
87 schema = build_schema("type Query")
88 extended_schema = extend_schema(schema, parse("{ field }"))
89 assert extended_schema == schema
90
91 def can_be_used_for_limited_execution():
92 schema = build_schema("type Query")
93 extend_ast = parse("""
94 extend type Query {
95 newField: String
96 }
97 """)
98 extended_schema = extend_schema(schema, extend_ast)
99
100 result = graphql_sync(
101 schema=extended_schema, source="{ newField }", root_value={"newField": 123}
102 )
103 assert result == ({"newField": "123"}, None)
104
105 def extends_objects_by_adding_new_fields():
106 schema = build_schema('''
107 type Query {
108 someObject: SomeObject
109 }
110
111 type SomeObject implements AnotherInterface & SomeInterface {
112 self: SomeObject
113 tree: [SomeObject]!
114 """Old field description."""
115 oldField: String
116 }
117
118 interface SomeInterface {
119 self: SomeInterface
120 }
121
122 interface AnotherInterface {
123 self: SomeObject
124 }
125 ''')
126 extension_sdl = dedent('''
127 extend type SomeObject {
128 """New field description."""
129 newField(arg: Boolean): String
130 }
131 ''')
132 extended_schema = extend_schema(schema, parse(extension_sdl))
133
134 assert validate_schema(extended_schema) == []
135 expect_schema_changes(
136 schema,
137 extended_schema,
138 dedent('''
139 type SomeObject implements AnotherInterface & SomeInterface {
140 self: SomeObject
141 tree: [SomeObject]!
142 """Old field description."""
143 oldField: String
144 """New field description."""
145 newField(arg: Boolean): String
146 }
147 '''),
148 )
149
150 def extends_objects_with_standard_type_fields():
151 schema = build_schema("type Query")
152
153 # Only String and Boolean are used by introspection types
154 assert schema.get_type("Int") is None
155 assert schema.get_type("Float") is None
156 assert schema.get_type("String") is GraphQLString
157 assert schema.get_type("Boolean") is GraphQLBoolean
158 assert schema.get_type("ID") is None
159
160 extend_ast = parse("""
161 extend type Query {
162 bool: Boolean
163 }
164 """)
165 extended_schema = extend_schema(schema, extend_ast)
166
167 assert validate_schema(extended_schema) == []
168 assert extended_schema.get_type("Int") is None
169 assert extended_schema.get_type("Float") is None
170 assert extended_schema.get_type("String") is GraphQLString
171 assert extended_schema.get_type("Boolean") is GraphQLBoolean
172 assert extended_schema.get_type("ID") is None
173
174 extend_twice_ast = parse("""
175 extend type Query {
176 int: Int
177 float: Float
178 id: ID
179 }
180 """)
181 extended_twice_schema = extend_schema(schema, extend_twice_ast)
182
183 assert validate_schema(extended_twice_schema) == []
184 assert extended_twice_schema.get_type("Int") is GraphQLInt
185 assert extended_twice_schema.get_type("Float") is GraphQLFloat
186 assert extended_twice_schema.get_type("String") is GraphQLString
187 assert extended_twice_schema.get_type("Boolean") is GraphQLBoolean
188 assert extended_twice_schema.get_type("ID") is GraphQLID
189
190 def extends_enums_by_adding_new_values():
191 schema = build_schema('''
192 type Query {
193 someEnum(arg: SomeEnum): SomeEnum
194 }
195
196 directive @foo(arg: SomeEnum) on SCHEMA
197
198 enum SomeEnum {
199 """Old value description."""
200 OLD_VALUE
201 }
202 ''')
203 extend_ast = parse('''
204 extend enum SomeEnum {
205 """New value description."""
206 NEW_VALUE
207 }
208 ''')
209 extended_schema = extend_schema(schema, extend_ast)
210
211 assert validate_schema(extended_schema) == []
212 expect_schema_changes(
213 schema,
214 extended_schema,
215 dedent('''
216 enum SomeEnum {
217 """Old value description."""
218 OLD_VALUE
219 """New value description."""
220 NEW_VALUE
221 }
222 '''),
223 )
224
225 def extends_unions_by_adding_new_types():
226 schema = build_schema("""
227 type Query {
228 someUnion: SomeUnion
229 }
230
231 union SomeUnion = Foo | Biz
232
233 type Foo { foo: String }
234 type Biz { biz: String }
235 type Bar { bar: String }
236 """)
237 extend_ast = parse("""
238 extend union SomeUnion = Bar
239 """)
240 extended_schema = extend_schema(schema, extend_ast)
241
242 assert validate_schema(extended_schema) == []
243 expect_schema_changes(
244 schema,
245 extended_schema,
246 dedent("""
247 union SomeUnion = Foo | Biz | Bar
248 """),
249 )
250
251 def allows_extension_of_union_by_adding_itself():
252 schema = build_schema("""
253 union SomeUnion
254 """)
255 extend_ast = parse("""
256 extend union SomeUnion = SomeUnion
257 """)
258 # invalid schema cannot be built with Python
259 with raises(TypeError) as exc_info:
260 extend_schema(schema, extend_ast)
261 assert str(exc_info.value) == (
262 "SomeUnion types must be specified"
263 " as a collection of GraphQLObjectType instances."
264 )
265
266 def extends_inputs_by_adding_new_fields():
267 schema = build_schema('''
268 type Query {
269 someInput(arg: SomeInput): String
270 }
271
272 directive @foo(arg: SomeInput) on SCHEMA
273
274 input SomeInput {
275 """Old field description."""
276 oldField: String
277 }
278 ''')
279 extend_ast = parse('''
280 extend input SomeInput {
281 """New field description."""
282 newField: String
283 }
284 ''')
285 extended_schema = extend_schema(schema, extend_ast)
286
287 assert validate_schema(extended_schema) == []
288 expect_schema_changes(
289 schema,
290 extended_schema,
291 dedent('''
292 input SomeInput {
293 """Old field description."""
294 oldField: String
295 """New field description."""
296 newField: String
297 }
298 '''),
299 )
300
301 def extends_scalars_by_adding_new_directives():
302 schema = build_schema("""
303 type Query {
304 someScalar(arg: SomeScalar): SomeScalar
305 }
306
307 directive @foo(arg: SomeScalar) on SCALAR
308
309 input FooInput {
310 foo: SomeScalar
311 }
312
313 scalar SomeScalar
314 """)
315 extension_sdl = dedent("""
316 extend scalar SomeScalar @foo
317 """)
318 extended_schema = extend_schema(schema, parse(extension_sdl))
319 some_scalar = assert_scalar_type(extended_schema.get_type("SomeScalar"))
320
321 assert validate_schema(extended_schema) == []
322 expect_extension_ast_nodes(some_scalar, extension_sdl)
323
324 def extends_scalars_by_adding_specified_by_directive():
325 schema = build_schema("""
326 type Query {
327 foo: Foo
328 }
329
330 scalar Foo
331
332 directive @foo on SCALAR
333 """)
334 extension_sdl = dedent("""
335 extend scalar Foo @foo
336
337 extend scalar Foo @specifiedBy(url: "https://example.com/foo_spec")
338 """)
339
340 extended_schema = extend_schema(schema, parse(extension_sdl))
341 foo = assert_scalar_type(extended_schema.get_type("Foo"))
342
343 assert foo.specified_by_url == "https://example.com/foo_spec"
344
345 assert validate_schema(extended_schema) == []
346 expect_extension_ast_nodes(foo, extension_sdl)
347
348 def correctly_assigns_ast_nodes_to_new_and_extended_types():
349 schema = build_schema("""
350 type Query
351
352 scalar SomeScalar
353 enum SomeEnum
354 union SomeUnion
355 input SomeInput
356 type SomeObject
357 interface SomeInterface
358
359 directive @foo on SCALAR
360 """)
361 first_extension_ast = parse("""
362 extend type Query {
363 newField(testArg: TestInput): TestEnum
364 }
365
366 extend scalar SomeScalar @foo
367
368 extend enum SomeEnum {
369 NEW_VALUE
370 }
371
372 extend union SomeUnion = SomeObject
373
374 extend input SomeInput {
375 newField: String
376 }
377
378 extend interface SomeInterface {
379 newField: String
380 }
381
382 enum TestEnum {
383 TEST_VALUE
384 }
385
386 input TestInput {
387 testInputField: TestEnum
388 }
389 """)
390 extended_schema = extend_schema(schema, first_extension_ast)
391
392 second_extension_ast = parse("""
393 extend type Query {
394 oneMoreNewField: TestUnion
395 }
396
397 extend scalar SomeScalar @test
398
399 extend enum SomeEnum {
400 ONE_MORE_NEW_VALUE
401 }
402
403 extend union SomeUnion = TestType
404
405 extend input SomeInput {
406 oneMoreNewField: String
407 }
408
409 extend interface SomeInterface {
410 oneMoreNewField: String
411 }
412
413 union TestUnion = TestType
414
415 interface TestInterface {
416 interfaceField: String
417 }
418
419 type TestType implements TestInterface {
420 interfaceField: String
421 }
422
423 directive @test(arg: Int) repeatable on FIELD | SCALAR
424 """)
425 extended_twice_schema = extend_schema(extended_schema, second_extension_ast)
426
427 extend_in_one_go_schema = extend_schema(
428 schema, concat_ast([first_extension_ast, second_extension_ast])
429 )
430 assert print_schema(extend_in_one_go_schema) == print_schema(
431 extended_twice_schema
432 )
433
434 query = assert_object_type(extended_twice_schema.get_type("Query"))
435 some_enum = assert_enum_type(extended_twice_schema.get_type("SomeEnum"))
436 some_union = assert_union_type(extended_twice_schema.get_type("SomeUnion"))
437 some_scalar = assert_scalar_type(extended_twice_schema.get_type("SomeScalar"))
438 some_input = assert_input_object_type(
439 extended_twice_schema.get_type("SomeInput")
440 )
441 some_interface = assert_interface_type(
442 extended_twice_schema.get_type("SomeInterface")
443 )
444
445 test_input = assert_input_object_type(
446 extended_twice_schema.get_type("TestInput")
447 )
448 test_enum = assert_enum_type(extended_twice_schema.get_type("TestEnum"))
449 test_union = assert_union_type(extended_twice_schema.get_type("TestUnion"))
450 test_type = assert_object_type(extended_twice_schema.get_type("TestType"))
451 test_interface = assert_interface_type(
452 extended_twice_schema.get_type("TestInterface")
453 )
454 test_directive = assert_directive(extended_twice_schema.get_directive("test"))
455
456 assert test_type.extension_ast_nodes == ()
457 assert test_enum.extension_ast_nodes == ()
458 assert test_union.extension_ast_nodes == ()
459 assert test_input.extension_ast_nodes == ()
460 assert test_interface.extension_ast_nodes == ()
461
462 assert query.extension_ast_nodes
463 assert len(query.extension_ast_nodes) == 2
464 assert some_scalar.extension_ast_nodes
465 assert len(some_scalar.extension_ast_nodes) == 2
466 assert some_enum.extension_ast_nodes
467 assert len(some_enum.extension_ast_nodes) == 2
468 assert some_union.extension_ast_nodes
469 assert len(some_union.extension_ast_nodes) == 2
470 assert some_input.extension_ast_nodes
471 assert len(some_input.extension_ast_nodes) == 2
472 assert some_interface.extension_ast_nodes
473 assert len(some_interface.extension_ast_nodes) == 2
474
475 assert {
476 test_input.ast_node,
477 test_enum.ast_node,
478 test_union.ast_node,
479 test_interface.ast_node,
480 test_type.ast_node,
481 test_directive.ast_node,
482 *query.extension_ast_nodes,
483 *some_scalar.extension_ast_nodes,
484 *some_enum.extension_ast_nodes,
485 *some_union.extension_ast_nodes,
486 *some_input.extension_ast_nodes,
487 *some_interface.extension_ast_nodes,
488 } == {*first_extension_ast.definitions, *second_extension_ast.definitions}
489
490 new_field = query.fields["newField"]
491 expect_ast_node(new_field, "newField(testArg: TestInput): TestEnum")
492 expect_ast_node(new_field.args["testArg"], "testArg: TestInput")
493 expect_ast_node(query.fields["oneMoreNewField"], "oneMoreNewField: TestUnion")
494
495 expect_ast_node(some_enum.values["NEW_VALUE"], "NEW_VALUE")
496
497 one_more_new_value = some_enum.values["ONE_MORE_NEW_VALUE"]
498 expect_ast_node(one_more_new_value, "ONE_MORE_NEW_VALUE")
499 expect_ast_node(some_input.fields["newField"], "newField: String")
500 expect_ast_node(some_input.fields["oneMoreNewField"], "oneMoreNewField: String")
501 expect_ast_node(some_interface.fields["newField"], "newField: String")
502 expect_ast_node(
503 some_interface.fields["oneMoreNewField"], "oneMoreNewField: String"
504 )
505
506 expect_ast_node(test_input.fields["testInputField"], "testInputField: TestEnum")
507
508 expect_ast_node(test_enum.values["TEST_VALUE"], "TEST_VALUE")
509
510 expect_ast_node(
511 test_interface.fields["interfaceField"], "interfaceField: String"
512 )
513 expect_ast_node(test_type.fields["interfaceField"], "interfaceField: String")
514 expect_ast_node(test_directive.args["arg"], "arg: Int")
515
516 def builds_types_with_deprecated_fields_and_values():
517 schema = GraphQLSchema()
518 extend_ast = parse("""
519 type SomeObject {
520 deprecatedField: String @deprecated(reason: "not used anymore")
521 }
522
523 enum SomeEnum {
524 DEPRECATED_VALUE @deprecated(reason: "do not use")
525 }
526 """)
527 extended_schema = extend_schema(schema, extend_ast)
528
529 some_type = assert_object_type(extended_schema.get_type("SomeObject"))
530 deprecated_field = some_type.fields["deprecatedField"]
531 assert deprecated_field.deprecation_reason == "not used anymore"
532
533 some_enum = assert_enum_type(extended_schema.get_type("SomeEnum"))
534 deprecated_enum = some_enum.values["DEPRECATED_VALUE"]
535 assert deprecated_enum.deprecation_reason == "do not use"
536
537 def extends_objects_with_deprecated_fields():
538 schema = build_schema("type SomeObject")
539 extend_ast = parse("""
540 extend type SomeObject {
541 deprecatedField: String @deprecated(reason: "not used anymore")
542 }
543 """)
544 extended_schema = extend_schema(schema, extend_ast)
545
546 some_type = assert_object_type(extended_schema.get_type("SomeObject"))
547 deprecated_field = some_type.fields["deprecatedField"]
548 assert deprecated_field.deprecation_reason == "not used anymore"
549
550 def extend_enums_with_deprecated_values():
551 schema = build_schema("enum SomeEnum")
552 extend_ast = parse("""
553 extend enum SomeEnum {
554 DEPRECATED_VALUE @deprecated(reason: "do not use")
555 }
556 """)
557 extended_schema = extend_schema(schema, extend_ast)
558
559 some_enum = assert_enum_type(extended_schema.get_type("SomeEnum"))
560 deprecated_value = some_enum.values["DEPRECATED_VALUE"]
561 assert deprecated_value.deprecation_reason == "do not use"
562
563 def adds_new_unused_types():
564 schema = build_schema("""
565 type Query {
566 dummy: String
567 }
568 """)
569 extension_sdl = dedent("""
570 type DummyUnionMember {
571 someField: String
572 }
573
574 enum UnusedEnum {
575 SOME_VALUE
576 }
577
578 input UnusedInput {
579 someField: String
580 }
581
582 interface UnusedInterface {
583 someField: String
584 }
585
586 type UnusedObject {
587 someField: String
588 }
589
590 union UnusedUnion = DummyUnionMember
591 """)
592 extended_schema = extend_schema(schema, parse(extension_sdl))
593
594 assert validate_schema(extended_schema) == []
595 expect_schema_changes(schema, extended_schema, extension_sdl)
596
597 def extends_objects_by_adding_new_fields_with_arguments():
598 schema = build_schema("""
599 type SomeObject
600
601 type Query {
602 someObject: SomeObject
603 }
604 """)
605 extend_ast = parse("""
606 input NewInputObj {
607 field1: Int
608 field2: [Float]
609 field3: String!
610 }
611
612 extend type SomeObject {
613 newField(arg1: String, arg2: NewInputObj!): String
614 }
615 """)
616 extended_schema = extend_schema(schema, extend_ast)
617
618 assert validate_schema(extended_schema) == []
619 expect_schema_changes(
620 schema,
621 extended_schema,
622 dedent("""
623 type SomeObject {
624 newField(arg1: String, arg2: NewInputObj!): String
625 }
626
627 input NewInputObj {
628 field1: Int
629 field2: [Float]
630 field3: String!
631 }
632 """),
633 )
634
635 def extends_objects_by_adding_new_fields_with_existing_types():
636 schema = build_schema("""
637 type Query {
638 someObject: SomeObject
639 }
640
641 type SomeObject
642 enum SomeEnum { VALUE }
643 """)
644 extend_ast = parse("""
645 extend type SomeObject {
646 newField(arg1: SomeEnum!): SomeEnum
647 }
648 """)
649 extended_schema = extend_schema(schema, extend_ast)
650
651 assert validate_schema(extended_schema) == []
652 expect_schema_changes(
653 schema,
654 extended_schema,
655 dedent("""
656 type SomeObject {
657 newField(arg1: SomeEnum!): SomeEnum
658 }
659 """),
660 )
661
662 def extends_objects_by_adding_implemented_interfaces():
663 schema = build_schema("""
664 type Query {
665 someObject: SomeObject
666 }
667
668 type SomeObject {
669 foo: String
670 }
671
672 interface SomeInterface {
673 foo: String
674 }
675 """)
676 extend_ast = parse("""
677 extend type SomeObject implements SomeInterface
678 """)
679 extended_schema = extend_schema(schema, extend_ast)
680
681 assert validate_schema(extended_schema) == []
682 expect_schema_changes(
683 schema,
684 extended_schema,
685 dedent("""
686 type SomeObject implements SomeInterface {
687 foo: String
688 }
689 """),
690 )
691
692 def extends_objects_by_including_new_types():
693 schema = build_schema("""
694 type Query {
695 someObject: SomeObject
696 }
697
698 type SomeObject {
699 oldField: String
700 }
701 """)
702 new_types_sdl = """
703 enum NewEnum {
704 VALUE
705 }
706
707 interface NewInterface {
708 baz: String
709 }
710
711 type NewObject implements NewInterface {
712 baz: String
713 }
714
715 scalar NewScalar
716
717 union NewUnion = NewObject
718 """
719 extend_ast = parse(new_types_sdl + """
720 extend type SomeObject {
721 newObject: NewObject
722 newInterface: NewInterface
723 newUnion: NewUnion
724 newScalar: NewScalar
725 newEnum: NewEnum
726 newTree: [SomeObject]!
727 }
728 """)
729 extended_schema = extend_schema(schema, extend_ast)
730
731 assert validate_schema(extended_schema) == []
732 expect_schema_changes(
733 schema,
734 extended_schema,
735 dedent("""
736 type SomeObject {
737 oldField: String
738 newObject: NewObject
739 newInterface: NewInterface
740 newUnion: NewUnion
741 newScalar: NewScalar
742 newEnum: NewEnum
743 newTree: [SomeObject]!
744 }\n""" + new_types_sdl),
745 )
746
747 def extends_objects_by_adding_implemented_new_interfaces():
748 schema = build_schema("""
749 type Query {
750 someObject: SomeObject
751 }
752
753 type SomeObject implements OldInterface {
754 oldField: String
755 }
756
757 interface OldInterface {
758 oldField: String
759 }
760 """)
761 extend_ast = parse("""
762 extend type SomeObject implements NewInterface {
763 newField: String
764 }
765
766 interface NewInterface {
767 newField: String
768 }
769 """)
770 extended_schema = extend_schema(schema, extend_ast)
771
772 assert validate_schema(extended_schema) == []
773 expect_schema_changes(
774 schema,
775 extended_schema,
776 dedent("""
777 type SomeObject implements OldInterface & NewInterface {
778 oldField: String
779 newField: String
780 }
781
782 interface NewInterface {
783 newField: String
784 }
785 """),
786 )
787
788 def extends_different_types_multiple_times():
789 schema = build_schema("""
790 type Query {
791 someScalar: SomeScalar
792 someObject(someInput: SomeInput): SomeObject
793 someInterface: SomeInterface
794 someEnum: SomeEnum
795 someUnion: SomeUnion
796 }
797
798 scalar SomeScalar
799
800 type SomeObject implements SomeInterface {
801 oldField: String
802 }
803
804 interface SomeInterface {
805 oldField: String
806 }
807
808 enum SomeEnum {
809 OLD_VALUE
810 }
811
812 union SomeUnion = SomeObject
813
814 input SomeInput {
815 oldField: String
816 }
817 """)
818 new_types_sdl = dedent("""
819 scalar NewScalar
820
821 scalar AnotherNewScalar
822
823 type NewObject {
824 foo: String
825 }
826
827 type AnotherNewObject {
828 foo: String
829 }
830
831 interface NewInterface {
832 newField: String
833 }
834
835 interface AnotherNewInterface {
836 anotherNewField: String
837 }
838 """)
839 schema_with_new_types = extend_schema(schema, parse(new_types_sdl))
840 expect_schema_changes(schema, schema_with_new_types, new_types_sdl)
841
842 extend_ast = parse("""
843 extend scalar SomeScalar @specifiedBy(url: "http://example.com/foo_spec")
844
845 extend type SomeObject implements NewInterface {
846 newField: String
847 }
848
849 extend type SomeObject implements AnotherNewInterface {
850 anotherNewField: String
851 }
852
853 extend enum SomeEnum {
854 NEW_VALUE
855 }
856
857 extend enum SomeEnum {
858 ANOTHER_NEW_VALUE
859 }
860
861 extend union SomeUnion = NewObject
862
863 extend union SomeUnion = AnotherNewObject
864
865 extend input SomeInput {
866 newField: String
867 }
868
869 extend input SomeInput {
870 anotherNewField: String
871 }
872 """)
873 extended_schema = extend_schema(schema_with_new_types, extend_ast)
874
875 assert validate_schema(extended_schema) == []
876 expect_schema_changes(
877 schema,
878 extended_schema,
879 dedent("""
880 scalar SomeScalar @specifiedBy(url: "http://example.com/foo_spec")
881
882 type SomeObject implements SomeInterface & NewInterface & AnotherNewInterface {
883 oldField: String
884 newField: String
885 anotherNewField: String
886 }
887
888 enum SomeEnum {
889 OLD_VALUE
890 NEW_VALUE
891 ANOTHER_NEW_VALUE
892 }
893
894 union SomeUnion = SomeObject | NewObject | AnotherNewObject
895
896 input SomeInput {
897 oldField: String
898 newField: String
899 anotherNewField: String
900 }
901
902 """) + "\n\n" + new_types_sdl, # noqa: E501
903 )
904
905 def extends_interfaces_by_adding_new_fields():
906 schema = build_schema("""
907 interface SomeInterface {
908 oldField: String
909 }
910
911 interface AnotherInterface implements SomeInterface {
912 oldField: String
913 }
914
915 type SomeObject implements SomeInterface & AnotherInterface {
916 oldField: String
917 }
918
919 type Query {
920 someInterface: SomeInterface
921 }
922 """)
923 extend_ast = parse("""
924 extend interface SomeInterface {
925 newField: String
926 }
927
928 extend interface AnotherInterface {
929 newField: String
930 }
931
932 extend type SomeObject {
933 newField: String
934 }
935 """)
936 extended_schema = extend_schema(schema, extend_ast)
937
938 assert validate_schema(extended_schema) == []
939 expect_schema_changes(
940 schema,
941 extended_schema,
942 dedent("""
943 interface SomeInterface {
944 oldField: String
945 newField: String
946 }
947
948 interface AnotherInterface implements SomeInterface {
949 oldField: String
950 newField: String
951 }
952
953 type SomeObject implements SomeInterface & AnotherInterface {
954 oldField: String
955 newField: String
956 }
957 """),
958 )
959
960 def extends_interfaces_by_adding_new_implemented_interfaces():
961 schema = build_schema("""
962 interface SomeInterface {
963 oldField: String
964 }
965
966 interface AnotherInterface implements SomeInterface {
967 oldField: String
968 }
969
970 type SomeObject implements SomeInterface & AnotherInterface {
971 oldField: String
972 }
973
974 type Query {
975 someInterface: SomeInterface
976 }
977 """)
978 extend_ast = parse("""
979 interface NewInterface {
980 newField: String
981 }
982
983 extend interface AnotherInterface implements NewInterface {
984 newField: String
985 }
986
987 extend type SomeObject implements NewInterface {
988 newField: String
989 }
990 """)
991 extended_schema = extend_schema(schema, extend_ast)
992
993 assert validate_schema(extended_schema) == []
994 expect_schema_changes(
995 schema,
996 extended_schema,
997 dedent("""
998 interface AnotherInterface implements SomeInterface & NewInterface {
999 oldField: String
1000 newField: String
1001 }
1002
1003 type SomeObject implements SomeInterface & AnotherInterface & NewInterface {
1004 oldField: String
1005 newField: String
1006 }
1007
1008 interface NewInterface {
1009 newField: String
1010 }
1011 """), # noqa: E501
1012 )
1013
1014 def allows_extension_of_interface_with_missing_object_fields():
1015 schema = build_schema("""
1016 type Query {
1017 someInterface: SomeInterface
1018 }
1019
1020 type SomeObject implements SomeInterface {
1021 oldField: SomeInterface
1022 }
1023
1024 interface SomeInterface {
1025 oldField: SomeInterface
1026 }
1027 """)
1028 extend_ast = parse("""
1029 extend interface SomeInterface {
1030 newField: String
1031 }
1032 """)
1033 extended_schema = extend_schema(schema, extend_ast)
1034
1035 assert validate_schema(extended_schema)
1036 expect_schema_changes(
1037 schema,
1038 extended_schema,
1039 dedent("""
1040 interface SomeInterface {
1041 oldField: SomeInterface
1042 newField: String
1043 }
1044 """),
1045 )
1046
1047 def extends_interfaces_multiple_times():
1048 schema = build_schema("""
1049 type Query {
1050 someInterface: SomeInterface
1051 }
1052
1053 interface SomeInterface {
1054 some: SomeInterface
1055 }
1056 """)
1057 extend_ast = parse("""
1058 extend interface SomeInterface {
1059 newFieldA: Int
1060 }
1061
1062 extend interface SomeInterface {
1063 newFieldB(test: Boolean): String
1064 }
1065 """)
1066 extended_schema = extend_schema(schema, extend_ast)
1067
1068 assert validate_schema(extended_schema) == []
1069 expect_schema_changes(
1070 schema,
1071 extended_schema,
1072 dedent("""
1073 interface SomeInterface {
1074 some: SomeInterface
1075 newFieldA: Int
1076 newFieldB(test: Boolean): String
1077 }
1078 """),
1079 )
1080
1081 def may_extend_mutations_and_subscriptions():
1082 mutation_schema = build_schema("""
1083 type Query {
1084 queryField: String
1085 }
1086
1087 type Mutation {
1088 mutationField: String
1089 }
1090
1091 type Subscription {
1092 subscriptionField: String
1093 }
1094 """)
1095 ast = parse("""
1096 extend type Query {
1097 newQueryField: Int
1098 }
1099
1100 extend type Mutation {
1101 newMutationField: Int
1102 }
1103
1104 extend type Subscription {
1105 newSubscriptionField: Int
1106 }
1107 """)
1108 original_print = print_schema(mutation_schema)
1109 extended_schema = extend_schema(mutation_schema, ast)
1110 assert extended_schema != mutation_schema
1111 assert print_schema(mutation_schema) == original_print
1112 assert print_schema(extended_schema) == dedent("""
1113 type Query {
1114 queryField: String
1115 newQueryField: Int
1116 }
1117
1118 type Mutation {
1119 mutationField: String
1120 newMutationField: Int
1121 }
1122
1123 type Subscription {
1124 subscriptionField: String
1125 newSubscriptionField: Int
1126 }
1127 """)
1128
1129 def may_extend_directives_with_new_directive():
1130 schema = build_schema("""
1131 type Query {
1132 foo: String
1133 }
1134 """)
1135 extension_sdl = dedent('''
1136 """New directive."""
1137 directive @new(enable: Boolean!, tag: String) repeatable on QUERY | FIELD
1138 ''')
1139 extended_schema = extend_schema(schema, parse(extension_sdl))
1140
1141 assert validate_schema(extended_schema) == []
1142 expect_schema_changes(schema, extended_schema, extension_sdl)
1143
1144 def rejects_invalid_sdl():
1145 schema = GraphQLSchema()
1146 extend_ast = parse("extend schema @unknown")
1147
1148 with raises(TypeError) as exc_info:
1149 extend_schema(schema, extend_ast)
1150 assert str(exc_info.value) == "Unknown directive '@unknown'."
1151
1152 def allows_to_disable_sdl_validation():
1153 schema = GraphQLSchema()
1154 extend_ast = parse("extend schema @unknown")
1155
1156 extend_schema(schema, extend_ast, assume_valid=True)
1157 extend_schema(schema, extend_ast, assume_valid_sdl=True)
1158
1159 def throws_on_unknown_types():
1160 schema = GraphQLSchema()
1161 ast = parse("""
1162 type Query {
1163 unknown: UnknownType
1164 }
1165 """)
1166 with raises(TypeError) as exc_info:
1167 extend_schema(schema, ast, assume_valid_sdl=True)
1168 assert str(exc_info.value).endswith("Unknown type: 'UnknownType'.")
1169
1170 def rejects_invalid_ast():
1171 schema = GraphQLSchema()
1172
1173 with raises(TypeError) as exc_info:
1174 # noinspection PyTypeChecker
1175 extend_schema(schema, None) # type: ignore
1176 assert str(exc_info.value) == "Must provide valid Document AST."
1177
1178 with raises(TypeError) as exc_info:
1179 # noinspection PyTypeChecker
1180 extend_schema(schema, {}) # type: ignore
1181 assert str(exc_info.value) == "Must provide valid Document AST."
1182
1183 def does_not_allow_replacing_a_default_directive():
1184 schema = GraphQLSchema()
1185 extend_ast = parse("""
1186 directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD
1187 """)
1188
1189 with raises(TypeError) as exc_info:
1190 extend_schema(schema, extend_ast)
1191 assert str(exc_info.value).startswith(
1192 "Directive '@include' already exists in the schema."
1193 " It cannot be redefined."
1194 )
1195
1196 def does_not_allow_replacing_an_existing_enum_value():
1197 schema = build_schema("""
1198 enum SomeEnum {
1199 ONE
1200 }
1201 """)
1202 extend_ast = parse("""
1203 extend enum SomeEnum {
1204 ONE
1205 }
1206 """)
1207
1208 with raises(TypeError) as exc_info:
1209 extend_schema(schema, extend_ast)
1210 assert str(exc_info.value).startswith(
1211 "Enum value 'SomeEnum.ONE' already exists in the schema."
1212 " It cannot also be defined in this type extension."
1213 )
1214
1215 def describe_can_add_additional_root_operation_types():
1216 def does_not_automatically_include_common_root_type_names():
1217 schema = GraphQLSchema()
1218 extended_schema = extend_schema(schema, parse("type Mutation"))
1219
1220 assert extended_schema.get_type("Mutation")
1221 assert extended_schema.mutation_type is None
1222
1223 def adds_schema_definition_missing_in_the_original_schema():
1224 schema = build_schema("""
1225 directive @foo on SCHEMA
1226 type Foo
1227 """)
1228 assert schema.query_type is None
1229
1230 extension_sdl = dedent("""
1231 schema @foo {
1232 query: Foo
1233 }
1234 """)
1235 extended_schema = extend_schema(schema, parse(extension_sdl))
1236
1237 query_type = assert_object_type(extended_schema.query_type)
1238 assert query_type.name == "Foo"
1239 expect_ast_node(extended_schema, extension_sdl)
1240
1241 def adds_new_root_types_via_schema_extension():
1242 schema = build_schema("""
1243 type Query
1244 type MutationRoot
1245 """)
1246 extension_sdl = dedent("""
1247 extend schema {
1248 mutation: MutationRoot
1249 }
1250 """)
1251 extended_schema = extend_schema(schema, parse(extension_sdl))
1252
1253 mutation_type = assert_object_type(extended_schema.mutation_type)
1254 assert mutation_type.name == "MutationRoot"
1255 expect_extension_ast_nodes(extended_schema, extension_sdl)
1256
1257 def adds_directive_via_schema_extension():
1258 schema = build_schema("""
1259 type Query
1260
1261 directive @foo on SCHEMA
1262 """)
1263 extension_sdl = dedent("""
1264 extend schema @foo
1265 """)
1266 extended_schema = extend_schema(schema, parse(extension_sdl))
1267
1268 expect_extension_ast_nodes(extended_schema, extension_sdl)
1269
1270 def adds_multiple_new_root_types_via_schema_extension():
1271 schema = build_schema("type Query")
1272 extend_ast = parse("""
1273 extend schema {
1274 mutation: Mutation
1275 subscription: Subscription
1276 }
1277
1278 type Mutation
1279 type Subscription
1280 """)
1281 extended_schema = extend_schema(schema, extend_ast)
1282
1283 mutation_type = assert_object_type(extended_schema.mutation_type)
1284 assert mutation_type.name == "Mutation"
1285
1286 subscription_type = assert_object_type(extended_schema.subscription_type)
1287 assert subscription_type.name == "Subscription"
1288
1289 def applies_multiple_schema_extensions():
1290 schema = build_schema("type Query")
1291 extend_ast = parse("""
1292 extend schema {
1293 mutation: Mutation
1294 }
1295 type Mutation
1296
1297 extend schema {
1298 subscription: Subscription
1299 }
1300 type Subscription
1301 """)
1302 extended_schema = extend_schema(schema, extend_ast)
1303
1304 mutation_type = assert_object_type(extended_schema.mutation_type)
1305 assert mutation_type.name == "Mutation"
1306
1307 subscription_type = assert_object_type(extended_schema.subscription_type)
1308 assert subscription_type.name == "Subscription"
1309
1310 def schema_extension_ast_are_available_from_schema_object():
1311 schema = build_schema("""
1312 type Query
1313
1314 directive @foo on SCHEMA
1315 """)
1316 extend_ast = parse("""
1317 extend schema {
1318 mutation: Mutation
1319 }
1320 type Mutation
1321
1322 extend schema {
1323 subscription: Subscription
1324 }
1325 type Subscription
1326 """)
1327 extended_schema = extend_schema(schema, extend_ast)
1328
1329 second_extend_ast = parse("extend schema @foo")
1330 extended_twice_schema = extend_schema(extended_schema, second_extend_ast)
1331
1332 expect_extension_ast_nodes(
1333 extended_twice_schema,
1334 dedent("""
1335 extend schema {
1336 mutation: Mutation
1337 }
1338
1339 extend schema {
1340 subscription: Subscription
1341 }
1342
1343 extend schema @foo
1344 """),
1345 )
1346
1347 def extend_directive_to_make_it_deprecated():
1348 schema = build_schema("directive @isDeprecated on FIELD_DEFINITION")
1349 extend_ast = parse(
1350 """
1351 extend directive @isDeprecated @deprecated(reason: "use another directive")
1352 """,
1353 experimental_directives_on_directive_definitions=True,
1354 )
1355 extended_schema = extend_schema(schema, extend_ast)
1356
1357 is_deprecated_directive = assert_directive(
1358 extended_schema.get_directive("isDeprecated")
1359 )
1360 assert is_deprecated_directive.deprecation_reason == "use another directive"
1361
1362 def preserves_deprecated_directives_when_extending_other_types():
1363 schema = build_ast_schema(
1364 parse(
1365 """
1366 type Query {
1367 foo: String
1368 }
1369
1370 directive @isDeprecated @deprecated(reason: "use another directive") on FIELD_DEFINITION
1371 """,
1372 experimental_directives_on_directive_definitions=True,
1373 )
1374 )
1375 extend_ast = parse(dedent("""
1376 extend type Query {
1377 bar: Int
1378 }
1379 """))
1380 extended_schema = extend_schema(schema, extend_ast)
1381
1382 is_deprecated_directive = assert_directive(
1383 extended_schema.get_directive("isDeprecated")
1384 )
1385 assert is_deprecated_directive.deprecation_reason == "use another directive"
1386
1387 def applies_directive_extensions_defined_in_the_same_document():
1388 schema = build_ast_schema(
1389 parse(
1390 dedent("""
1391 directive @onDirective on DIRECTIVE_DEFINITION
1392 directive @someDirective on FIELD_DEFINITION
1393
1394 extend directive @someDirective @onDirective
1395 """),
1396 experimental_directives_on_directive_definitions=True,
1397 )
1398 )
1399
1400 some_directive = assert_directive(schema.get_directive("someDirective"))
1401 expect_extension_ast_nodes(
1402 some_directive, "extend directive @someDirective @onDirective"
1403 )
1404
1405 def applies_multiple_directive_extensions_defined_in_the_same_document():
1406 schema = build_ast_schema(
1407 parse(
1408 dedent("""
1409 directive @onDirective on DIRECTIVE_DEFINITION
1410 directive @otherDirective on DIRECTIVE_DEFINITION
1411 directive @someDirective on FIELD_DEFINITION
1412
1413 extend directive @someDirective @onDirective
1414 extend directive @someDirective @otherDirective
1415 """),
1416 experimental_directives_on_directive_definitions=True,
1417 )
1418 )
1419
1420 some_directive = assert_directive(schema.get_directive("someDirective"))
1421 expect_extension_ast_nodes(
1422 some_directive,
1423 dedent("""
1424 extend directive @someDirective @onDirective
1425
1426 extend directive @someDirective @otherDirective
1427 """),
1428 )
1429
1430 def extend_directive_without_adding_new_directives_is_an_error():
1431 with raises(GraphQLSyntaxError) as exc_info:
1432 parse(
1433 "extend directive @isDeprecated",
1434 experimental_directives_on_directive_definitions=True,
1435 )
1436 assert str(exc_info.value).startswith("Syntax Error: Unexpected <EOF>.")