fix(codegen): keep GraphQL field names that don't survive camel-casing - #4551
fix(codegen): keep GraphQL field names that don't survive camel-casing#4551ritsth wants to merge 2 commits into
Conversation
|
Thanks for adding the Below is the changelog that will be used for the release. This release fixes schema codegen silently renaming fields whose GraphQL names Strawberry derives the GraphQL name of a field by camel-casing its Python name, Codegen now adds an explicit alias whenever camel-casing would not give the @strawberry.type
class Example:
some_field: int | None = strawberry.field(name="some_field")
allow_custom_export_url: bool = strawberry.field(name="allowCustomExportURL")Fields whose GraphQL names convert to the same Python name (for example |
Greptile SummaryThis PR preserves original GraphQL field names during SDL-to-Python schema generation.
Confidence Score: 5/5The PR appears safe to merge, with no actionable defects identified in the changed behavior. The generated fields retain distinct Python attributes while explicit aliases preserve their distinct original GraphQL names, and the added tests cover the principal naming transformations addressed by the fix.
|
| Filename | Overview |
|---|---|
| strawberry/schema_codegen/init.py | Adds per-definition Python-name collision handling and aliases fields whose original GraphQL names cannot be recovered through camel-casing. |
| tests/schema_codegen/test_names.py | Updates acronym expectations and adds focused coverage for preserved GraphQL names and colliding generated Python attributes. |
| RELEASE.md | Clearly documents the schema-codegen naming defect and corrected generated output. |
Reviews (1): Last reviewed commit: "fix(codegen): keep GraphQL field names t..." | Re-trigger Greptile
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
test_handles_names_converting_to_the_same_python_namebuilds anexpectedstring but never asserts againstcodegen(schema), so the test currently doesn't validate behavior and should add an assertion similar to the other tests. - The collision-handling logic that repeatedly appends underscores to
namein_get_fieldwould benefit from being factored into a small helper (e.g.make_unique_name(name, used_names)) to make the intent clearer and easier to reuse or adjust.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `test_handles_names_converting_to_the_same_python_name` builds an `expected` string but never asserts against `codegen(schema)`, so the test currently doesn't validate behavior and should add an assertion similar to the other tests.
- The collision-handling logic that repeatedly appends underscores to `name` in `_get_field` would benefit from being factored into a small helper (e.g. `make_unique_name(name, used_names)`) to make the intent clearer and easier to reuse or adjust.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Description
schema-codegengenerated code that produces a different schema than the SDL itwas given. Strawberry derives a field's GraphQL name by camel-casing its Python
name, but codegen only added an explicit
name=alias for Python keywords. Anyname that isn't reproduced by camel-casing was silently renamed:
some_field: IntsomeField: IntallowCustomExportURL: Boolean!allowCustomExportUrl: Boolean!URL: Stringurl: StringAdditionally, two GraphQL names converting to the same Python name (e.g.
someFieldandsome_field) produced a duplicate attribute, silently droppingone field.
Codegen now adds an alias whenever camel-casing wouldn't give the original name
back, and makes colliding Python names unique.
Note: this updates
test_converts_names_to_snake_case, which asserted theprevious (incorrect) output for
allowCustomExportURL/allowInsecureTLS—those generated
allowCustomExportUrl/allowInsecureTlsin the resultingschema.
Types of Changes
Issues Fixed or Closed by This PR
Checklist
Summary by Sourcery
Ensure schema codegen preserves original GraphQL field names and avoids silently dropping fields when names collide after conversion to Python identifiers.
Bug Fixes:
Documentation:
Tests: