Skip to content

Commit 9823664

Browse files
committed
Require non-empty directive locations
Replicates graphql/graphql-js@36e59f4
1 parent 6b887af commit 9823664

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
@@ -440,6 +440,21 @@ def rejects_a_schema_whose_directives_are_incorrectly_typed():
440440
{"message": "Expected directive but got: SomeScalar."},
441441
]
442442

443+
def rejects_a_schema_whose_directives_have_empty_locations():
444+
bad_directive = GraphQLDirective(
445+
name="BadDirective1",
446+
locations=[],
447+
)
448+
schema = GraphQLSchema(
449+
SomeObjectType,
450+
directives=[bad_directive],
451+
)
452+
assert validate_schema(schema) == [
453+
{
454+
"message": "Directive @BadDirective1 must include 1 or more locations.",
455+
},
456+
]
457+
443458

444459
def describe_type_system_root_types_must_all_be_different_if_provided():
445460
def accepts_a_schema_with_different_root_types():

0 commit comments

Comments
 (0)