Skip to content

**Improve handling of duplicate types across schema files to avoid HC…#9836

Open
andreashellquist wants to merge 4 commits into
ChilliCream:mainfrom
andreashellquist:duplicate-type-name-fix
Open

**Improve handling of duplicate types across schema files to avoid HC…#9836
andreashellquist wants to merge 4 commits into
ChilliCream:mainfrom
andreashellquist:duplicate-type-name-fix

Conversation

@andreashellquist

@andreashellquist andreashellquist commented Jun 3, 2026

Copy link
Copy Markdown

Summary of Changes

I've identified and fixed the HC0065 bug in StrawberryShake's code generation pipeline. The issue manifested in two main areas.

#9837


1. SchemaHelper.Load — HC0065 Duplicate Type Name Fix

File:
src/StrawberryShake/CodeGeneration/src/CodeGeneration/Utilities/SchemaHelper.cs

Root Cause

When multiple schema files contain overlapping type definitions (common with Hygraph/CMS schemas where glob patterns may pick up multiple files, or when schemas are split across files), SchemaHelper.Load previously called:

builder.AddDocument(document);

for each non-extension file independently.

This caused the Hot Chocolate type system to register the same type name multiple times, triggering the HC0065 (duplicate type name) error.

Additionally, documents that contained both extension nodes and regular type definitions were treated entirely as extension files, causing the regular type definitions in those mixed files to be silently ignored.

Fix

Merge and Deduplicate Type Definitions

All non-extension definitions from all schema files are now merged into a single deduplicated document before calling builder.AddDocument().

  • Deduplication is keyed by type name.
  • Uses TryAdd(...).
  • First occurrence wins.

Correctly Process Mixed Files

Files containing both:

  • extension nodes
  • regular type definitions

are now processed correctly, ensuring both categories are included.

Deduplicate Scalar Registrations

Added a HashSet<string> to track already-registered scalars and prevent duplicate AddType(...) calls.

Exclude Scalars from the Merged Document

Scalar type definitions are already registered through:

builder.AddType(new AnyType(...))

Therefore, scalar definitions are excluded from the merged document to avoid potential registration conflicts.


2. TypeDescriptorMapper.ExtractRuntimeType — SS0009 Name Collision Resilience

File:
src/StrawberryShake/CodeGeneration/src/CodeGeneration/Mappers/TypeDescriptorMapper.cs

Root Cause

When generated C# output type names collided with existing registered type names (possible in large schemas with many similarly named types), the code would immediately throw a TypeNameCollision (SS0009) exception.

Fix

Instead of failing immediately, the code now attempts alternative names by appending numeric suffixes:

  • _1
  • _2
  • _3
  • ...

This mirrors the behavior of:

DocumentAnalyzerContext.ResolveTypeName(...)

and makes code generation more resilient when working with large or complex schemas.


3. Tests

File:
src/StrawberryShake/CodeGeneration/test/CodeGeneration.Tests/Utilities/SchemaHelperTests.cs

Added Test Cases

Test Description
Load_DuplicateTypesAcrossFiles_DoesNotThrowHC0065 Verifies that overlapping type definitions across multiple schema files no longer trigger HC0065.
Load_MixedExtensionAndDefinitionFile_ProcessesBoth Verifies that files containing both extensions and regular type definitions correctly process both.

Resulting Behavior Changes

Duplicate Types Across Schema Files

Before

  • Duplicate type definitions caused HC0065.

After

  • Duplicate definitions are deduplicated.
  • First occurrence wins.

Mixed Extension + Definition Files

Before

  • Only extensions were processed.
  • Regular type definitions were ignored.

After

  • Both extensions and definitions are processed.

Runtime Type Name Collisions

Before

  • Name collisions immediately caused SS0009.

After

  • Alternative names are automatically generated using numeric suffixes before failing.
  • Code generation is more resilient for large schemas.

Copilot AI review requested due to automatic review settings June 3, 2026 13:38
@CLAassistant

CLAassistant commented Jun 3, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Improves schema loading and type mapping resiliency when working with multiple GraphQL SDL files that may overlap or contain mixed extensions/definitions, and adds tests covering these scenarios.

Changes:

  • Deduplicate non-extension schema definitions across multiple documents to avoid duplicate-type errors during schema load.
  • Handle scalar registration and mixed extension/definition documents during SchemaHelper.Load.
  • Add collision-avoidance logic for generated runtime type names and new unit tests for duplicate/mixed schema inputs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
src/StrawberryShake/CodeGeneration/test/CodeGeneration.Tests/Utilities/SchemaHelperTests.cs Adds tests for duplicate type definitions across files and mixed extension+definition documents.
src/StrawberryShake/CodeGeneration/src/CodeGeneration/Utilities/SchemaHelper.cs Merges and deduplicates schema definitions across files; changes scalar registration handling.
src/StrawberryShake/CodeGeneration/src/CodeGeneration/Mappers/TypeDescriptorMapper.cs Adds retry logic to avoid runtime type name collisions by appending a numeric suffix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/StrawberryShake/CodeGeneration/src/CodeGeneration/Utilities/SchemaHelper.cs Outdated

@michaelstaib michaelstaib left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't really solve the problem and could lead to a plethora of issues.

@andreashellquist

andreashellquist commented Jun 4, 2026

Copy link
Copy Markdown
Author

This doesn't really solve the problem and could lead to a plethora of issues.

Thanks for the review. I'm primarily a user of the package who hit this bug directly when trying to use it — I don't have deep knowledge of the codebase architecture, just gave copilot a chance to sort it out. Happy to close this PR if the team prefers to solve it differently. The issue and repro repo are there for reference. I've reverted the TypeDescriptorMapper change. Could you clarify what you see as the proper approach? Should the deduplication happen upstream in the CLI download step, or would you prefer a validation-with-clear-error rather than silent dedup? Happy to adjust

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.

4 participants