Skip to content

Commit 663d00d

Browse files
committed
backport: Require non-empty directive locations
Replicates graphql/graphql-js@993d7ce
1 parent 42328a6 commit 663d00d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/graphql/type/validate.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ def validate_directives(self) -> None:
154154
# Ensure they are named correctly.
155155
self.validate_name(directive)
156156

157+
if not directive.locations:
158+
self.report_error(
159+
f"Directive @{directive.name} must include 1 or more locations.",
160+
directive.ast_node,
161+
)
162+
157163
# Ensure the arguments are valid.
158164
for arg_name, arg in directive.args.items():
159165
# Ensure they are named correctly.

tests/type/test_validation.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,21 @@ def rejects_a_schema_whose_directives_are_incorrectly_typed():
446446
{"message": "Expected directive but got: SomeScalar."},
447447
]
448448

449+
def rejects_a_schema_whose_directives_have_empty_locations():
450+
bad_directive = GraphQLDirective(
451+
name="BadDirective1",
452+
locations=[],
453+
)
454+
schema = GraphQLSchema(
455+
SomeObjectType,
456+
directives=[bad_directive],
457+
)
458+
assert validate_schema(schema) == [
459+
{
460+
"message": "Directive @BadDirective1 must include 1 or more locations.",
461+
},
462+
]
463+
449464

450465
def describe_type_system_objects_must_have_fields():
451466
def accepts_an_object_type_with_fields_object():

0 commit comments

Comments
 (0)