diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..de8f153a6b --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,13 @@ +Release type: patch + +This release fixes schema codegen to support custom directive definitions. Previously, schemas containing custom directives like `@authz` would fail with `NotImplementedError: Unknown definition None`. Now directive definitions are gracefully skipped since they don't require code generation. + +```graphql +directive @authz(resource: String!, action: String!) on FIELD_DEFINITION + +type Query { + hello: String! @authz(resource: "greeting", action: "read") +} +``` + +This also fixes the error message for truly unknown definition types to show the actual type name instead of `None`. diff --git a/strawberry/schema_codegen/__init__.py b/strawberry/schema_codegen/__init__.py index 3c8ceb2018..5222f21324 100644 --- a/strawberry/schema_codegen/__init__.py +++ b/strawberry/schema_codegen/__init__.py @@ -9,6 +9,7 @@ import libcst as cst from graphql import ( + DirectiveDefinitionNode, EnumTypeDefinitionNode, EnumValueDefinitionNode, FieldDefinitionNode, @@ -748,8 +749,14 @@ def codegen(schema: str) -> str: _is_federation_link_directive(directive) for directive in graphql_definition.directives ) + elif isinstance(graphql_definition, DirectiveDefinitionNode): + # Custom directive definitions don't need code generation, + # they're only used by the GraphQL schema itself + pass else: - raise NotImplementedError(f"Unknown definition {definition}") + raise NotImplementedError( + f"Unknown definition {type(graphql_definition).__name__}" + ) if definition is not None: definitions[definition.name] = definition diff --git a/tests/cli/test_schema_codegen.py b/tests/cli/test_schema_codegen.py index 9d3e3286a5..d5bb8e9c7b 100644 --- a/tests/cli/test_schema_codegen.py +++ b/tests/cli/test_schema_codegen.py @@ -5,8 +5,10 @@ from typer.testing import CliRunner schema = """ +directive @foo on FIELD_DEFINITION + type Query { - hello: String! + hello: String! @foo } """