Skip to content

Commit

Permalink
chore: add deprecationReason to relation decorators (#352)
Browse files Browse the repository at this point in the history
Closes #350

@TriPSs I did not add e2e tests for it, do you think it is needed? I
tested it manually and confirmed that it will add `@deprecated`
directive to the field.
  • Loading branch information
TriPSs authored Jan 30, 2025
2 parents 7b72ab8 + 3e01f6e commit 7cff3b2
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.prettierignore
.docusaurus/
/.nx/cache
/.nx/workspace-data
/.nx/workspace-data
**/examples/**/*.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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<RelationOneDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -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<RelationOneDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -58,6 +99,27 @@ describe('@UnPagedRelation', () => {
}
})
})

it('should add the deprecationReason to the options', () => {
// Arrange
const relationFn = () => TestRelation
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -76,6 +138,27 @@ describe('@FilterableUnPagedRelation', () => {
}
})
})

it('should add the deprecationReason to the options', () => {
// Arrange
const relationFn = () => TestRelation
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -94,6 +177,27 @@ describe('@OffsetConnection', () => {
}
})
})

it('should add the deprecationReason to the options', () => {
// Arrange
const relationFn = () => TestRelation
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -112,6 +216,27 @@ describe('@FilterableOffsetConnection', () => {
}
})
})

it('should add the deprecationReason to the options', () => {
// Arrange
const relationFn = () => TestRelation
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -130,6 +255,27 @@ describe('@CursorConnection', () => {
}
})
})

it('should add the deprecationReason to the options', () => {
// Arrange
const relationFn = () => TestRelation
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
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', () => {
Expand All @@ -148,6 +294,27 @@ describe('@FilterableCursorConnection', () => {
}
})
})

it('should add the deprecationReason to the options', () => {
// Arrange
const relationFn = () => TestRelation
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)] }
)
Expand Down Expand Up @@ -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)] }
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -68,8 +68,6 @@ export type ResolverRelation<Relation> = {
*/
description?: string

complexity?: Complexity

update?: Pick<ResolverRelation<Relation>, 'description'> & ResolverRelationMethodOpts
remove?: Pick<ResolverRelation<Relation>, 'description'> & ResolverRelationMethodOpts
/**
Expand All @@ -82,7 +80,8 @@ export type ResolverRelation<Relation> = {
} & DTONamesOpts &
ResolverMethodOpts &
QueryArgsTypeOpts<Relation> &
Pick<ConnectionOptions, 'enableTotalCount'>
Pick<ConnectionOptions, 'enableTotalCount'> &
Omit<FieldOptions, 'name' | 'description' | 'middleware'>

export type RelationTypeMap<RT> = Record<string, RT>

Expand Down

0 comments on commit 7cff3b2

Please sign in to comment.