Repository: mirumee/ariadne
Pull request #1173 (2024-04-03 11:49:59)

raise-error-when-directive-declaration-is-missing
Fixes: https://github.com/mirumee/ariadne/issues/420

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/mirumee/ariadne/pull/1173?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mirumee) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 97.86%. Comparing base [(`bc32076`)](https://app.codecov.io/gh/mirumee/ariadne/commit/bc32076e4b13734376110440b15a4c0c58367a11?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mirumee) to head [(`948deaa`)](https://app.codecov.io/gh/mirumee/ariadne/pull/1173?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mirumee).


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #1173   +/-   ##
=======================================
  Coverage   97.86%   97.86%           
=======================================
  Files         109      109           
  Lines        8135     8142    +7     
=======================================
+ Hits         7961     7968    +7     
  Misses        174      174           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/mirumee/ariadne/pull/1173?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mirumee).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mirumee).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6268b2e..6bdf3a0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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`.
diff --git a/ariadne/schema_visitor.py b/ariadne/schema_visitor.py
index 5300c88..7d76d7d 100644
--- a/ariadne/schema_visitor.py
+++ b/ariadne/schema_visitor.py
@@ -468,6 +468,15 @@ class SchemaDirectiveVisitor(SchemaVisitor):
 
         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


Test patch:
diff --git a/tests/test_directives.py b/tests/test_directives.py
index 85440b4..3759b57 100644
--- a/tests/test_directives.py
+++ b/tests/test_directives.py
@@ -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