From c50afd87d259fc185b0e9c00709cc67623209a17 Mon Sep 17 00:00:00 2001 From: Kasir Barati Date: Sun, 26 Jan 2025 10:55:53 +0100 Subject: [PATCH 1/3] chore: add deprecationReason to relation decorators --- .prettierignore | 3 ++- .../resolvers/relations/read-relations.resolver.ts | 14 ++++++++++++-- .../src/resolvers/relations/relations.interface.ts | 12 +++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/.prettierignore b/.prettierignore index 8f92f6248..1c83f7348 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,5 @@ .prettierignore .docusaurus/ /.nx/cache -/.nx/workspace-data \ No newline at end of file +/.nx/workspace-data +**/examples/**/*.gql \ No newline at end of file diff --git a/packages/query-graphql/src/resolvers/relations/read-relations.resolver.ts b/packages/query-graphql/src/resolvers/relations/read-relations.resolver.ts index b2841c79f..7bd109046 100644 --- a/packages/query-graphql/src/resolvers/relations/read-relations.resolver.ts +++ b/packages/query-graphql/src/resolvers/relations/read-relations.resolver.ts @@ -37,7 +37,12 @@ const ReadOneRelationMixin = @ResolverField( baseNameLower, () => relationDTO, - { nullable: relation.nullable, complexity: relation.complexity, description: relation?.description }, + { + nullable: relation.nullable, + complexity: relation.complexity, + description: relation?.description, + deprecationReason: relation?.deprecationReason + }, commonResolverOpts, { interceptors: [AuthorizerInterceptor(DTOClass)] } ) @@ -107,7 +112,12 @@ const ReadManyRelationMixin = @ResolverField( baseNameLower, () => CT.resolveType, - { nullable: relation.nullable, complexity: relation.complexity, description: relation?.description }, + { + nullable: relation.nullable, + complexity: relation.complexity, + description: relation?.description, + deprecationReason: relation?.deprecationReason + }, commonResolverOpts, { interceptors: [AuthorizerInterceptor(DTOClass)] } ) diff --git a/packages/query-graphql/src/resolvers/relations/relations.interface.ts b/packages/query-graphql/src/resolvers/relations/relations.interface.ts index 0259585de..50f51a146 100644 --- a/packages/query-graphql/src/resolvers/relations/relations.interface.ts +++ b/packages/query-graphql/src/resolvers/relations/relations.interface.ts @@ -1,4 +1,4 @@ -import { Complexity } from '@nestjs/graphql' +import { Complexity, FieldOptions } from '@nestjs/graphql' import { Class } from '@ptc-org/nestjs-query-core' import { AuthorizerOptions } from '../../auth' @@ -63,13 +63,6 @@ export type ResolverRelation = { */ allowFiltering?: boolean - /** - * Description of the relation. - */ - description?: string - - complexity?: Complexity - update?: Pick, 'description'> & ResolverRelationMethodOpts remove?: Pick, 'description'> & ResolverRelationMethodOpts /** @@ -82,7 +75,8 @@ export type ResolverRelation = { } & DTONamesOpts & ResolverMethodOpts & QueryArgsTypeOpts & - Pick + Pick & + Omit export type RelationTypeMap = Record From 1769f7a3629f79c334253026a7f527cb4a77f90e Mon Sep 17 00:00:00 2001 From: Kasir Barati Date: Sun, 26 Jan 2025 11:22:24 +0100 Subject: [PATCH 2/3] chore: add unit test for deprecationReason --- .../decorators/relation.decorator.spec.ts | 169 +++++++++++++++++- 1 file changed, 168 insertions(+), 1 deletion(-) diff --git a/packages/query-graphql/__tests__/decorators/relation.decorator.spec.ts b/packages/query-graphql/__tests__/decorators/relation.decorator.spec.ts index 85d83e4fb..a3366a017 100644 --- a/packages/query-graphql/__tests__/decorators/relation.decorator.spec.ts +++ b/packages/query-graphql/__tests__/decorators/relation.decorator.spec.ts @@ -9,7 +9,14 @@ import { UnPagedRelation } from '@ptc-org/nestjs-query-graphql' -import { CursorConnection, FilterableCursorConnection, FilterableOffsetConnection, getRelations } from '../../src/decorators' +import { + CursorConnection, + FilterableCursorConnection, + FilterableOffsetConnection, + getRelations, + RelationOneDecoratorOpts +} from '../../src/decorators' +import { RelationManyDecoratorOpts } from 'packages/query-graphql/src/decorators/relation.decorator' @ObjectType() class TestRelation {} @@ -26,6 +33,23 @@ describe('@Relation', () => { const relations = getRelations(TestDTO) expect(relations).toEqual({ one: { test: { DTO: TestRelation, allowFiltering: false, ...relationOpts } } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @Relation('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ one: { test: { DTO: TestRelation, allowFiltering: false, ...relationOpts } } }) + }) }) describe('@FilterableRelation', () => { @@ -40,6 +64,23 @@ describe('@FilterableRelation', () => { const relations = getRelations(TestDTO) expect(relations).toEqual({ one: { test: { DTO: TestRelation, ...relationOpts, allowFiltering: true } } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @FilterableRelation('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ one: { test: { DTO: TestRelation, ...relationOpts, allowFiltering: true } } }) + }) }) describe('@UnPagedRelation', () => { @@ -58,6 +99,27 @@ describe('@UnPagedRelation', () => { } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @UnPagedRelation('tests', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ + many: { + tests: { DTO: TestRelation, ...relationOpts, allowFiltering: false, pagingStrategy: PagingStrategies.NONE } + } + }) + }) }) describe('@FilterableUnPagedRelation', () => { @@ -76,6 +138,27 @@ describe('@FilterableUnPagedRelation', () => { } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @FilterableUnPagedRelation('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ + many: { + test: { DTO: TestRelation, pagingStrategy: PagingStrategies.NONE, ...relationOpts, allowFiltering: true } + } + }) + }) }) describe('@OffsetConnection', () => { @@ -94,6 +177,27 @@ describe('@OffsetConnection', () => { } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @OffsetConnection('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ + many: { + test: { DTO: TestRelation, ...relationOpts, allowFiltering: false, pagingStrategy: PagingStrategies.OFFSET } + } + }) + }) }) describe('@FilterableOffsetConnection', () => { @@ -112,6 +216,27 @@ describe('@FilterableOffsetConnection', () => { } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @FilterableOffsetConnection('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ + many: { + test: { DTO: TestRelation, ...relationOpts, pagingStrategy: PagingStrategies.OFFSET, allowFiltering: true } + } + }) + }) }) describe('@CursorConnection', () => { @@ -130,6 +255,27 @@ describe('@CursorConnection', () => { } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @CursorConnection('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ + many: { + test: { DTO: TestRelation, ...relationOpts, allowFiltering: false, pagingStrategy: PagingStrategies.CURSOR } + } + }) + }) }) describe('@FilterableCursorConnection', () => { @@ -148,6 +294,27 @@ describe('@FilterableCursorConnection', () => { } }) }) + + it('should add the deprecationReason to the options', () => { + // Arrange + const relationFn = () => TestRelation + const relationOpts: Partial> = { + deprecationReason: 'Deprecated for no apparent reason.' + } + @ObjectType() + @FilterableCursorConnection('test', relationFn, relationOpts) + class TestDTO {} + + // Act + const relations = getRelations(TestDTO) + + // Assert + expect(relations).toEqual({ + many: { + test: { DTO: TestRelation, ...relationOpts, pagingStrategy: PagingStrategies.CURSOR, allowFiltering: true } + } + }) + }) }) describe('getRelations', () => { From 3e01f6ebd0e724b11b132197a75f5b05cc340235 Mon Sep 17 00:00:00 2001 From: Kasir Barati Date: Sun, 26 Jan 2025 12:33:44 +0100 Subject: [PATCH 3/3] chore: add description back for JSDoc sake --- .../src/resolvers/relations/relations.interface.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/query-graphql/src/resolvers/relations/relations.interface.ts b/packages/query-graphql/src/resolvers/relations/relations.interface.ts index 50f51a146..9ae3ebd4a 100644 --- a/packages/query-graphql/src/resolvers/relations/relations.interface.ts +++ b/packages/query-graphql/src/resolvers/relations/relations.interface.ts @@ -63,6 +63,11 @@ export type ResolverRelation = { */ allowFiltering?: boolean + /** + * Description of the relation. + */ + description?: string + update?: Pick, 'description'> & ResolverRelationMethodOpts remove?: Pick, 'description'> & ResolverRelationMethodOpts /** @@ -76,7 +81,7 @@ export type ResolverRelation = { ResolverMethodOpts & QueryArgsTypeOpts & Pick & - Omit + Omit export type RelationTypeMap = Record