fix(@nestjs/graphql): validate registerEnumType/createUnionType options eagerly#3963
Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom Apr 28, 2026
Conversation
…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.
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
registerEnumTypeorcreateUnionTypeare called without an options object, without a non-emptyname, or (for unions) without atypesfunction.LazyMetadataStorage.load->addEnumMetadatadeep 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:createUnionType({ types: () => [...] })(no name) silently registered aSymbol(undefined)and surfaced as a "Schema must have a name" failure inside graphql-js much later.Fix
registerEnumType: requireoptionsand requireoptions.nameto be a non-empty string.createUnionType: same name check, plustypeof options.types === 'function'.Test plan
tests/type-factories/enum-union-options-validation.spec.tscovers each missing-input branch and the happy path.tests/schema-builder/**,tests/type-helpers/**,tests/type-factories/**continue to pass.