Skip to content

fix: Always merge multiple GraphQL schemas before parsing#2873

Merged
koxudaxi merged 5 commits intokoxudaxi:mainfrom
siminn-arnorgj:support-combined-graphql-schema
Dec 31, 2025
Merged

fix: Always merge multiple GraphQL schemas before parsing#2873
koxudaxi merged 5 commits intokoxudaxi:mainfrom
siminn-arnorgj:support-combined-graphql-schema

Conversation

@siminn-arnorgj
Copy link
Copy Markdown
Contributor

@siminn-arnorgj siminn-arnorgj commented Dec 30, 2025

Summary

  • GraphQL parser now merges multiple input schemas instead of parsing them one by one.
  • Adds a test for the new behavior.

Fixes: #2872

Summary by CodeRabbit

  • New Features

    • Multiple GraphQL schema files are now processed as a single combined schema.
    • Generates GraphQL models and scalar type aliases (Foo, Bar, Baz; Int, String, Boolean) with forward-reference resolution.
  • Tests

    • Added test coverage for handling split / multiple GraphQL schema files.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 30, 2025

📝 Walkthrough

Walkthrough

Aggregate multiple GraphQL source texts into a single unified schema before parsing; remove the per-source path-context helper _get_context_source_path_parts; set self.raw_obj to the combined schema; resolve and parse types from the aggregated schema. Add expected output and a test for split GraphQL schema files. (34 words)

Changes

Cohort / File(s) Summary
GraphQL parser refactor
src/datamodel_code_generator/parser/graphql.py
Removed def _get_context_source_path_parts(...); aggregate all source.text into a single combined_schema; set self.raw_obj to the unified schema; call _resolve_types with empty paths; removed per-source parsing loop and related per-source handling.
Generated expected output (tests)
tests/data/expected/main/graphql/split_graphql_schemas.py
New generated module adding GraphQL scalar type aliases (Boolean, Int, String) and Pydantic models Foo, Bar, Baz with __typename alias fields and a Bar.update_forward_refs() call.
Tests
tests/main/graphql/test_main_graphql.py
Added test_main_graphql_split_graphql_schemas(output_file: Path) to validate generation from multiple split GraphQL schema files.

Sequence Diagram(s)

(omitted — changes consolidate internal parser behavior; no multi-component sequential flow meeting diagram criteria)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐇 I found small shards and tiny seams,
I stitched them into fuller dreams.
One schema now, no parts apart,
A tidy map for every chart. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: Always merge multiple GraphQL schemas before parsing' accurately describes the main change: modifying the GraphQL parser to merge multiple schemas before parsing instead of parsing them individually.
Linked Issues check ✅ Passed The PR addresses issue #2872 by implementing the expected behavior: merging multiple GraphQL schemas before parsing to allow cross-file type extensions. Code changes aggregate sources into a combined schema and resolve types once, eliminating per-source parsing that caused the 'Cannot extend type' errors.
Out of Scope Changes check ✅ Passed All changes are directly related to the stated objective: refactoring the GraphQL parser to merge schemas, updating test expectations for the new behavior, and adding a test case for split schema handling. No unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (7486e2a) to head (8987fbe).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2873      +/-   ##
==========================================
- Coverage   99.38%   99.38%   -0.01%     
==========================================
  Files          91       91              
  Lines       15648    15643       -5     
  Branches     1849     1848       -1     
==========================================
- Hits        15552    15547       -5     
  Misses         50       50              
  Partials       46       46              
Flag Coverage Δ
unittests 99.38% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/datamodel_code_generator/parser/graphql.py (1)

1-1: Pipeline failure: Commit the auto-fixed lint changes.

The Ruff linter auto-fixed 5 import-related issues. Please commit these changes to pass CI.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c3e159c and c899724.

⛔ Files ignored due to path filters (3)
  • tests/data/graphql/split/a.graphql is excluded by !tests/data/**/*.graphql and included by none
  • tests/data/graphql/split/b.graphql is excluded by !tests/data/**/*.graphql and included by none
  • tests/data/graphql/split/c.graphql is excluded by !tests/data/**/*.graphql and included by none
📒 Files selected for processing (3)
  • src/datamodel_code_generator/parser/graphql.py
  • tests/data/expected/main/graphql/split_graphql_schemas.py
  • tests/main/graphql/test_main_graphql.py
