Skip to content

Give GraphQL models a __typename property graphql-core can read - #6792

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix-mapped-allof-postpassfrom
graphql-typename-property
Nov 13, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix-mapped-allof-postpassfrom
graphql-typename-property

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Fixes: #6768

Summary by CodeRabbit

  • New Features
    • Generated GraphQL object models now expose a read-only __typename property across supported model types, making them easier to resolve in GraphQL unions and interfaces.
  • Documentation
    • Updated GraphQL and customization examples to show the property and clarify where it is generated and where it is not.
  • Tests
    • Added coverage for GraphQL type resolution across supported model types and custom templates.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

GraphQL object models now expose a read-only __typename property that returns the existing typename field. The change adds parser and rendering logic, updates generated examples and snapshots, and tests GraphQL union and interface type resolution.

Changes: GraphQL typename property

Layer / File(s) Summary
Mark and render typename properties
src/datamodel_code_generator/parser/graphql.py, src/datamodel_code_generator/model/base.py, src/datamodel_code_generator/model/_rendered_names.py, src/datamodel_code_generator/model/output.py, src/datamodel_code_generator/model/typed_dict.py
The GraphQL parser marks object models for property generation when graphql_no_typename is disabled. Rendering adds the property when supported and no custom root template is used. Name binding and collision checks now include method decorators.
Generated model output coverage
tests/data/expected/main/*, tests/data/expected/parser/graphql/*, tests/data/expected/skills/*
Expected outputs add properties backed by existing typename fields across GraphQL model types, aliases, unions, interfaces, and naming cases. The snapshots cover dataclasses, Pydantic models, and msgspec structs.
GraphQL execution and template tests
tests/main/graphql/test_main_graphql.py, tests/main/graphql/test_main_graphql.refs
Parameterized tests execute a query across a union and interface for four model types. Additional coverage checks generated output when a custom template supplies the class body.
Documentation examples
docs/graphql.md, docs/cli-reference/*
Examples show the property. GraphQL documentation describes its use in default union and interface type resolution and lists excluded cases.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant GraphQLParser
  participant DataModelRender
  participant GeneratedModel
  participant GraphQLCore
  GraphQLParser->>DataModelRender: mark GraphQL object model
  DataModelRender->>GeneratedModel: add __typename property
  GraphQLCore->>GeneratedModel: read __typename for type resolution
Loading
Merge Risk: Low · up to 3ff07

GraphQL models now expose a read-only __typename property that graphql-core's default resolver can read. In the common case this works. An edge case remains: with future annotations disabled, a schema field named Literal can make the generated module fail to import. This is narrow, so the change is mergeable with owner awareness and a small follow-up.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Give GraphQL models a __typename property graphql-core can read" matches the main change. The PR adds a generated __typename property to GraphQL object models, backed by the existing `typ…
Linked Issues check Passed Objective [#6768] Generated GraphQL object models expose __typename that graphql-core's default type resolver reads, without renaming typename__. Evidence from the PR #6792 summary:
- `model/base.…
Out of Scope Changes check Passed Changes in PR #6792 relate to the #6768 objective:
- model/base.py, parser/graphql.py, and model/typed_dict.py implement the property and its TypedDict and --graphql-no-typename exclusions.
- …
Full details: Title check

Explanation

The title "Give GraphQL models a __typename property graphql-core can read" matches the main change. The PR adds a generated __typename property to GraphQL object models, backed by the existing typename__ field, so graphql-core's default type resolver can read it. The title is short, specific, and states the primary change. It does not mention the docs or test updates, which is acceptable because they support the main change.

Full details: Linked Issues check

Explanation

Objective [#6768] Generated GraphQL object models expose __typename that graphql-core's default type resolver reads, without renaming typename__. Evidence from the PR #6792 summary:

  • model/base.py adds TYPENAME_PROPERTY_KEY. DataModel._render appends a __typename property that returns the existing typename__ field. Name mangling gives _<ClassName>__typename, which get_typename reads.
  • parser/graphql.py sets the key only for GraphQL object types when graphql_no_typename is false. Interfaces and input objects are not marked.
  • model/typed_dict.py sets _HOLDS_PROPERTIES = False, so TypedDict output gets no property. With --graphql-no-typename, no property is emitted.
  • typename__ field names, constructor keywords, and aliases stay unchanged in the expected outputs.
  • model/_rendered_names.py and model/output.py rewrite or check method decorators. This handles the property field in TextMatch, which would otherwise shadow the builtin @property.
  • Tests and expected outputs cover dataclass, Pydantic v2 BaseModel, Pydantic v2 dataclass, and msgspec output. A runtime test resolves a union through graphql-core.

The current PR diff covers the requirement. Stack members below #6792 have summaries unrelated to GraphQL __typename rendering. Two members are uncertain, so this verdict depends on the current PR's own changes and not on those members. The verdict relies on attested summaries and was not confirmed by direct code inspection in this review.

Full details: Out of Scope Changes check

Explanation

Changes in PR #6792 relate to the #6768 objective:

  • model/base.py, parser/graphql.py, and model/typed_dict.py implement the property and its TypedDict and --graphql-no-typename exclusions.
  • model/_rendered_names.py and model/output.py handle method decorators and name collisions. These changes are needed because generated models can have a field named property, which shadows @property.
  • Expected-output fixtures and tests/main/graphql/test_main_graphql.py (plus its .refs file) validate the generated output and runtime resolution.
  • Documentation updates in docs/graphql.md and the docs/cli-reference files describe the generated __typename property.

No unrelated change is identified in the summary. Excluded files listed as ignored were not inspected.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1bf982c and 3ff0773.

Files ignored due to path filters: 5

  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/expected/main/graphql/main_graphql/typename_property[DataModelType.DataclassesDataclass].graphql.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/main_general/msgspec_constraints_on_a_graphql_scalar.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/main_general/render_plan_of_a_declared_default_factory_of_none[False].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/graphql/typename_property.graphql is excluded by !tests/data/**/*.graphql and included by none
