|
| 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