Skip to content

Commit 4d8b5ae

Browse files
committed
feat(@nestjs/graphql): Allow InputType Deprecation (code first)
1 parent 5be746c commit 4d8b5ae

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

packages/graphql/lib/schema-builder/factories/input-type-definition.factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export class InputTypeDefinitionFactory {
8080
description: property.description,
8181
type,
8282
defaultValue: property.options.defaultValue,
83+
deprecationReason: property.deprecationReason,
8384
/**
8485
* AST node has to be manually created in order to define directives
8586
* (more on this topic here: https://github.com/graphql/graphql-js/issues/1343)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Test } from '@nestjs/testing';
2+
import {
3+
Field,
4+
InputType,
5+
GraphQLSchemaBuilderModule,
6+
TypeMetadataStorage,
7+
} from '../../../lib';
8+
import { TypeDefinitionsGenerator } from '../../../lib/schema-builder/type-definitions.generator';
9+
import { TypeDefinitionsStorage } from '../../../lib/schema-builder/storages/type-definitions.storage';
10+
import { LazyMetadataStorage } from '../../../lib/schema-builder/storages/lazy-metadata.storage';
11+
12+
@InputType('DeprecationInput')
13+
class DeprecationInput {
14+
@Field(() => String, { description: 'regular' })
15+
regular!: string;
16+
17+
@Field(() => String, {
18+
description: 'deprecated',
19+
deprecationReason: 'use something else',
20+
})
21+
oldField!: string;
22+
}
23+
24+
describe('InputTypeDefinitionFactory (deprecation)', () => {
25+
let generator: TypeDefinitionsGenerator;
26+
let storage: TypeDefinitionsStorage;
27+
28+
beforeAll(async () => {
29+
// Register metadata for the input type
30+
LazyMetadataStorage.load([DeprecationInput]);
31+
TypeMetadataStorage.compile();
32+
33+
const moduleRef = await Test.createTestingModule({
34+
imports: [GraphQLSchemaBuilderModule],
35+
}).compile();
36+
37+
generator = moduleRef.get(TypeDefinitionsGenerator);
38+
storage = moduleRef.get(TypeDefinitionsStorage);
39+
});
40+
41+
afterAll(() => {
42+
TypeMetadataStorage.clear();
43+
});
44+
45+
it('should mark input fields as deprecated if value is set', () => {
46+
// Generate type definitions (inputs included)
47+
generator.generate({} as any);
48+
49+
const inputType: any = storage.getInputTypeAndExtract(DeprecationInput);
50+
expect(inputType).toBeDefined();
51+
52+
const fields = inputType.getFields();
53+
expect(fields.regular.deprecationReason).toBeUndefined();
54+
expect(fields.oldField.deprecationReason).toBe('use something else');
55+
});
56+
});

0 commit comments

Comments
 (0)