Debian Codegraph @

tests/validation/test_scalar_leafs.py

raw
1 from functools import partial
2
3 from graphql.language import (
4 DocumentNode,
5 FieldNode,
6 NameNode,
7 OperationDefinitionNode,
8 OperationType,
9 SelectionSetNode,
10 )
11 from graphql.validation import ScalarLeafsRule
12 from graphql.validation.validate import validate
13
14 from .harness import assert_validation_errors, test_schema
15
16 assert_errors = partial(assert_validation_errors, ScalarLeafsRule)
17
18 assert_valid = partial(assert_errors, errors=[])
19
20
21 def describe_validate_scalar_leafs():
22 def valid_scalar_selection():
23 assert_valid("""
24 fragment scalarSelection on Dog {
25 barks
26 }
27 """)
28
29 def object_type_missing_selection():
30 assert_errors(
31 """
32 query directQueryOnObjectWithoutSubFields {
33 human
34 }
35 """,
36 [
37 {
38 "message": "Field 'human' of type 'Human'"
39 " must have a selection of subfields."
40 " Did you mean 'human { ... }'?",
41 "locations": [(3, 15)],
42 },
43 ],
44 )
45
46 def interface_type_missing_selection():
47 assert_errors(
48 """
49 {
50 human { pets }
51 }
52 """,
53 [
54 {
55 "message": "Field 'pets' of type '[Pet]'"
56 " must have a selection of subfields."
57 " Did you mean 'pets { ... }'?",
58 "locations": [(3, 23)],
59 },
60 ],
61 )
62
63 def valid_scalar_selection_with_args():
64 assert_valid("""
65 fragment scalarSelectionWithArgs on Dog {
66 doesKnowCommand(dogCommand: SIT)
67 }
68 """)
69
70 def scalar_selection_not_allowed_on_boolean():
71 assert_errors(
72 """
73 fragment scalarSelectionsNotAllowedOnBoolean on Dog {
74 barks { sinceWhen }
75 }
76 """,
77 [
78 {
79 "message": "Field 'barks' must not have a selection"
80 " since type 'Boolean' has no subfields.",
81 "locations": [(3, 21)],
82 },
83 ],
84 )
85
86 def scalar_selection_not_allowed_on_enum():
87 assert_errors(
88 """
89 fragment scalarSelectionsNotAllowedOnEnum on Cat {
90 furColor { inHexDec }
91 }
92 """,
93 [
94 {
95 "message": "Field 'furColor' must not have a selection"
96 " since type 'FurColor' has no subfields.",
97 "locations": [(3, 24)],
98 },
99 ],
100 )
101
102 def scalar_selection_not_allowed_with_args():
103 assert_errors(
104 """
105 fragment scalarSelectionsNotAllowedWithArgs on Dog {
106 doesKnowCommand(dogCommand: SIT) { sinceWhen }
107 }
108 """,
109 [
110 {
111 "message": "Field 'doesKnowCommand' must not have a selection"
112 " since type 'Boolean' has no subfields.",
113 "locations": [(3, 48)],
114 },
115 ],
116 )
117
118 def scalar_selection_not_allowed_with_directives():
119 assert_errors(
120 """
121 fragment scalarSelectionsNotAllowedWithDirectives on Dog {
122 name @include(if: true) { isAlsoHumanName }
123 }
124 """,
125 [
126 {
127 "message": "Field 'name' must not have a selection"
128 " since type 'String' has no subfields.",
129 "locations": [(3, 39)],
130 },
131 ],
132 )
133
134 def scalar_selection_not_allowed_with_directives_and_args():
135 assert_errors(
136 """
137 fragment scalarSelectionsNotAllowedWithDirectivesAndArgs on Dog {
138 doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen }
139 }
140 """,
141 [
142 {
143 "message": "Field 'doesKnowCommand' must not have a selection"
144 " since type 'Boolean' has no subfields.",
145 "locations": [(3, 67)],
146 },
147 ],
148 )
149
150 def object_type_having_only_one_selection():
151 # We can't leverage assert_errors since it doesn't support passing in the
152 # document node directly. We have to do this because this is technically
153 # an invalid document.
154 doc = DocumentNode(
155 definitions=[
156 OperationDefinitionNode(
157 operation=OperationType.QUERY,
158 selection_set=SelectionSetNode(
159 selections=[
160 FieldNode(
161 name=NameNode(value="human"),
162 selection_set=SelectionSetNode(selections=[]),
163 ),
164 ],
165 ),
166 ),
167 ],
168 )
169 errors = validate(test_schema, doc, [ScalarLeafsRule])
170 assert errors == [
171 {
172 "message": "Field 'human' of type 'Human'"
173 " must have at least one field selected.",
174 },
175 ]