Skip to content

Commit 7cff3b2

Browse files
authored
chore: add deprecationReason to relation decorators (#352)
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.
2 parents 7b72ab8 + 3e01f6e commit 7cff3b2

File tree

4 files changed

+185
-8
lines changed

4 files changed

+185
-8
lines changed

Diff for: .prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.prettierignore
22
.docusaurus/
33
/.nx/cache
4-
/.nx/workspace-data
4+
/.nx/workspace-data
5+
**/examples/**/*.gql

Diff for: packages/query-graphql/__tests__/decorators/relation.decorator.spec.ts

+168-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import {
99
UnPagedRelation
1010
} from '@ptc-org/nestjs-query-graphql'
1111

12-
import { CursorConnection, FilterableCursorConnection, FilterableOffsetConnection, getRelations } from '../../src/decorators'
12+
import {
13+
CursorConnection,
14+
FilterableCursorConnection,
15+
FilterableOffsetConnection,
16+
getRelations,
17+
RelationOneDecoratorOpts
18+
} from '../../src/decorators'
19+
import { RelationManyDecoratorOpts } from 'packages/query-graphql/src/decorators/relation.decorator'
1320

1421
@ObjectType()
1522
class TestRelation {}
@@ -26,6 +33,23 @@ describe('@Relation', () => {
2633
const relations = getRelations(TestDTO)
2734
expect(relations).toEqual({ one: { test: { DTO: TestRelation, allowFiltering: false, ...relationOpts } } })
2835
})
36+
37+
it('should add the deprecationReason to the options', () => {
38+
// Arrange
39+
const relationFn = () => TestRelation
40+
const relationOpts: Partial<RelationOneDecoratorOpts<TestRelation>> = {
41+
deprecationReason: 'Deprecated for no apparent reason.'
42+
}
43+
@ObjectType()
44+
@Relation('test', relationFn, relationOpts)
45+
class TestDTO {}
46+
47+
// Act
48+
const relations = getRelations(TestDTO)
49+
50+
// Assert
51+
expect(relations).toEqual({ one: { test: { DTO: TestRelation, allowFiltering: false, ...relationOpts } } })
52+
})
2953
})
3054

3155
describe('@FilterableRelation', () => {
@@ -40,6 +64,23 @@ describe('@FilterableRelation', () => {
4064
const relations = getRelations(TestDTO)
4165
expect(relations).toEqual({ one: { test: { DTO: TestRelation, ...relationOpts, allowFiltering: true } } })
4266
})
67+
68+
it('should add the deprecationReason to the options', () => {
69+
// Arrange
70+
const relationFn = () => TestRelation
71+
const relationOpts: Partial<RelationOneDecoratorOpts<TestRelation>> = {
72+
deprecationReason: 'Deprecated for no apparent reason.'
73+
}
74+
@ObjectType()
75+
@FilterableRelation('test', relationFn, relationOpts)
76+
class TestDTO {}
77+
78+
// Act
79+
const relations = getRelations(TestDTO)
80+
81+
// Assert
82+
expect(relations).toEqual({ one: { test: { DTO: TestRelation, ...relationOpts, allowFiltering: true } } })
83+
})
4384
})
4485

4586
describe('@UnPagedRelation', () => {
@@ -58,6 +99,27 @@ describe('@UnPagedRelation', () => {
5899
}
59100
})
60101
})
102+
103+
it('should add the deprecationReason to the options', () => {
104+
// Arrange
105+
const relationFn = () => TestRelation
106+
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
107+
deprecationReason: 'Deprecated for no apparent reason.'
108+
}
109+
@ObjectType()
110+
@UnPagedRelation('tests', relationFn, relationOpts)
111+
class TestDTO {}
112+
113+
// Act
114+
const relations = getRelations(TestDTO)
115+
116+
// Assert
117+
expect(relations).toEqual({
118+
many: {
119+
tests: { DTO: TestRelation, ...relationOpts, allowFiltering: false, pagingStrategy: PagingStrategies.NONE }
120+
}
121+
})
122+
})
61123
})
62124

63125
describe('@FilterableUnPagedRelation', () => {
@@ -76,6 +138,27 @@ describe('@FilterableUnPagedRelation', () => {
76138
}
77139
})
78140
})
141+
142+
it('should add the deprecationReason to the options', () => {
143+
// Arrange
144+
const relationFn = () => TestRelation
145+
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
146+
deprecationReason: 'Deprecated for no apparent reason.'
147+
}
148+
@ObjectType()
149+
@FilterableUnPagedRelation('test', relationFn, relationOpts)
150+
class TestDTO {}
151+
152+
// Act
153+
const relations = getRelations(TestDTO)
154+
155+
// Assert
156+
expect(relations).toEqual({
157+
many: {
158+
test: { DTO: TestRelation, pagingStrategy: PagingStrategies.NONE, ...relationOpts, allowFiltering: true }
159+
}
160+
})
161+
})
79162
})
80163

81164
describe('@OffsetConnection', () => {
@@ -94,6 +177,27 @@ describe('@OffsetConnection', () => {
94177
}
95178
})
96179
})
180+
181+
it('should add the deprecationReason to the options', () => {
182+
// Arrange
183+
const relationFn = () => TestRelation
184+
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
185+
deprecationReason: 'Deprecated for no apparent reason.'
186+
}
187+
@ObjectType()
188+
@OffsetConnection('test', relationFn, relationOpts)
189+
class TestDTO {}
190+
191+
// Act
192+
const relations = getRelations(TestDTO)
193+
194+
// Assert
195+
expect(relations).toEqual({
196+
many: {
197+
test: { DTO: TestRelation, ...relationOpts, allowFiltering: false, pagingStrategy: PagingStrategies.OFFSET }
198+
}
199+
})
200+
})
97201
})
98202

