Skip to content

fix(@nestjs/graphql): validate registerEnumType/createUnionType options eagerly#3963

Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom
yogeshwaran-c:fix/enum-union-options-validation
Apr 28, 2026
Merged

fix(@nestjs/graphql): validate registerEnumType/createUnionType options eagerly#3963
kamilmysliwiec merged 1 commit intonestjs:masterfrom
yogeshwaran-c:fix/enum-union-options-validation

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

Summary

  • Throw a descriptive error when registerEnumType or createUnionType are called without an options object, without a non-empty name, or (for unions) without a types function.
  • Validation runs at registration time so the stack trace points at the offending call site instead of LazyMetadataStorage.load -> addEnumMetadata deep in the schema-builder pipeline.

Bug

The TypeScript signatures mark these inputs as optional/permissive but the lazy callbacks dereference them unguarded. Calling registerEnumType(MyEnum) therefore blew up with:

TypeError: Cannot read properties of undefined (reading 'name')
  at LazyMetadataStorage.load
  at GraphQLSchemaFactory.create
  ...

createUnionType({ types: () => [...] }) (no name) silently registered a Symbol(undefined) and surfaced as a "Schema must have a name" failure inside graphql-js much later.

Fix

  • registerEnumType: require options and require options.name to be a non-empty string.
  • createUnionType: same name check, plus typeof options.types === 'function'.
  • Both errors now identify the failing API and show a corrected example in the message.

Test plan

  • New spec tests/type-factories/enum-union-options-validation.spec.ts covers each missing-input branch and the happy path.
  • tests/schema-builder/**, tests/type-helpers/**, tests/type-factories/** continue to pass.

…ns eagerly

`registerEnumType(MyEnum)` and `createUnionType({ types: () => [...] })`
both compiled fine but blew up later inside `LazyMetadataStorage.load`
with `Cannot read properties of undefined (reading 'name')` (or registered
a nameless union via `Symbol(undefined)`), pointing at internals far from
the user's call site. Validate the inputs at decorator time so the error
message names the missing field and is raised next to the offending
registration.

For `createUnionType`, also assert that `options.types` is a function so
the "types is not a function" failure surfaces at registration rather
than at schema-build time.
@kamilmysliwiec kamilmysliwiec merged commit badb861 into nestjs:master Apr 28, 2026
1 check passed
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.

2 participants