@@ -78,6 +78,23 @@ class Router {
78
78
* return payload;
79
79
* });
80
80
81
+ * export const handler = async (event, context) =>
82
+ * app.resolve(event, context);
83
+ * ```
84
+ *
85
+ * You can also specify the type of the arguments using a generic type parameter:
86
+ *
87
+ * @example
88
+ * ```ts
89
+ * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
90
+ *
91
+ * const app = new AppSyncGraphQLResolver();
92
+ *
93
+ * app.onQuery<{ postId: string }>('getPost', async ({ postId }) => {
94
+ * // postId is now typed as string
95
+ * return { id: postId };
96
+ * });
97
+
81
98
* export const handler = async (event, context) =>
82
99
* app.resolve(event, context);
83
100
* ```
@@ -111,24 +128,24 @@ class Router {
111
128
* @param options - Optional route options.
112
129
* @param options.typeName - The name of the GraphQL type to use for the resolver (defaults to 'Query').
113
130
*/
114
- public onQuery (
131
+ public onQuery < TParams extends Record < string , unknown > > (
115
132
fieldName : string ,
116
- handler : OnQueryHandler ,
133
+ handler : OnQueryHandler < TParams > ,
117
134
options ?: GraphQlRouteOptions
118
135
) : void ;
119
136
public onQuery (
120
137
fieldName : string ,
121
138
options ?: GraphQlRouteOptions
122
139
) : MethodDecorator ;
123
- public onQuery (
140
+ public onQuery < TParams extends Record < string , unknown > > (
124
141
fieldName : string ,
125
- handler ?: OnQueryHandler | GraphQlRouteOptions ,
142
+ handler ?: OnQueryHandler < TParams > | GraphQlRouteOptions ,
126
143
options ?: GraphQlRouteOptions
127
144
) : MethodDecorator | undefined {
128
145
if ( handler && typeof handler === 'function' ) {
129
146
this . onQueryRegistry . register ( {
130
147
fieldName,
131
- handler,
148
+ handler : handler as OnQueryHandler < Record < string , unknown > > ,
132
149
typeName : options ?. typeName ?? 'Query' ,
133
150
} ) ;
134
151
return ;
@@ -138,7 +155,7 @@ class Router {
138
155
const routeOptions = isRecord ( handler ) ? handler : options ;
139
156
this . onQueryRegistry . register ( {
140
157
fieldName,
141
- handler : descriptor . value ,
158
+ handler : descriptor . value as OnQueryHandler < Record < string , unknown > > ,
142
159
typeName : routeOptions ?. typeName ?? 'Query' ,
143
160
} ) ;
144
161
return descriptor ;
@@ -166,6 +183,23 @@ class Router {
166
183
* app.resolve(event, context);
167
184
* ```
168
185
*
186
+ * You can also specify the type of the arguments using a generic type parameter:
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
191
+ *
192
+ * const app = new AppSyncGraphQLResolver();
193
+ *
194
+ * app.onMutation<{ title: string; content: string }>('createPost', async ({ title, content }) => {
195
+ * // title and content are now typed as string
196
+ * return { id: '123', title, content };
197
+ * });
198
+ *
199
+ * export const handler = async (event, context) =>
200
+ * app.resolve(event, context);
201
+ * ```
202
+ *
169
203
* As a decorator:
170
204
*
171
205
* @example
@@ -195,24 +229,24 @@ class Router {
195
229
* @param options - Optional route options.
196
230
* @param options.typeName - The name of the GraphQL type to use for the resolver (defaults to 'Mutation').
197
231
*/
198
- public onMutation (
232
+ public onMutation < TParams extends Record < string , unknown > > (
199
233
fieldName : string ,
200
- handler : OnMutationHandler ,
234
+ handler : OnMutationHandler < TParams > ,
201
235
options ?: GraphQlRouteOptions
202
236
) : void ;
203
237
public onMutation (
204
238
fieldName : string ,
205
239
options ?: GraphQlRouteOptions
206
240
) : MethodDecorator ;
207
- public onMutation (
241
+ public onMutation < TParams extends Record < string , unknown > > (
208
242
fieldName : string ,
209
- handler ?: OnMutationHandler | GraphQlRouteOptions ,
243
+ handler ?: OnMutationHandler < TParams > | GraphQlRouteOptions ,
210
244
options ?: GraphQlRouteOptions
211
245
) : MethodDecorator | undefined {
212
246
if ( handler && typeof handler === 'function' ) {
213
247
this . onMutationRegistry . register ( {
214
248
fieldName,
215
- handler,
249
+ handler : handler as OnMutationHandler < Record < string , unknown > > ,
216
250
typeName : options ?. typeName ?? 'Mutation' ,
217
251
} ) ;
218
252
return ;
@@ -222,7 +256,7 @@ class Router {
222
256
const routeOptions = isRecord ( handler ) ? handler : options ;
223
257
this . onMutationRegistry . register ( {
224
258
fieldName,
225
- handler : descriptor . value ,
259
+ handler : descriptor . value as OnMutationHandler < Record < string , unknown > > ,
226
260
typeName : routeOptions ?. typeName ?? 'Mutation' ,
227
261
} ) ;
228
262
return descriptor ;
0 commit comments