Skip to content

Commit d00d83e

Browse files
Citoclaude
andcommitted
Address empty selection-set
Replicates graphql/graphql-js@4ef04286 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 9628498 commit d00d83e

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/graphql/validation/rules/scalar_leafs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ def enter_field(self, node: FieldNode, *_args: Any) -> None:
3939
node,
4040
)
4141
)
42+
elif not selection_set.selections:
43+
field_name = node.name.value
44+
self.report_error(
45+
GraphQLError(
46+
f"Field '{field_name}' of type '{type_}'"
47+
" must have at least one field selected.",
48+
node,
49+
)
50+
)

tests/validation/test_scalar_leafs.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
from functools import partial
22

3+
from graphql.language import (
4+
DocumentNode,
5+
FieldNode,
6+
NameNode,
7+
OperationDefinitionNode,
8+
OperationType,
9+
SelectionSetNode,
10+
)
311
from graphql.validation import ScalarLeafsRule
12+
from graphql.validation.validate import validate
413

5-
from .harness import assert_validation_errors
14+
from .harness import assert_validation_errors, test_schema
615

716
assert_errors = partial(assert_validation_errors, ScalarLeafsRule)
817

@@ -137,3 +146,30 @@ def scalar_selection_not_allowed_with_directives_and_args():
137146
},
138147
],
139148
)
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+
]

0 commit comments

Comments
 (0)