Skip to content

Fix codegen crash with directive definitions and non-string args#4230

Draft
meadsteve wants to merge 3 commits intostrawberry-graphql:mainfrom
meadsteve:fix/codegen-directive-definition-node
Draft

Fix codegen crash with directive definitions and non-string args#4230
meadsteve wants to merge 3 commits intostrawberry-graphql:mainfrom
meadsteve:fix/codegen-directive-definition-node

Conversation

@meadsteve
Copy link

@meadsteve meadsteve commented Feb 12, 2026

Summary

I've been using your library and the codegen from a schema and ran in to an issue when using custom directives. I added a failing test case and got an AI to write the fix. I've looked at it myself and it seems to make sense.

🤖 Generated with Claude Code

Summary by Sourcery

Handle directive definitions and non-string directive argument values in schema code generation to prevent crashes when processing input type fields.

Bug Fixes:

  • Prevent codegen from raising NotImplementedError when encountering DirectiveDefinitionNode entries in the schema.
  • Ensure directive arguments with enum, int, float, and null literal values are correctly parsed and no longer crash codegen.

Enhancements:

  • Extend argument sanitization and type aliasing to support int, float, None, and nested lists of these values in generated code.

Tests:

  • Add regression test for input types with a directive on a field using an int argument to verify codegen succeeds.

…-string args

The schema codegen raised NotImplementedError when encountering
DirectiveDefinitionNode (e.g. `directive @limits(max: Int!) on INPUT_FIELD_DEFINITION`)
and when directive arguments used IntValueNode, FloatValueNode, or NullValueNode.

Also fixes EnumValueDefinitionNode being checked instead of EnumValueNode
in _get_argument_value().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Feb 12, 2026

Reviewer's Guide

Handles directive definitions and non-string directive argument values in schema codegen to prevent crashes, and extends argument value sanitization to support ints, floats, and nulls, with a regression test for directives on input fields.

File-Level Changes

Change Details Files
Extend argument value model and sanitizer to support numeric and null directive arguments.
  • Update ArgumentValue type alias to include int, float, None, and nested lists of these types.
  • Enhance _sanitize_argument to convert None, ints, and floats into the appropriate libcst nodes (Name('None'), Integer, Float) while preserving existing handling for bool, str, and list values.
  • Keep list handling recursive so nested argument structures are still correctly converted to CST.
strawberry/schema_codegen/__init__.py
Fix argument value extraction from GraphQL AST to cover more ConstValueNode variants and correct enum handling.
  • Change enum argument handling from EnumValueDefinitionNode to EnumValueNode and use its value field directly.
  • Add support in _get_argument_value for IntValueNode, FloatValueNode, and NullValueNode, converting them to Python int, float, and None respectively.
  • Preserve and reuse existing handling of strings, lists, and booleans, with a NotImplementedError fallback for unknown node types.
strawberry/schema_codegen/__init__.py
Prevent codegen from crashing on directive definitions in the schema.
  • Recognize DirectiveDefinitionNode in codegen() and treat it as a no-op (skip over it) instead of raising NotImplementedError.
  • Leave existing handling for object/enum/input types and federation link directives unchanged.
strawberry/schema_codegen/__init__.py
Add regression test to ensure directives with int args on input fields don’t crash codegen and are ignored in the generated code.
  • Add test_input_type_with_directive_on_field using a schema with a directive definition and a directive with an int argument on an input field.
  • Assert that codegen(schema) produces the expected @strawberry.input class without including directive-specific code and without raising errors.
tests/schema_codegen/test_input_types.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

meadsteve and others added 2 commits February 12, 2026 22:59
The schema codegen now generates @strawberry.schema_directive classes from
directive definitions and applies them via directives=[...] kwargs on types
and fields, instead of silently dropping them.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant