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
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');
});
});