Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 8 additions & 1 deletion strawberry/schema_codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import libcst as cst
from graphql import (
DirectiveDefinitionNode,
EnumTypeDefinitionNode,
EnumValueDefinitionNode,
FieldDefinitionNode,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion tests/cli/test_schema_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from typer.testing import CliRunner

schema = """
directive @foo on FIELD_DEFINITION

type Query {
hello: String!
hello: String! @foo
}
"""

Expand Down
Loading