From 4d8b5aea80eda980b870cd19970fdb7d78eb15f0 Mon Sep 17 00:00:00 2001 From: riley-pikus Date: Wed, 20 Aug 2025 16:07:10 -0500 Subject: [PATCH] feat(@nestjs/graphql): Allow InputType Deprecation (code first) --- .../input-type-definition.factory.ts | 1 + .../input-type-definition.factory.spec.ts | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 packages/graphql/tests/schema-builder/factories/input-type-definition.factory.spec.ts diff --git a/packages/graphql/lib/schema-builder/factories/input-type-definition.factory.ts b/packages/graphql/lib/schema-builder/factories/input-type-definition.factory.ts index 788a3ba76..212d3b784 100644 --- a/packages/graphql/lib/schema-builder/factories/input-type-definition.factory.ts +++ b/packages/graphql/lib/schema-builder/factories/input-type-definition.factory.ts @@ -80,6 +80,7 @@ export class InputTypeDefinitionFactory { description: property.description, type, defaultValue: property.options.defaultValue, + deprecationReason: property.deprecationReason, /** * AST node has to be manually created in order to define directives * (more on this topic here: https://github.com/graphql/graphql-js/issues/1343) diff --git a/packages/graphql/tests/schema-builder/factories/input-type-definition.factory.spec.ts b/packages/graphql/tests/schema-builder/factories/input-type-definition.factory.spec.ts new file mode 100644 index 000000000..a0035d4e4 --- /dev/null +++ b/packages/graphql/tests/schema-builder/factories/input-type-definition.factory.spec.ts @@ -0,0 +1,56 @@ +import { Test } from '@nestjs/testing'; +import { + Field, + InputType, + GraphQLSchemaBuilderModule, + TypeMetadataStorage, +} from '../../../lib'; +import { TypeDefinitionsGenerator } from '../../../lib/schema-builder/type-definitions.generator'; +import { TypeDefinitionsStorage } from '../../../lib/schema-builder/storages/type-definitions.storage'; +import { LazyMetadataStorage } from '../../../lib/schema-builder/storages/lazy-metadata.storage'; + +@InputType('DeprecationInput') +class DeprecationInput { + @Field(() => String, { description: 'regular' }) + regular!: string; + + @Field(() => String, { + description: 'deprecated', + deprecationReason: 'use something else', + }) + oldField!: string; +} + +describe('InputTypeDefinitionFactory (deprecation)', () => { + let generator: TypeDefinitionsGenerator; + let storage: TypeDefinitionsStorage; + + beforeAll(async () => { + // Register metadata for the input type + LazyMetadataStorage.load([DeprecationInput]); + TypeMetadataStorage.compile(); + + const moduleRef = await Test.createTestingModule({ + imports: [GraphQLSchemaBuilderModule], + }).compile(); + + generator = moduleRef.get(TypeDefinitionsGenerator); + storage = moduleRef.get(TypeDefinitionsStorage); + }); + + afterAll(() => { + TypeMetadataStorage.clear(); + }); + + it('should mark input fields as deprecated if value is set', () => { + // Generate type definitions (inputs included) + generator.generate({} as any); + + const inputType: any = storage.getInputTypeAndExtract(DeprecationInput); + expect(inputType).toBeDefined(); + + const fields = inputType.getFields(); + expect(fields.regular.deprecationReason).toBeUndefined(); + expect(fields.oldField.deprecationReason).toBe('use something else'); + }); +});