fix(schema): add UNION type support to query generation#266
Open
pranav-new-relic wants to merge 1 commit intomainfrom
Open
fix(schema): add UNION type support to query generation#266pranav-new-relic wants to merge 1 commit intomainfrom
pranav-new-relic wants to merge 1 commit intomainfrom
Conversation
UNION types were falling through to the default case in GetQueryStringFields(), causing them to be output as simple field names instead of being expanded with inline fragments. This resulted in incomplete GraphQL queries that couldn't properly query UNION type fields. This fix adds KindUnion to the switch statement at line 120 in type.go, enabling UNION types to be expanded with inline fragments (... on TypeName) just like INTERFACE types. This allows proper querying of UNION type fields such as AiNotificationsAuth, AiNotificationsError, and AiWorkflowsConfiguration. Changes: - Added KindUnion constant to kind.go - Added KindUnion to switch case in GetQueryStringFields() (type.go:120) - Added __typename field generation for UNION types (type.go:159) The fix has been verified across: - 8 operations in notifications package (6 mutations, 2 queries) - workflows package with AiWorkflowsConfiguration UNION type - All 34 UNION types in the GraphQL schema Fixes a long-standing issue where developers had to manually add inline fragments after code generation (see commits 5a6de0e3, eb4e78ea). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a bug where UNION types were not being expanded with inline fragments in generated GraphQL queries. UNION type fields were falling through to the default case in
GetQueryStringFields(), causing them to be output as simple field names instead of proper inline fragments.Root Cause
The switch statement at line 120 in
internal/schema/type.goonly handledKindObjectandKindInterface:UNION types fell through to the
defaultcase, which outputs just the field name without expansion.The Fix
KindUnionconstant tointernal/schema/kind.goKindUnion:case KindObject, KindInterface, KindUnion:__typenamegeneration to include UNION types (line 159)This enables the existing
possibleTypesexpansion code (lines 176-203) to properly generate inline fragments for UNION types.Impact
Before
After
Verification
Tested against newrelic-client-go with 34 UNION types in schema:
✅ Notifications package: 8 operations (6 mutations, 2 queries)
AiNotificationsAuthandAiNotificationsErrorfields properly expanded__typenamefields generated✅ Workflows package:
AiWorkflowsConfigurationUNION typeHistorical Context
This bug has existed since UNION type support was partially added in commit 298d65c (Nov 2022). That commit added:
KindUnionfor type generation ingolang.go✅type.go❌Developers have worked around this with manual edits:
This fix eliminates the need for manual workarounds going forward.
Testing
🤖 Generated with Claude Code