fix(@nestjs/graphql): de-duplicate per-target metadata in TargetMetadataCollection#3960
Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom Apr 28, 2026
Conversation
…ataCollection `@ObjectType()` (and `@ArgsType()`) call `addObjectTypeMetadata` / `addArgsMetadata` eagerly so resolvers can read the type name synchronously, and again through `LazyMetadataStorage.store(...)` so the metadata is rebuildable. The collection's `set objectType` / `set argumentType` / `set inputType` / `set resolver` accessors blindly pushed onto the canonical `all.<kind>` arrays each time, so every class ended up listed twice and the schema generator processed it twice. Replace the value in place when the same target is re-set so the canonical list always contains exactly one entry per target while still exposing the latest metadata reference.
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
TargetMetadataCollection'sobjectType,argumentType,inputTypeandresolversetters idempotent: when the same target's metadata is re-assigned, replace the entry in the canonicalall.<kind>array in place instead of pushing a second one.Bug
@ObjectType()registers metadata both eagerly (sogetObjectTypeMetadataByTarget(...)works synchronously inside@Resolver(() => Cat)) and again throughLazyMetadataStorage.store(...). Each registration goes through:So every class decorated with
@ObjectType()(or@ArgsType(), which also pairs an eager + lazy registration) ended up listed twice ingetObjectTypesMetadata(). The downstreamgenerateObjectTypeDefsthen ran the type-definition factory once per duplicate.addObjectTypes(Map.set) hid the resulting collision but the per-target metadata compilation, plugin metadata loading, and orphan walks still all ran twice.The same shape applies to
@ArgsType()(also eager + lazy) and to@InputType()oncePickType(or other helpers) double-decorate a generated abstract class.Fix
The setters now look up the previous value in the canonical array and replace it in place, falling back to a push only when the target has not been registered yet:
This keeps the
all.<kind>arrays as a one-entry-per-target list while still exposing the most recent metadata reference.interfacealready usesMap.setkeyed bytargetand is unchanged.Test plan
tests/schema-builder/storages/target-metadata-set.spec.ts(fails on master; passes after the fix) covers@ObjectTypeand@ArgsType.packages/graphqlVitest run is green (the pre-existingmodel-class-visitor/readonly-visitorplugin snapshot failures already exist on master).packages/apolloe2e run (tests/e2e) passes includingserialized-graph.