Skip to content

fix(@nestjs/graphql): keep class directive when a field has the same SDL#3952

Merged
kamilmysliwiec merged 1 commit intonestjs:masterfrom
yogeshwaran-c:fix/class-directive-shadowed-by-field
Apr 22, 2026
Merged

fix(@nestjs/graphql): keep class directive when a field has the same SDL#3952
kamilmysliwiec merged 1 commit intonestjs:masterfrom
yogeshwaran-c:fix/class-directive-shadowed-by-field

Conversation

@yogeshwaran-c
Copy link
Copy Markdown
Contributor

@yogeshwaran-c yogeshwaran-c commented Apr 22, 2026

Summary

  • Fix addDirectiveMetadata silently dropping a class-level @Directive(sdl) whenever any field on the same class already has a property @Directive(sdl) with the same SDL.
  • Dedupe against the class's own directive list instead, preserving the original "no exact duplicates" intent but without the cross-contamination between the class and its fields.

Bug

@ObjectType()
@Directive('@tag(name: "public")')
class Account {
  @Field(() => ID) id: string;

  @Field()
  @Directive('@tag(name: "public")')
  email: string;
}

Because decorators run field-before-class, fieldDirectives.sdls already contained @tag(name: "public") by the time the class decorator fired, and the class directive was skipped. The emitted SDL therefore lacked the class-level @tag, which breaks Apollo Contracts / federation tagging patterns that tag both the entity and one of its fields.

Fix

Check classMetadata.classDirectives for an existing SDL match instead of the field-directive set. This still prevents stacking the exact same class directive twice on the same target, but decouples the check from field-directive state.

Test plan

`addDirectiveMetadata` deduplicated class-level directives against the
set of SDLs already seen on *field* directives, so a class directive
was silently dropped whenever any field on the same class had been
annotated with the same SDL string. This broke common patterns like
tagging both a class and one of its fields with
`@tag(name: "public")` for Apollo Contracts.

Dedupe against the class's own directive list instead, preserving the
original intent of preventing exact duplicates on the same target.
@yogeshwaran-c yogeshwaran-c force-pushed the fix/class-directive-shadowed-by-field branch from 20fb051 to eb9582d Compare April 22, 2026 06:33
@kamilmysliwiec kamilmysliwiec merged commit 53eff5f into nestjs:master Apr 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants