Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(@nestjs/graphql): keep class directive when a field has the same SDL
`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.
  • Loading branch information
yogeshwaran-c committed Apr 22, 2026
commit eb9582dfec7ea76f6d33fa7205f4ee96c6e83b3a
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ export class TypeMetadataStorageHost {

addDirectiveMetadata(metadata: ClassDirectiveMetadata) {
const classMetadata = this.metadataByTargetCollection.get(metadata.target);
if (!classMetadata.fieldDirectives.sdls.has(metadata.sdl)) {
const isDuplicate = classMetadata.classDirectives
.getAll()
.some((directive) => directive.sdl === metadata.sdl);
if (!isDuplicate) {
classMetadata.classDirectives.push(metadata);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Test } from '@nestjs/testing';
import { GraphQLObjectType } from 'graphql';
import {
Directive,
Field,
GraphQLSchemaBuilderModule,
GraphQLSchemaFactory,
ID,
ObjectType,
Query,
Resolver,
TypeMetadataStorage,
} from '../../../lib';

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

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

@Resolver(() => Account)
class AccountResolver {
@Query(() => Account)
me(): Account {
return null;
}
}

describe('Class-level directive not shadowed by identical field-level directive', () => {
let accountType: GraphQLObjectType;

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [GraphQLSchemaBuilderModule],
}).compile();

const schemaFactory = moduleRef.get(GraphQLSchemaFactory);
const schema = await schemaFactory.create([AccountResolver]);
accountType = schema.getType('Account') as GraphQLObjectType;
});

afterAll(() => {
TypeMetadataStorage.clear();
});

it('should keep the class-level directive even when a field declares the same SDL', () => {
const classDirectives = accountType.astNode?.directives ?? [];
const classTagNames = classDirectives
.filter((d) => d.name.value === 'tag')
.map(
(d) =>
(d.arguments?.[0]?.value as { value?: string } | undefined)?.value,
);
expect(classTagNames).toContain('public');
});

it('should still keep the field-level directive', () => {
const emailField = accountType.getFields().email;
const fieldDirectives = emailField.astNode?.directives ?? [];
const fieldTagNames = fieldDirectives
.filter((d) => d.name.value === 'tag')
.map(
(d) =>
(d.arguments?.[0]?.value as { value?: string } | undefined)?.value,
);
expect(fieldTagNames).toContain('public');
});
});