Files selected for processing: 125

  • docs/cli-reference/field-customization.md
  • docs/cli-reference/model-customization.md
  • docs/cli-reference/template-customization.md
  • docs/cli-reference/typing-customization.md
  • docs/graphql.md
  • src/datamodel_code_generator/model/_rendered_names.py
  • src/datamodel_code_generator/model/base.py
  • src/datamodel_code_generator/model/output.py
  • src/datamodel_code_generator/model/typed_dict.py
  • src/datamodel_code_generator/parser/graphql.py
  • tests/data/expected/main/apply_field_options_to_every_field/graphql[every-field].py
  • tests/data/expected/main/apply_field_options_to_every_field/graphql[released].py
  • tests/data/expected/main/graphql/additional_imports.py
  • tests/data/expected/main/graphql/annotated.py
  • tests/data/expected/main/graphql/annotated_field_aliases.py
  • tests/data/expected/main/graphql/annotated_use_default_kwarg.py
  • tests/data/expected/main/graphql/casing.py
  • tests/data/expected/main/graphql/custom_formatters.py
  • tests/data/expected/main/graphql/custom_scalar_types.py
  • tests/data/expected/main/graphql/default_values_required_use_default.py
  • tests/data/expected/main/graphql/different_types_of_fields.py
  • tests/data/expected/main/graphql/field_aliases.py
  • tests/data/expected/main/graphql/graphql_multiple_aliases_pydantic_v2.py
  • tests/data/expected/main/graphql/graphql_scalar_type_name_follows_target.py
  • tests/data/expected/main/graphql/graphql_scalar_type_name_follows_target~scalar_type_name_follows_target[3.12].py
  • tests/data/expected/main/graphql/graphql_serialization_aliases_pydantic_v2.py
  • tests/data/expected/main/graphql/graphql_typename_property.py
  • tests/data/expected/main/graphql/graphql_typename_property_custom_template.py
  • tests/data/expected/main/graphql/graphql_typename_property~typename_property[DataModelType.MsgspecStruct].py
  • tests/data/expected/main/graphql/graphql_typename_property~typename_property[DataModelType.PydanticV2BaseModel].py
  • tests/data/expected/main/graphql/graphql_typename_property~typename_property[DataModelType.PydanticV2Dataclass].py
  • tests/data/expected/main/graphql/interface_mro.py
  • tests/data/expected/main/graphql/main_graphql/strip_default_none_constructor_order[msgspec].py
  • tests/data/expected/main/graphql/main_graphql/strip_default_none_constructor_order[pydantic-dataclass].py
  • tests/data/expected/main/graphql/msgspec_unions.py
  • tests/data/expected/main/graphql/root_type_references.py
  • tests/data/expected/main/graphql/root_union_custom_dataclasses_dataclass.py
  • tests/data/expected/main/graphql/root_union_custom_msgspec_Struct.py
  • tests/data/expected/main/graphql/root_union_custom_pydantic_v2_BaseModel.py
  • tests/data/expected/main/graphql/root_union_custom_pydantic_v2_dataclass.py
  • tests/data/expected/main/graphql/root_union_field_dataclasses_dataclass.py
  • tests/data/expected/main/graphql/root_union_field_msgspec_Struct.py
  • tests/data/expected/main/graphql/root_union_field_pydantic_v2_BaseModel.py
  • tests/data/expected/main/graphql/root_union_field_pydantic_v2_dataclass.py
  • tests/data/expected/main/graphql/root_union_mutation_dataclasses_dataclass.py
  • tests/data/expected/main/graphql/root_union_mutation_msgspec_Struct.py
  • tests/data/expected/main/graphql/root_union_mutation_pydantic_v2_BaseModel.py
  • tests/data/expected/main/graphql/root_union_mutation_pydantic_v2_dataclass.py
  • tests/data/expected/main/graphql/root_union_ordinary_dataclasses_dataclass.py
  • tests/data/expected/main/graphql/root_union_ordinary_msgspec_Struct.py
  • tests/data/expected/main/graphql/root_union_ordinary_pydantic_v2_BaseModel.py
  • tests/data/expected/main/graphql/root_union_ordinary_pydantic_v2_dataclass.py
  • tests/data/expected/main/graphql/root_union_query_dataclasses_dataclass.py
  • tests/data/expected/main/graphql/root_union_query_msgspec_Struct.py
  • tests/data/expected/main/graphql/root_union_query_pydantic_v2_BaseModel.py
  • tests/data/expected/main/graphql/root_union_query_pydantic_v2_dataclass.py
  • tests/data/expected/main/graphql/root_union_subscription_dataclasses_dataclass.py
  • tests/data/expected/main/graphql/root_union_subscription_msgspec_Struct.py
  • tests/data/expected/main/graphql/root_union_subscription_pydantic_v2_BaseModel.py
  • tests/data/expected/main/graphql/root_union_subscription_pydantic_v2_dataclass.py
  • tests/data/expected/main/graphql/same_named_list_msgspec_no_future_imports.py
  • tests/data/expected/main/graphql/simple_star_wars.py
  • tests/data/expected/main/graphql/simple_star_wars_class_name_prefix.py
  • tests/data/expected/main/graphql/simple_star_wars_dataclass.py
  • tests/data/expected/main/graphql/simple_star_wars_dataclass_arguments.py
  • tests/data/expected/main/graphql/simple_star_wars_dataclass_frozen_kw_only.py
  • tests/data/expected/main/graphql/simple_star_wars_extra_fields_allow.py
  • tests/data/expected/main/graphql/split_graphql_schemas.py
  • tests/data/expected/main/graphql/type_alias.py
  • tests/data/expected/main/graphql/type_alias_py312.py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[annotations-dataclasses.dataclass-extra_args3].py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[annotations-msgspec.Struct-extra_args5].py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[annotations-pydantic_v2.BaseModel-extra_args0].py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[annotations-pydantic_v2.dataclass-extra_args2].py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[members-dataclasses.dataclass-extra_args8].py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[members-pydantic_v2.BaseModel-extra_args6].py
  • tests/data/expected/main/graphql/type_overrides_literal/graphql_type_overrides_literal[members-pydantic_v2.dataclass-extra_args7].py
  • tests/data/expected/main/graphql/typename_aliases_dataclasses_dataclass_False.py
  • tests/data/expected/main/graphql/typename_aliases_msgspec_Struct_False.py
  • tests/data/expected/main/graphql/typename_aliases_pydantic_v2_BaseModel_False.py
  • tests/data/expected/main/graphql/typename_aliases_pydantic_v2_dataclass_False.py
  • tests/data/expected/main/graphql/typename_collision_dataclasses_dataclass_False.py
  • tests/data/expected/main/graphql/typename_collision_msgspec_Struct_False.py
  • tests/data/expected/main/graphql/typename_collision_pydantic_v2_BaseModel_False.py
  • tests/data/expected/main/graphql/typename_collision_pydantic_v2_dataclass_False.py
  • tests/data/expected/main/graphql/typename_normal_dataclasses_dataclass_False.py
  • tests/data/expected/main/graphql/typename_normal_msgspec_Struct_False.py
  • tests/data/expected/main/graphql/typename_normal_pydantic_v2_BaseModel_False.py
  • tests/data/expected/main/graphql/typename_normal_pydantic_v2_dataclass_False.py
  • tests/data/expected/main/graphql/typename_scoped_dataclasses_dataclass_False.py
  • tests/data/expected/main/graphql/typename_scoped_msgspec_Struct_False.py
  • tests/data/expected/main/graphql/typename_scoped_pydantic_v2_BaseModel_False.py
  • tests/data/expected/main/graphql/typename_scoped_pydantic_v2_dataclass_False.py
  • tests/data/expected/main/graphql/typename_serialization_pydantic_v2_BaseModel_False.py
  • tests/data/expected/main/graphql/typename_serialization_pydantic_v2_dataclass_False.py
  • tests/data/expected/main/graphql/typename_snake_dataclasses_dataclass_False.py
  • tests/data/expected/main/graphql/typename_snake_msgspec_Struct_False.py
  • tests/data/expected/main/graphql/typename_snake_pydantic_v2_BaseModel_False.py
  • tests/data/expected/main/graphql/typename_snake_pydantic_v2_dataclass_False.py
  • tests/data/expected/main/graphql/union.py
  • tests/data/expected/main/graphql/union_class_name_prefix.py
  • tests/data/expected/main/graphql/union_description_cr_dataclass.py
  • tests/data/expected/main/graphql/union_description_cr_py312.py
  • tests/data/expected/main/graphql/union_description_cr_pydantic_v2.py
  • tests/data/expected/main/graphql/union_snake_case_field.py
  • tests/data/expected/main/graphql/use_standard_collections.py
  • tests/data/expected/main/graphql/use_union_operator.py
  • tests/data/expected/main/jsonschema/explicit_alias_names_graphql_global.py
  • tests/data/expected/main/jsonschema/explicit_alias_names_graphql_literal.py
  • tests/data/expected/main/jsonschema/model_metadata/schema_location_metadata[graphql].py
  • tests/data/expected/main/main_general/class_keyed_options_name_renamed_classes[graphql-type].py
  • tests/data/expected/main/msgspec_inheritance/graphql_diamond_both_typename.py
  • tests/data/expected/main/msgspec_inheritance/graphql_diamond_one_typename.py
  • tests/data/expected/main/msgspec_inheritance/graphql_diamond_typename.py
  • tests/data/expected/main/msgspec_inheritance/graphql_independent_typename.py
  • tests/data/expected/main/msgspec_inheritance/graphql_redundant_typename.py
  • tests/data/expected/main/msgspec_inheritance/graphql_single_typename.py
  • tests/data/expected/parser/graphql/collection_options.py
  • tests/data/expected/parser/graphql/collection_options_generate.py
  • tests/data/expected/parser/graphql/union-aliased-bug.py
  • tests/data/expected/parser/graphql/union-commented.py
  • tests/data/expected/parser/graphql/union_with_prefix.py
  • tests/data/expected/skills/datamodel-code-generator/graphql.py
  • tests/main/graphql/test_main_graphql.py
  • tests/main/graphql/test_main_graphql.refs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 13, 2026 04:26
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T04:29:08.642185Z 3ff0773 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