99203
describe('@FilterableOffsetConnection', () => {
@@ -112,6 +216,27 @@ describe('@FilterableOffsetConnection', () => {
112216
}
113217
})
114218
})
219+
220+
it('should add the deprecationReason to the options', () => {
221+
// Arrange
222+
const relationFn = () => TestRelation
223+
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
224+
deprecationReason: 'Deprecated for no apparent reason.'
225+
}
226+
@ObjectType()
227+
@FilterableOffsetConnection('test', relationFn, relationOpts)
228+
class TestDTO {}
229+
230+
// Act
231+
const relations = getRelations(TestDTO)
232+
233+
// Assert
234+
expect(relations).toEqual({
235+
many: {
236+
test: { DTO: TestRelation, ...relationOpts, pagingStrategy: PagingStrategies.OFFSET, allowFiltering: true }
237+
}
238+
})
239+
})
115240
})
116241

117242
describe('@CursorConnection', () => {
@@ -130,6 +255,27 @@ describe('@CursorConnection', () => {
130255
}
131256
})
132257
})
258+
259+
it('should add the deprecationReason to the options', () => {
260+
// Arrange
261+
const relationFn = () => TestRelation
262+
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
263+
deprecationReason: 'Deprecated for no apparent reason.'
264+
}
265+
@ObjectType()
266+
@CursorConnection('test', relationFn, relationOpts)
267+
class TestDTO {}
268+
269+
// Act
270+
const relations = getRelations(TestDTO)
271+
272+
// Assert
273+
expect(relations).toEqual({
274+
many: {
275+
test: { DTO: TestRelation, ...relationOpts, allowFiltering: false, pagingStrategy: PagingStrategies.CURSOR }
276+
}
277+
})
278+
})
133279
})
134280

135281
describe('@FilterableCursorConnection', () => {
@@ -148,6 +294,27 @@ describe('@FilterableCursorConnection', () => {
148294
}
149295
})
150296
})
297+
298+
it('should add the deprecationReason to the options', () => {
299+
// Arrange
300+
const relationFn = () => TestRelation
301+
const relationOpts: Partial<RelationManyDecoratorOpts<TestRelation>> = {
302+
deprecationReason: 'Deprecated for no apparent reason.'
303+
}
304+
@ObjectType()
305+
@FilterableCursorConnection('test', relationFn, relationOpts)
306+
class TestDTO {}
307+
308+
// Act
309+
const relations = getRelations(TestDTO)
310+
311+
// Assert
312+
expect(relations).toEqual({
313+
many: {
314+
test: { DTO: TestRelation, ...relationOpts, pagingStrategy: PagingStrategies.CURSOR, allowFiltering: true }
315+
}
316+
})
317+
})
151318
})
152319

153320
describe('getRelations', () => {

Diff for: packages/query-graphql/src/resolvers/relations/read-relations.resolver.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ const ReadOneRelationMixin =
3737
@ResolverField(
3838
baseNameLower,
3939
() => relationDTO,
40-
{ nullable: relation.nullable, complexity: relation.complexity, description: relation?.description },
40+
{
41+
nullable: relation.nullable,
42+
complexity: relation.complexity,
43+
description: relation?.description,
44+
deprecationReason: relation?.deprecationReason
45+
},
4146
commonResolverOpts,
4247
{ interceptors: [AuthorizerInterceptor(DTOClass)] }
4348
)
@@ -107,7 +112,12 @@ const ReadManyRelationMixin =
107112
@ResolverField(
108113
baseNameLower,
109114
() => CT.resolveType,
110-
{ nullable: relation.nullable, complexity: relation.complexity, description: relation?.description },
115+
{
116+
nullable: relation.nullable,
117+
complexity: relation.complexity,
118+
description: relation?.description,
119+
deprecationReason: relation?.deprecationReason
120+
},
111121
commonResolverOpts,
112122
{ interceptors: [AuthorizerInterceptor(DTOClass)] }
113123
)

Diff for: packages/query-graphql/src/resolvers/relations/relations.interface.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Complexity } from '@nestjs/graphql'
1+
import { Complexity, FieldOptions } from '@nestjs/graphql'
22
import { Class } from '@ptc-org/nestjs-query-core'
33

44
import { AuthorizerOptions } from '../../auth'
@@ -68,8 +68,6 @@ export type ResolverRelation<Relation> = {
6868
*/
6969
description?: string
7070

71-
complexity?: Complexity
72-
7371
update?: Pick<ResolverRelation<Relation>, 'description'> & ResolverRelationMethodOpts
7472
remove?: Pick<ResolverRelation<Relation>, 'description'> & ResolverRelationMethodOpts
7573
/**
@@ -82,7 +80,8 @@ export type ResolverRelation<Relation> = {
8280
} & DTONamesOpts &
8381
ResolverMethodOpts &
8482
QueryArgsTypeOpts<Relation> &
85-
Pick<ConnectionOptions, 'enableTotalCount'>
83+
Pick<ConnectionOptions, 'enableTotalCount'> &
84+
Omit<FieldOptions, 'name' | 'description' | 'middleware'>
8685

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

0 commit comments

Comments
 (0)