Skip to content

fix(schema): add UNION type support to query generation#266

Open
pranav-new-relic wants to merge 1 commit intomainfrom
fix/union-type-inline-fragments
Open

fix(schema): add UNION type support to query generation#266
pranav-new-relic wants to merge 1 commit intomainfrom
fix/union-type-inline-fragments

Conversation

@pranav-new-relic
Copy link
Copy Markdown
Member

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.go only handled KindObject and KindInterface:

case KindObject, KindInterface:

UNION types fell through to the default case, which outputs just the field name without expansion.

The Fix

  1. Added KindUnion constant to internal/schema/kind.go
  2. Updated switch case to include KindUnion: case KindObject, KindInterface, KindUnion:
  3. Updated __typename generation to include UNION types (line 159)

This enables the existing possibleTypes expansion code (lines 176-203) to properly generate inline fragments for UNION types.

Impact

Before

auth
errors

After

auth {
  __typename
  ... on AiNotificationsBasicAuth {
    __typename
    authType
    user
  }
  ... on AiNotificationsTokenAuth {
    __typename
    authType
    prefix
  }
  ... on AiNotificationsOAuth2Auth {
    __typename
    accessTokenUrl
    authType
    authorizationUrl
    clientId
    prefix
    refreshInterval
    refreshable
    scope
  }
  ... on AiNotificationsCustomHeadersAuth {
    __typename
    authType
  }
}

Verification

Tested against newrelic-client-go with 34 UNION types in schema:

Notifications package: 8 operations (6 mutations, 2 queries)

  • All AiNotificationsAuth and AiNotificationsError fields properly expanded
  • 44 inline fragments, 55 __typename fields generated
  • Package builds successfully

Workflows package: AiWorkflowsConfiguration UNION type

  • Inline fragments now properly generated
  • Package builds successfully

Historical Context

This bug has existed since UNION type support was partially added in commit 298d65c (Nov 2022). That commit added:

  • KindUnion for type generation in golang.go
  • But NOT for query generation in type.go

Developers have worked around this with manual edits:

  • Commit 5a6de0e3 (July 2022): "fix(destinations): add integration tests + small fix for union type"
  • Commit eb4e78ea (April 2024): Manually reverted simplified auth fields

This fix eliminates the need for manual workarounds going forward.

Testing

  • Regenerated notifications package with 8 operations
  • Regenerated workflows package
  • Verified inline fragments in all queries/mutations
  • Verified packages compile successfully
  • Verified against all 34 UNION types in schema

🤖 Generated with Claude Code

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>
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.

1 participant