@@ -9,15 +9,19 @@ import { Type } from '@nestjs/common';
9
9
import { isFunction } from '@nestjs/common/utils/shared.utils' ;
10
10
import { Complexity , FieldMiddleware } from '../interfaces' ;
11
11
import { BaseTypeOptions } from '../interfaces/base-type-options.interface' ;
12
- import { ReturnTypeFunc } from '../interfaces/return-type-func.interface' ;
12
+ import {
13
+ GqlTypeReference ,
14
+ ReturnTypeFunc ,
15
+ ReturnTypeFuncValue ,
16
+ } from '../interfaces/return-type-func.interface' ;
13
17
import { LazyMetadataStorage } from '../schema-builder/storages/lazy-metadata.storage' ;
14
18
import { TypeMetadataStorage } from '../schema-builder/storages/type-metadata.storage' ;
15
19
import { reflectTypeFromMetadata } from '../utils/reflection.utilts' ;
16
20
17
21
/**
18
22
* Interface defining options that can be passed to `@Field()` decorator.
19
23
*/
20
- export interface FieldOptions extends BaseTypeOptions {
24
+ export interface FieldOptions < T = any > extends BaseTypeOptions < T > {
21
25
/**
22
26
* Name of the field.
23
27
*/
@@ -40,6 +44,12 @@ export interface FieldOptions extends BaseTypeOptions {
40
44
middleware ?: FieldMiddleware [ ] ;
41
45
}
42
46
47
+ type FieldOptionsExtractor < T > = T extends [ GqlTypeReference < infer P > ]
48
+ ? FieldOptions < P [ ] >
49
+ : T extends GqlTypeReference < infer P >
50
+ ? FieldOptions < P >
51
+ : never ;
52
+
43
53
/**
44
54
* @Field () decorator is used to mark a specific class property as a GraphQL field.
45
55
* Only properties decorated with this decorator will be defined in the schema.
@@ -49,24 +59,25 @@ export function Field(): PropertyDecorator & MethodDecorator;
49
59
* @Field () decorator is used to mark a specific class property as a GraphQL field.
50
60
* Only properties decorated with this decorator will be defined in the schema.
51
61
*/
52
- export function Field (
53
- options : FieldOptions ,
62
+ export function Field < T extends ReturnTypeFuncValue > (
63
+ options : FieldOptionsExtractor < T > ,
54
64
) : PropertyDecorator & MethodDecorator ;
55
65
/**
56
66
* @Field () decorator is used to mark a specific class property as a GraphQL field.
57
67
* Only properties decorated with this decorator will be defined in the schema.
58
68
*/
59
- export function Field (
60
- returnTypeFunction ?: ReturnTypeFunc ,
61
- options ?: FieldOptions ,
69
+ export function Field < T extends ReturnTypeFuncValue > (
70
+ returnTypeFunction ?: ReturnTypeFunc < T > ,
71
+ options ?: FieldOptionsExtractor < T > ,
62
72
) : PropertyDecorator & MethodDecorator ;
73
+
63
74
/**
64
75
* @Field () decorator is used to mark a specific class property as a GraphQL field.
65
76
* Only properties decorated with this decorator will be defined in the schema.
66
77
*/
67
- export function Field (
68
- typeOrOptions ?: ReturnTypeFunc | FieldOptions ,
69
- fieldOptions ?: FieldOptions ,
78
+ export function Field < T extends ReturnTypeFuncValue > (
79
+ typeOrOptions ?: ReturnTypeFunc < T > | FieldOptionsExtractor < T > ,
80
+ fieldOptions ?: FieldOptionsExtractor < T > ,
70
81
) : PropertyDecorator & MethodDecorator {
71
82
return (
72
83
prototype : Object ,
@@ -83,9 +94,9 @@ export function Field(
83
94
} ;
84
95
}
85
96
86
- export function addFieldMetadata (
87
- typeOrOptions : ReturnTypeFunc | FieldOptions ,
88
- fieldOptions : FieldOptions ,
97
+ export function addFieldMetadata < T extends ReturnTypeFuncValue > (
98
+ typeOrOptions : ReturnTypeFunc < T > | FieldOptionsExtractor < T > ,
99
+ fieldOptions : FieldOptionsExtractor < T > ,
89
100
prototype : Object ,
90
101
propertyKey ?: string ,
91
102
descriptor ?: TypedPropertyDescriptor < any > ,
@@ -103,7 +114,7 @@ export function addFieldMetadata(
103
114
metadataKey : isResolverMethod ? 'design:returntype' : 'design:type' ,
104
115
prototype,
105
116
propertyKey,
106
- explicitTypeFn : typeFunc as ReturnTypeFunc ,
117
+ explicitTypeFn : typeFunc as ReturnTypeFunc < T > ,
107
118
typeOptions : options ,
108
119
} ) ;
109
120
0 commit comments