Skip to content

[plugin-helpers] fix: guard isUsingTypes against union and interface types without fields (dotansimha/graphql-code-generator-community#934) - #11057

Open
agustin18 wants to merge 1 commit into
dotansimha:masterfrom
agustin18:fix/is-using-types-union-and-interface
Open

agustin18 wants to merge 1 commit into
dotansimha:masterfrom
agustin18:fix/is-using-types-union-and-interface

Conversation

@agustin18

Copy link
Copy Markdown

Description

In isUsingTypes, when an operation contains subselections on a field whose parent type is a GraphQLUnionType (or any non-object/non-interface type), lastType.getFields() throws:

TypeError: lastType.getFields is not a function

This unhandled error is caught by presets such as near-operation-file and masked as a generic Unable to validate GraphQL document!, preventing real GraphQL validation errors from reaching the user.

Additionally, when resolving fields on GraphQLInterfaceType instances:

  • In Field.enter, isObjectType(lastType) returned false, skipping nullability checks and potentially undercounting foundFields.

This PR:

  1. Types typesStack as Array<GraphQLNamedType | undefined> instead of GraphQLObjectType[], removing the as any cast on typesStack.push(...).
  2. In the Kind.FIELD branch of SelectionSet.enter, guards lastType with isObjectType(lastType) || isInterfaceType(lastType) before accessing .getFields(), safely returning null when the parent cannot define fields.
  3. In Kind.INLINE_FRAGMENT, safely resolves typesStack[typesStack.length - 1]?.name || null.
  4. In Field.enter, updates the check to isObjectType(lastType) || isInterfaceType(lastType) so interface fields are properly recognized.

Related dotansimha/graphql-code-generator-community#934

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added regression unit tests in packages/utils/plugins-helpers/tests/is-using-types.spec.ts:
    • community #934 - should not crash when selecting fields on union type
    • should correctly detect types and nullable fields on interface types
    • should handle untyped inline fragments on unions without error
  • Ran targeted vitest suite (pnpm vitest run packages/utils/plugins-helpers/tests/is-using-types.spec.ts) - 11/11 tests passing.
  • Ran full pnpm types:check and pnpm prettier:check.

Test Environment:

  • OS: Linux (Ubuntu 24.04)
  • @graphql-codegen/plugin-helpers: 7.4.2
  • NodeJS: 24.12.0

Checklist:

  • I have followed the CONTRIBUTING doc and the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@changeset-bot

changeset-bot Bot commented Oct 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: aa670b0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/plugin-helpers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
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