Debian Codegraph @

tests/validation/test_overlapping_fields_can_be_merged.py

raw
1 from contextlib import contextmanager
2 from functools import partial
3
4 from graphql.utilities import build_schema
5 from graphql.validation import OverlappingFieldsCanBeMergedRule
6 from graphql.validation.rules import (
7 overlapping_fields_can_be_merged as overlapping_module,
8 )
9
10 from .harness import assert_validation_errors
11
12 assert_errors = partial(assert_validation_errors, OverlappingFieldsCanBeMergedRule)
13
14 assert_valid = partial(assert_errors, errors=[])
15
16 budget_schema = build_schema("""
17 type Query {
18 field: Node
19 other: Node
20 }
21
22 type Node {
23 f: Node
24 x: String
25 }
26 """)
27
28
29 @contextmanager
30 def budget_of(limit):
31 # Temporarily lower the module-level comparison budget so the budget tests can
32 # trigger the guard with small, fast queries.
33 saved = overlapping_module.MAX_FIELD_COMPARISONS
34 overlapping_module.MAX_FIELD_COMPARISONS = limit
35 try:
36 yield
37 finally:
38 overlapping_module.MAX_FIELD_COMPARISONS = saved
39
40
41 def describe_validate_overlapping_fields_can_be_merged():
42 def unique_fields():
43 assert_valid("""
44 fragment uniqueFields on Dog {
45 name
46 nickname
47 }
48 """)
49
50 def identical_fields():
51 assert_valid("""
52 fragment mergeIdenticalFields on Dog {
53 name
54 name
55 }
56 """)
57
58 def identical_fields_with_identical_args():
59 assert_valid("""
60 fragment mergeIdenticalFieldsWithIdenticalArgs on Dog {
61 doesKnowCommand(dogCommand: SIT)
62 doesKnowCommand(dogCommand: SIT)
63 }
64 """)
65
66 def identical_fields_with_identical_directives():
67 assert_valid("""
68 fragment mergeSameFieldsWithSameDirectives on Dog {
69 name @include(if: true)
70 name @include(if: true)
71 }
72 """)
73
74 def different_args_with_different_aliases():
75 assert_valid("""
76 fragment differentArgsWithDifferentAliases on Dog {
77 knowsSit: doesKnowCommand(dogCommand: SIT)
78 knowsDown: doesKnowCommand(dogCommand: DOWN)
79 }
80 """)
81
82 def different_directives_with_different_aliases():
83 assert_valid("""
84 fragment differentDirectivesWithDifferentAliases on Dog {
85 nameIfTrue: name @include(if: true)
86 nameIfFalse: name @include(if: false)
87 }
88 """)
89
90 def different_skip_or_include_directives_accepted():
91 # Note: Differing skip/include directives don't create an ambiguous
92 # return value and are acceptable in conditions where differing runtime
93 # values may have the same desired effect of including/skipping a field
94 assert_valid("""
95 fragment differentDirectivesWithDifferentAliases on Dog {
96 name @include(if: true)
97 name @include(if: false)
98 }
99 """)
100
101 def same_aliases_with_different_field_targets():
102 assert_errors(
103 """
104 fragment sameAliasesWithDifferentFieldTargets on Dog {
105 fido: name
106 fido: nickname
107 }
108 """,
109 [
110 {
111 "message": "Fields 'fido' conflict"
112 " because 'name' and 'nickname' are different fields."
113 " Use different aliases on the fields"
114 " to fetch both if this was intentional.",
115 "locations": [(3, 15), (4, 15)],
116 "path": None,
117 }
118 ],
119 )
120
121 def same_aliases_allowed_on_non_overlapping_fields():
122 assert_valid("""
123 fragment sameAliasesWithDifferentFieldTargets on Pet {
124 ... on Dog {
125 name
126 }
127 ... on Cat {
128 name: nickname
129 }
130 }
131 """)
132
133 def alias_masking_direct_field_access():
134 assert_errors(
135 """
136 fragment aliasMaskingDirectFieldAccess on Dog {
137 name: nickname
138 name
139 }
140 """,
141 [
142 {
143 "message": "Fields 'name' conflict"
144 " because 'nickname' and 'name' are different fields."
145 " Use different aliases on the fields"
146 " to fetch both if this was intentional.",
147 "locations": [(3, 15), (4, 15)],
148 }
149 ],
150 )
151
152 def different_args_second_adds_an_argument():
153 assert_errors(
154 """
155 fragment conflictingArgs on Dog {
156 doesKnowCommand
157 doesKnowCommand(dogCommand: HEEL)
158 }
159 """,
160 [
161 {
162 "message": "Fields 'doesKnowCommand' conflict"
163 " because they have differing arguments."
164 " Use different aliases on the fields"
165 " to fetch both if this was intentional.",
166 "locations": [(3, 15), (4, 15)],
167 }
168 ],
169 )
170
171 def different_args_second_missing_an_argument():
172 assert_errors(
173 """
174 fragment conflictingArgs on Dog {
175 doesKnowCommand(dogCommand: SIT)
176 doesKnowCommand
177 }
178 """,
179 [
180 {
181 "message": "Fields 'doesKnowCommand' conflict"
182 " because they have differing arguments."
183 " Use different aliases on the fields"
184 " to fetch both if this was intentional.",
185 "locations": [(3, 15), (4, 15)],
186 }
187 ],
188 )
189
190 def conflicting_arg_values():
191 assert_errors(
192 """
193 fragment conflictingArgs on Dog {
194 doesKnowCommand(dogCommand: SIT)
195 doesKnowCommand(dogCommand: HEEL)
196 }
197 """,
198 [
199 {
200 "message": "Fields 'doesKnowCommand' conflict"
201 " because they have differing arguments."
202 " Use different aliases on the fields"
203 " to fetch both if this was intentional.",
204 "locations": [(3, 15), (4, 15)],
205 }
206 ],
207 )
208
209 def conflicting_arg_names():
210 assert_errors(
211 """
212 fragment conflictingArgs on Dog {
213 isAtLocation(x: 0)
214 isAtLocation(y: 0)
215 }
216 """,
217 [
218 {
219 "message": "Fields 'isAtLocation' conflict"
220 " because they have differing arguments."
221 " Use different aliases on the fields"
222 " to fetch both if this was intentional.",
223 "locations": [(3, 15), (4, 15)],
224 }
225 ],
226 )
227
228 def allows_different_args_where_no_conflict_is_possible():
229 # This is valid since no object can be both a "Dog" and a "Cat", thus
230 # these fields can never overlap.
231 assert_valid("""
232 fragment conflictingArgs on Pet {
233 ... on Dog {
234 name(surname: true)
235 }
236 ... on Cat {
237 name
238 }
239 }
240 """)
241
242 def allows_different_order_of_args():
243 schema = build_schema("""
244 type Query {
245 someField(a: String, b: String): String
246 }
247 """)
248 # This is valid since arguments are unordered, see:
249 # https://spec.graphql.org/draft/#
250 # sec-Language.Arguments.Arguments-are-unordered
251 assert_valid(
252 """
253 {
254 someField(a: null, b: null)
255 someField(b: null, a: null)
256 }
257 """,
258 schema=schema,
259 )
260
261 def allows_different_order_of_input_object_fields_in_arg_values():
262 schema = build_schema("""
263 input SomeInput {
264 a: String
265 b: String
266 }
267
268 type Query {
269 someField(arg: SomeInput): String
270 }
271 """)
272 # This is valid since input object fields are unordered, see:
273 # https://spec.graphql.org/draft/#
274 # sec-Input-Object-Values.Input-object-fields-are-unordered
275 assert_valid(
276 """
277 {
278 someField(arg: { a: null, b: null })
279 someField(arg: { b: null, a: null })
280 }
281 """,
282 schema=schema,
283 )
284
285 def encounters_conflict_in_fragments():
286 assert_errors(
287 """
288 {
289 ...A
290 ...B
291 }
292 fragment A on Type {
293 x: a
294 }
295 fragment B on Type {
296 x: b
297 }
298 """,
299 [
300 {
301 "message": "Fields 'x' conflict"
302 " because 'a' and 'b' are different fields."
303 " Use different aliases on the fields"
304 " to fetch both if this was intentional.",
305 "locations": [(7, 15), (10, 15)],
306 }
307 ],
308 )
309
310 def reports_each_conflict_once():
311 assert_errors(
312 """
313 {
314 f1 {
315 ...A
316 ...B
317 }
318 f2 {
319 ...B
320 ...A
321 }
322 f3 {
323 ...A
324 ...B
325 x: c
326 }
327 }
328 fragment A on Type {
329 x: a
330 }
331 fragment B on Type {
332 x: b
333 }
334 """,
335 [
336 {
337 "message": "Fields 'x' conflict"
338 " because 'a' and 'b' are different fields."
339 " Use different aliases on the fields"
340 " to fetch both if this was intentional.",
341 "locations": [(18, 15), (21, 15)],
342 },
343 {
344 "message": "Fields 'x' conflict"
345 " because 'c' and 'a' are different fields."
346 " Use different aliases on the fields"
347 " to fetch both if this was intentional.",
348 "locations": [(14, 17), (18, 15)],
349 },
350 {
351 "message": "Fields 'x' conflict"
352 " because 'c' and 'b' are different fields."
353 " Use different aliases on the fields"
354 " to fetch both if this was intentional.",
355 "locations": [(14, 17), (21, 15)],
356 },
357 ],
358 )
359
360 def deep_conflict():
361 assert_errors(
362 """
363 {
364 field {
365 x: a
366 },
367 field {
368 x: b
369 }
370 }
371 """,
372 [
373 {
374 "message": "Fields 'field' conflict"
375 " because subfields 'x' conflict"
376 " because 'a' and 'b' are different fields."
377 " Use different aliases on the fields"
378 " to fetch both if this was intentional.",
379 "locations": [(3, 15), (4, 17), (6, 15), (7, 17)],
380 }
381 ],
382 )
383
384 def deep_conflict_with_multiple_issues():
385 assert_errors(
386 """
387 {
388 field {
389 x: a
390 y: c
391 },
392 field {
393 x: b
394 y: d
395 }
396 }
397 """,
398 [
399 {
400 "message": "Fields 'field' conflict"
401 " because subfields 'x' conflict"
402 " because 'a' and 'b' are different fields"
403 " and subfields 'y' conflict"
404 " because 'c' and 'd' are different fields."
405 " Use different aliases on the fields"
406 " to fetch both if this was intentional.",
407 "locations": [(3, 15), (4, 17), (5, 17), (7, 15), (8, 17), (9, 17)],
408 "path": None,
409 }
410 ],
411 )
412
413 def very_deep_conflict():
414 assert_errors(
415 """
416 {
417 field {
418 deepField {
419 x: a
420 }
421 },
422 field {
423 deepField {
424 x: b
425 }
426 }
427 }
428 """,
429 [
430 {
431 "message": "Fields 'field' conflict"
432 " because subfields 'deepField' conflict"
433 " because subfields 'x' conflict"
434 " because 'a' and 'b' are different fields."
435 " Use different aliases on the fields"
436 " to fetch both if this was intentional.",
437 "locations": [
438 (3, 15),
439 (4, 17),
440 (5, 19),
441 (8, 15),
442 (9, 17),
443 (10, 19),
444 ],
445 "path": None,
446 }
447 ],
448 )
449
450 def reports_deep_conflict_to_nearest_common_ancestor():
451 assert_errors(
452 """
453 {
454 field {
455 deepField {
456 x: a
457 }
458 deepField {
459 x: b
460 }
461 },
462 field {
463 deepField {
464 y
465 }
466 }
467 }
468 """,
469 [
470 {
471 "message": "Fields 'deepField' conflict"
472 " because subfields 'x' conflict"
473 " because 'a' and 'b' are different fields."
474 " Use different aliases on the fields"
475 " to fetch both if this was intentional.",
476 "locations": [(4, 17), (5, 19), (7, 17), (8, 19)],
477 }
478 ],
479 )
480
481 def reports_deep_conflict_to_nearest_common_ancestor_in_fragments():
482 assert_errors(
483 """
484 {
485 field {
486 ...F
487 }
488 field {
489 ...F
490 }
491 }
492 fragment F on T {
493 deepField {
494 deeperField {
495 x: a
496 }
497 deeperField {
498 x: b
499 }
500 },
501 deepField {
502 deeperField {
503 y
504 }
505 }
506 }
507 """,
508 [
509 {
510 "message": "Fields 'deeperField' conflict"
511 " because subfields 'x' conflict"
512 " because 'a' and 'b' are different fields."
513 " Use different aliases on the fields"
514 " to fetch both if this was intentional.",
515 "locations": [(12, 17), (13, 19), (15, 17), (16, 19)],
516 }
517 ],
518 )
519
520 def reports_deep_conflict_in_nested_fragments():
521 assert_errors(
522 """
523 {
524 field {
525 ...F
526 },
527 field {
528 ...I
529 }
530 }
531 fragment F on T {
532 x: a
533 ...G
534 }
535 fragment G on T {
536 y: c
537 }
538 fragment I on T {
539 y: d
540 ...J
541 }
542 fragment J on T {
543 x: b
544 }
545 """,
546 [
547 {
548 "message": "Fields 'field' conflict"
549 " because subfields 'x' conflict"
550 " because 'a' and 'b' are different fields"
551 " and subfields 'y' conflict"
552 " because 'c' and 'd' are different fields."
553 " Use different aliases on the fields"
554 " to fetch both if this was intentional.",
555 "locations": [
556 (3, 15),
557 (11, 15),
558 (15, 15),
559 (6, 15),
560 (22, 15),
561 (18, 15),
562 ],
563 "path": None,
564 }
565 ],
566 )
567
568 def reports_deep_conflict_after_nested_fragments():
569 assert_errors(
570 """
571 fragment F on T {
572 ...G
573 }
574 fragment G on T {
575 ...H
576 }
577 fragment H on T {
578 x: a
579 }
580 {
581 x: b
582 ...F
583 }
584 """,
585 [
586 {
587 "message": "Fields 'x' conflict"
588 " because 'b' and 'a' are different fields."
589 " Use different aliases on the fields"
590 " to fetch both if this was intentional.",
591 "locations": [(12, 15), (9, 15)],
592 "path": None,
593 }
594 ],
595 )
596
597 def ignores_unknown_fragments():
598 assert_valid("""
599 {
600 field
601 ...Unknown
602 ...Known
603 }
604
605 fragment Known on T {
606 field
607 ...OtherUnknown
608 }
609 """)
610
611 def describe_return_types_must_be_unambiguous():
612
613 schema = build_schema("""
614 interface SomeBox {
615 deepBox: SomeBox
616 unrelatedField: String
617 }
618
619 type StringBox implements SomeBox {
620 scalar: String
621 deepBox: StringBox
622 unrelatedField: String
623 listStringBox: [StringBox]
624 stringBox: StringBox
625 intBox: IntBox
626 }
627
628 type IntBox implements SomeBox {
629 scalar: Int
630 deepBox: IntBox
631 unrelatedField: String
632 listStringBox: [StringBox]
633 stringBox: StringBox
634 intBox: IntBox
635 }
636
637 interface NonNullStringBox1 {
638 scalar: String!
639 }
640
641 type NonNullStringBox1Impl implements SomeBox & NonNullStringBox1 {
642 scalar: String!
643 unrelatedField: String
644 deepBox: SomeBox
645 }
646
647 interface NonNullStringBox2 {
648 scalar: String!
649 }
650
651 type NonNullStringBox2Impl implements SomeBox & NonNullStringBox2 {
652 scalar: String!
653 unrelatedField: String
654 deepBox: SomeBox
655 }
656
657 type Connection {
658 edges: [Edge]
659 }
660
661 type Edge {
662 node: Node
663 }
664
665 type Node {
666 id: ID
667 name: String
668 }
669
670 type Query {
671 someBox: SomeBox
672 connection: Connection
673 }
674 """)
675
676 def conflicting_return_types_which_potentially_overlap():
677 # This is invalid since an object could potentially be both the
678 # Object type IntBox and the interface type NonNullStringBox1.
679 # While that condition does not exist in the current schema, the
680 # schema could expand in the future to allow this.
681 assert_errors(
682 """
683 {
684 someBox {
685 ...on IntBox {
686 scalar
687 }
688 ...on NonNullStringBox1 {
689 scalar
690 }
691 }
692 }
693 """,
694 [
695 {
696 "message": "Fields 'scalar' conflict because"
697 " they return conflicting types 'Int' and 'String!'."
698 " Use different aliases on the fields"
699 " to fetch both if this was intentional.",
700 "locations": [(5, 23), (8, 23)],
701 }
702 ],
703 schema,
704 )
705
706 def compatible_return_shapes_on_different_return_types():
707 # In this case `deepBox` returns `SomeBox` in the first usage, and
708 # `StringBox` in the second usage. These types are not the same!
709 # However this is valid because the return *shapes* are compatible.
710 assert_valid(
711 """
712 {
713 someBox {
714 ... on SomeBox {
715 deepBox {
716 unrelatedField
717 }
718 }
719 ... on StringBox {
720 deepBox {
721 unrelatedField
722 }
723 }
724 }
725 }
726 """,
727 schema=schema,
728 )
729
730 def disallows_differing_return_types_despite_no_overlap():
731 assert_errors(
732 """
733 {
734 someBox {
735 ... on IntBox {
736 scalar
737 }
738 ... on StringBox {
739 scalar
740 }
741 }
742 }
743 """,
744 [
745 {
746 "message": "Fields 'scalar' conflict because"
747 " they return conflicting types 'Int' and 'String'."
748 " Use different aliases on the fields"
749 " to fetch both if this was intentional.",
750 "locations": [(5, 23), (8, 23)],
751 }
752 ],
753 schema,
754 )
755
756 def reports_correctly_when_a_non_exclusive_follows_an_exclusive():
757 assert_errors(
758 """
759 {
760 someBox {
761 ... on IntBox {
762 deepBox {
763 ...X
764 }
765 }
766 }
767 someBox {
768 ... on StringBox {
769 deepBox {
770 ...Y
771 }
772 }
773 }
774 memoed: someBox {
775 ... on IntBox {
776 deepBox {
777 ...X
778 }
779 }
780 }
781 memoed: someBox {
782 ... on StringBox {
783 deepBox {
784 ...Y
785 }
786 }
787 }
788 other: someBox {
789 ...X
790 }
791 other: someBox {
792 ...Y
793 }
794 }
795 fragment X on SomeBox {
796 scalar
797 }
798 fragment Y on SomeBox {
799 scalar: unrelatedField
800 }
801 """,
802 [
803 {
804 "message": "Fields 'other' conflict because"
805 " subfields 'scalar' conflict because"
806 " 'scalar' and 'unrelatedField' are different fields."
807 " Use different aliases on the fields"
808 " to fetch both if this was intentional.",
809 "locations": [(31, 19), (39, 19), (34, 19), (42, 19)],
810 "path": None,
811 }
812 ],
813 schema,
814 )
815
816 def disallows_differing_return_type_nullability_despite_no_overlap():
817 assert_errors(
818 """
819 {
820 someBox {
821 ... on NonNullStringBox1 {
822 scalar
823 }
824 ... on StringBox {
825 scalar
826 }
827 }
828 }
829 """,
830 [
831 {
832 "message": "Fields 'scalar' conflict because"
833 " they return conflicting types 'String!' and 'String'."
834 " Use different aliases on the fields"
835 " to fetch both if this was intentional.",
836 "locations": [(5, 23), (8, 23)],
837 }
838 ],
839 schema,
840 )
841
842 def disallows_differing_return_type_list_despite_no_overlap_1():
843 assert_errors(
844 """
845 {
846 someBox {
847 ... on IntBox {
848 box: listStringBox {
849 scalar
850 }
851 }
852 ... on StringBox {
853 box: stringBox {
854 scalar
855 }
856 }
857 }
858 }
859 """,
860 [
861 {
862 "message": "Fields 'box' conflict because they return"
863 " conflicting types '[StringBox]' and 'StringBox'."
864 " Use different aliases on the fields"
865 " to fetch both if this was intentional.",
866 "locations": [(5, 23), (10, 23)],
867 }
868 ],
869 schema,
870 )
871
872 assert_errors(
873 """
874 {
875 someBox {
876 ... on IntBox {
877 box: stringBox {
878 scalar
879 }
880 }
881 ... on StringBox {
882 box: listStringBox {
883 scalar
884 }
885 }
886 }
887 }
888 """,
889 [
890 {
891 "message": "Fields 'box' conflict because they return"
892 " conflicting types 'StringBox' and '[StringBox]'."
893 " Use different aliases on the fields"
894 " to fetch both if this was intentional.",
895 "locations": [(5, 23), (10, 23)],
896 }
897 ],
898 schema,
899 )
900
901 def disallows_differing_subfields():
902 assert_errors(
903 """
904 {
905 someBox {
906 ... on IntBox {
907 box: stringBox {
908 val: scalar
909 val: unrelatedField
910 }
911 }
912 ... on StringBox {
913 box: stringBox {
914 val: scalar
915 }
916 }
917 }
918 }
919 """,
920 [
921 {
922 "message": "Fields 'val' conflict because"
923 " 'scalar' and 'unrelatedField' are different fields."
924 " Use different aliases on the fields"
925 " to fetch both if this was intentional.",
926 "locations": [(6, 25), (7, 25)],
927 }
928 ],
929 schema,
930 )
931
932 def disallows_differing_deep_return_types_despite_no_overlap():
933 assert_errors(
934 """
935 {
936 someBox {
937 ... on IntBox {
938 box: stringBox {
939 scalar
940 }
941 }
942 ... on StringBox {
943 box: intBox {
944 scalar
945 }
946 }
947 }
948 }
949 """,
950 [
951 {
952 "message": "Fields 'box' conflict"
953 " because subfields 'scalar' conflict"
954 " because they return conflicting types 'String' and 'Int'."
955 " Use different aliases on the fields"
956 " to fetch both if this was intentional.",
957 "locations": [(5, 23), (6, 25), (10, 23), (11, 25)],
958 "path": None,
959 }
960 ],
961 schema,
962 )
963
964 def allows_non_conflicting_overlapping_types():
965 assert_valid(
966 """
967 {
968 someBox {
969 ... on IntBox {
970 scalar: unrelatedField
971 }
972 ... on StringBox {
973 scalar
974 }
975 }
976 }
977 """,
978 schema=schema,
979 )
980
981 def same_wrapped_scalar_return_types():
982 assert_valid(
983 """
984 {
985 someBox {
986 ...on NonNullStringBox1 {
987 scalar
988 }
989 ...on NonNullStringBox2 {
990 scalar
991 }
992 }
993 }
994 """,
995 schema=schema,
996 )
997
998 def allows_inline_fragments_without_type_condition():
999 assert_valid(
1000 """
1001 {
1002 a
1003 ... {
1004 a
1005 }
1006 }
1007 """,
1008 schema=schema,
1009 )
1010
1011 def compares_deep_types_including_list():
1012 assert_errors(
1013 """
1014 {
1015 connection {
1016 ...edgeID
1017 edges {
1018 node {
1019 id: name
1020 }
1021 }
1022 }
1023 }
1024
1025 fragment edgeID on Connection {
1026 edges {
1027 node {
1028 id
1029 }
1030 }
1031 }
1032 """,
1033 [
1034 {
1035 "message": "Fields 'edges' conflict"
1036 " because subfields 'node' conflict"
1037 " because subfields 'id' conflict"
1038 " because 'name' and 'id' are different fields."
1039 " Use different aliases on the fields"
1040 " to fetch both if this was intentional.",
1041 "locations": [
1042 (5, 21),
1043 (6, 23),
1044 (7, 25),
1045 (14, 19),
1046 (15, 21),
1047 (16, 23),
1048 ],
1049 "path": None,
1050 }
1051 ],
1052 schema,
1053 )
1054
1055 def ignores_unknown_types():
1056 assert_valid(
1057 """
1058 {
1059 someBox {
1060 ...on UnknownType {
1061 scalar
1062 }
1063 ...on NonNullStringBox2 {
1064 scalar
1065 }
1066 }
1067 }
1068 """,
1069 schema=schema,
1070 )
1071
1072 def works_for_field_names_that_are_js_keywords():
1073 schema_with_keywords = build_schema("""
1074 type Foo {
1075 constructor: String
1076 }
1077
1078 type Query {
1079 foo: Foo
1080 }
1081 """)
1082
1083 assert_valid(
1084 """
1085 {
1086 foo {
1087 constructor
1088 }
1089 }
1090 """,
1091 schema=schema_with_keywords,
1092 )
1093
1094 def works_for_field_names_that_are_python_keywords():
1095 schema_with_keywords = build_schema("""
1096 type Foo {
1097 class: String
1098 }
1099
1100 type Query {
1101 foo: Foo
1102 }
1103 """)
1104
1105 assert_valid(
1106 """
1107 {
1108 foo {
1109 class
1110 }
1111 }
1112 """,
1113 schema=schema_with_keywords,
1114 )
1115
1116 def does_not_infinite_loop_on_recursive_fragments():
1117 assert_valid("""
1118 {
1119 ...fragA
1120 }
1121
1122 fragment fragA on Human { name, relatives { name, ...fragA } }
1123 """)
1124
1125 def does_not_infinite_loop_on_immediately_recursive_fragments():
1126 assert_valid("""
1127 {
1128 ...fragA
1129 }
1130
1131 fragment fragA on Human { name, ...fragA }
1132 """)
1133
1134 def does_not_infinite_loop_on_recursive_fragment_with_field_named_after_fragment():
1135 assert_valid("""
1136 {
1137 ...fragA
1138 fragA
1139 }
1140
1141 fragment fragA on Query { ...fragA }
1142 """)
1143
1144 def finds_invalid_cases_even_with_field_named_after_fragment():
1145 assert_errors(
1146 """
1147 {
1148 fragA
1149 ...fragA
1150 }
1151
1152 fragment fragA on Type {
1153 fragA: b
1154 }
1155 """,
1156 [
1157 {
1158 "message": "Fields 'fragA' conflict"
1159 " because 'fragA' and 'b' are different fields."
1160 " Use different aliases on the fields"
1161 " to fetch both if this was intentional.",
1162 "locations": [(3, 15), (8, 15)],
1163 }
1164 ],
1165 )
1166
1167 def does_not_infinite_loop_on_transitively_recursive_fragments():
1168 assert_valid("""
1169 {
1170 ...fragA
1171 fragB
1172 }
1173
1174 fragment fragA on Human { name, ...fragB }
1175 fragment fragB on Human { name, ...fragC }
1176 fragment fragC on Human { name, ...fragA }
1177 """)
1178
1179 def finds_invalid_case_even_with_immediately_recursive_fragment():
1180 assert_errors(
1181 """
1182 fragment sameAliasesWithDifferentFieldTargets on Dog {
1183 ...sameAliasesWithDifferentFieldTargets
1184 fido: name
1185 fido: nickname
1186 }
1187 """,
1188 [
1189 {
1190 "message": "Fields 'fido' conflict"
1191 " because 'name' and 'nickname' are different fields."
1192 " Use different aliases on the fields"
1193 " to fetch both if this was intentional.",
1194 "locations": [(4, 15), (5, 15)],
1195 }
1196 ],
1197 )
1198
1199 def does_not_infinite_loop_on_recursive_fragments_separated_by_fields():
1200 assert_valid("""
1201 {
1202 ...fragA
1203 ...fragB
1204 }
1205
1206 fragment fragA on T {
1207 x {
1208 ...fragA
1209 x {
1210 ...fragA
1211 }
1212 }
1213 }
1214
1215 fragment fragB on T {
1216 x {
1217 ...fragB
1218 x {
1219 ...fragB
1220 }
1221 }
1222 }
1223 """)
1224
1225 def describe_comparison_budget():
1226 def aborts_validation_when_the_comparison_budget_is_exceeded():
1227 # Repeated inline fragments select the same response name, forcing O(n^2)
1228 # pairwise comparisons. Once the budget is exhausted the rule aborts with
1229 # a single error instead of running the comparison to completion. The
1230 # trailing `other` selection set is visited after the abort and must be
1231 # skipped, so exactly one error is reported for the whole document.
1232 query = (
1233 "{ field { " + " ".join(["... on Node { x }"] * 10) + " } other { x } }"
1234 )
1235 with budget_of(5):
1236 assert_errors(
1237 query,
1238 [
1239 {
1240 "message": "Query is too complex to validate for"
1241 " overlapping fields: exceeded 5 field comparisons."
1242 " Validation was aborted."
1243 }
1244 ],
1245 schema=budget_schema,
1246 )
1247
1248 def deduplication_alone_would_not_bound_this_query():
1249 # Each `f` selection shares the response name `f` but wraps a distinct
1250 # sub-selection (`a0: x`, `a1: x`, ...), so every field has a distinct
1251 # structural fingerprint and none conflicts with another (differing
1252 # aliases merge cleanly). A fix based purely on deduplicating structurally
1253 # identical fields cannot remove any of these, so it would still perform
1254 # O(n^2) comparisons; the comparison budget bounds the work regardless.
1255 query = (
1256 "{ field { "
1257 + " ".join(f"... on Node {{ f {{ a{i}: x }} }}" for i in range(20))
1258 + " } }"
1259 )
1260 # With a generous budget the query is valid, proving the fields do not
1261 # conflict and that no deduplication pass could collapse them.
1262 with budget_of(1_000_000):
1263 assert_valid(query, schema=budget_schema)
1264 # With a small budget the same query is aborted rather than validated in
1265 # quadratic time.
1266 with budget_of(10):
1267 assert_errors(
1268 query,
1269 [
1270 {
1271 "message": "Query is too complex to validate for"
1272 " overlapping fields: exceeded 10 field comparisons."
1273 " Validation was aborted."
1274 }
1275 ],
1276 schema=budget_schema,
1277 )
1278
1279 def does_not_abort_queries_that_stay_within_the_budget():
1280 # A valid overlapping query well under the budget validates normally, with
1281 # no spurious abort error.
1282 with budget_of(50):
1283 assert_valid(
1284 "{ field { ... on Node { x } ... on Node { x } } }",
1285 schema=budget_schema,
1286 )