Skip to content

Commit

Permalink
raise-error-when-directive-declaration-is-missing (#1173)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianCzajkowski authored Apr 3, 2024
1 parent bc32076 commit 5a0f358
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## 0.24 (UNRELEASED)

- Added validation for directive declarations in `make_executable_schema` to prevent schema creation with undeclared directives.


## 0.23 (2024-03-18)

- Added `execute_get_queries` setting to the `GraphQL` apps that controls execution of the GraphQL "query" operations made with GET requests. Defaults to `False`.
Expand Down
9 changes: 9 additions & 0 deletions ariadne/schema_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,15 @@ def _add_directive(decl):

each(schema.directives, _add_directive)

missing_directives = [
name for name in directive_visitors if name not in declared_directives
]

if missing_directives:
raise ValueError(
f"Missing directive declarations for: {', '.join(missing_directives)}"
)

# If the visitor subclass overrides get_directive_declaration, and it
# returns a non-null GraphQLDirective, use that instead of any directive
# declared in the schema itself. Reasoning: if a SchemaDirectiveVisitor
Expand Down
15 changes: 15 additions & 0 deletions tests/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,21 @@ def test_directive_raises_type_error_if_required_argument_is_not_given():
make_executable_schema(type_defs, directives={"test": ReturnValueDirective})


def test_directive_raises_type_error_if_there_is_typo():
type_defs = """
directive @test on FIELD_DEFINITION
type Query {
hello: String! @test,
}
"""

with pytest.raises(ValueError):
make_executable_schema(
type_defs, directives={"test_typo": ReturnValueDirective}
)


def test_can_implement_unique_id_directive():
type_defs = """
directive @uniqueID(name: String, from: [String]) on OBJECT
Expand Down

0 comments on commit 5a0f358

Please sign in to comment.