Copy link
Copy Markdown
Contributor

@codecov

codecov Bot commented Nov 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Nov 13, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.89%

2 warnings

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ 21 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 1 improved benchmark
❌ 20 regressed benchmarks
✅ 32 untouched benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_empty_pydantic_v2_field_render_plans 29 ms 36.8 ms -21.13%
❌ test_perf_pattern_properties_adapter_reuse 3 ms 3.8 ms -20.76%
❌ test_perf_simple_pydantic_v2_field_construction 9.1 ms 11.3 ms -18.98%
❌ test_perf_load_pyproject_config[mapping-multiple] 169.6 µs 203.7 µs -16.72%
❌ test_perf_load_pyproject_config[config-multiple] 169.1 µs 202.1 µs -16.34%
❌ test_perf_load_pyproject_config[mapping-None] 155.1 µs 183.9 µs -15.66%
❌ test_perf_unique_items_runtime_scalar_validation 2.7 ms 3.2 ms -15.53%
❌ test_perf_load_pyproject_config[config-None] 156.3 µs 184.7 µs -15.38%
❌ test_perf_recommend_options_full_kubernetes_style 20.4 s 23.7 s -13.93%
❌ test_perf_pydantic_field_name_resolution 2.1 ms 2.4 ms -13.4%
❌ test_perf_mapped_reference_users 727.2 ms 838.9 ms -13.31%
❌ test_perf_unique_items_permuted_object_validation 61.5 ms 69.6 ms -11.71%
❌ test_perf_target_render[client-small] 32.8 ms 37 ms -11.29%
❌ test_perf_unique_items_collapsed_builtin 1.8 s 2 s -11.16%
❌ test_perf_stripe_style_pydantic_v2 2.1 s 2.3 s -10.89%
❌ test_perf_deep_nested 4.6 s 5.1 s -10.7%
❌ test_perf_unique_items_schema_validators 1.8 s 2 s -10.67%
❌ test_perf_unique_items_nested_model_validation 2.8 ms 3.1 ms -10.52%
❌ test_perf_load_yaml_openapi_large 30.7 ms 34.3 ms -10.4%
❌ test_perf_aws_style_openapi_pydantic_v2 2 s 2.2 s -10.11%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing graphql-typename-property (3ff0773) with fix-mapped-allof-postpass (1bf982c)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@koxudaxi
Koudai Aono (koxudaxi) merged commit 6945ef2 into main Nov 13, 2026
117 of 118 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the graphql-typename-property branch November 13, 2026 05:05
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR adds a read-only, name-mangled typename property to the generated model of each GraphQL object type. This happens unless --graphql-no-typename is set, and it applies to pydantic v2 BaseModel, pydantic dataclass, dataclasses and msgspec Struct output. The property returns the existing typename field so graphql-core's default type resolver can resolve unions and interfaces. Input types, interface types, TypedDict output and custom model templates do not get the property, because of the new _HOLDS_PROPERTIES flag and the custom-template check. No fields, aliases, defaults, validation or serialization change, and no CLI option, public Python name or template variable is removed or renamed. The only removed lines are internal refactors in _rendered_names.py, base.py, output.py and an import in parser/graphql.py. Those refactors widen the internal name-binding and collision passes to cover method decorators, so a field named property makes the generator import builtins.property under an alias. The behaviour diff gate classifies every GraphQL cell, including the new typename_property input, as no-behaviour. Its only accepted cells are JSON Schema regex patterns that hold single quotes, which this diff does not touch, so they come from other changes since 0.95.0. The added property is therefore an additive code generation enhancement, not a breaking change to released behaviour.


This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.96.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GraphQL dataclass output names the field typename__, so graphql-core cannot resolve union types without a custom resolver

1 participant