🧰 Additional context used
🧬 Code graph analysis (3)
src/datamodel_code_generator/parser/graphql.py (1)
src/datamodel_code_generator/parser/base.py (1)
  • iter_source (1033-1056)
tests/main/graphql/test_main_graphql.py (2)
tests/test_main_kr.py (1)
  • output_file (44-46)
tests/main/conftest.py (2)
  • output_file (98-100)
  • run_main_and_assert (244-408)
tests/data/expected/main/graphql/split_graphql_schemas.py (1)
src/datamodel_code_generator/model/type_alias.py (1)
  • TypeAlias (37-42)
🪛 GitHub Actions: Lint
src/datamodel_code_generator/parser/graphql.py

[error] 1-1: Ruff linting failed (legacy alias) with exit code 1 after auto-fixing 5 issues. Some imports were adjusted to satisfy lint rules (ruff: 5 fixes, 0 remaining). Run 'prek run --all-files' to reproduce locally or commit the changes and re-run CI.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.10 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: Analyze (python)
🔇 Additional comments (6)
src/datamodel_code_generator/parser/graphql.py (2)

700-702: LGTM! Schema merging approach is correct.

Combining all GraphQL sources into a single unified schema before parsing is the right approach for handling cross-file type references and resolving forward references.


704-704: The unusual path format []/SCALAR/Boolean is semantically odd but doesn't cause functional issues.

Passing an empty list [] to _resolve_types at line 704 results in reference paths like "[]/SCALAR/Boolean" (see line 353). While this format is unusual and doesn't match the intended path structure, it doesn't break path parsing or reference resolution since paths are used only as string keys, set members, and comparisons—not parsed or validated for structure. However, the empty list should be replaced with a meaningful initial path segment or removed entirely if the path shouldn't be nested, as the [] prefix is semantically misleading for GraphQL type resolution.

tests/main/graphql/test_main_graphql.py (1)

777-785: LGTM! Test follows established patterns.

The new test properly exercises the split GraphQL schema scenario and follows the same structure as existing tests in this file.

tests/data/expected/main/graphql/split_graphql_schemas.py (3)

11-26: LGTM! Scalar type aliases are correctly defined.

The TypeAlias definitions for GraphQL scalar types (Boolean, Int, String) properly map to Python built-in types with appropriate docstrings.


29-43: LGTM! Model definitions with circular dependencies are correct.

The Pydantic models correctly represent the GraphQL types with proper forward references. The circular dependency between Bar and Foo is a valid GraphQL pattern, and the typename__ fields with Literal types and __typename aliases are correctly implemented.


46-46: LGTM! Forward reference resolution is correctly applied.

Only Bar.update_forward_refs() is needed because Bar references Foo before Foo is defined, while Foo.baz: Bar can reference Bar directly since Bar is already defined at that point. This is the correct approach.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2873 will not alter performance

Comparing siminn-arnorgj:support-combined-graphql-schema (8987fbe) with main (7486e2a)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 11 untouched
⏩ 98 skipped1

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bb8921e and 8987fbe.

📒 Files selected for processing (1)
  • src/datamodel_code_generator/parser/graphql.py
🧰 Additional context used
🧬 Code graph analysis (1)
src/datamodel_code_generator/parser/graphql.py (1)
src/datamodel_code_generator/parser/base.py (2)
  • parse (3050-3130)
  • iter_source (988-1011)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: 3.10 on Windows
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: py312-black24 on Ubuntu
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.11 on Windows
  • GitHub Check: benchmarks
  • GitHub Check: Analyze (python)
🔇 Additional comments (2)
src/datamodel_code_generator/parser/graphql.py (2)

39-45: LGTM!

The Path and ParseResult type imports are correctly placed under TYPE_CHECKING for type annotation purposes without runtime overhead.


505-508: LGTM!

The parsing loop correctly processes all GraphQL types from the unified schema in the defined order. The fix properly addresses the issue where cross-file type extensions failed by merging schemas before parsing.

Comment thread src/datamodel_code_generator/parser/graphql.py
@koxudaxi koxudaxi merged commit efe8dfa into koxudaxi:main Dec 31, 2025
38 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 1, 2026

🎉 Released in 0.51.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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GraphQL parser does not handle multiple input schemas like other tools

2 participants