@@ -106,7 +106,7 @@ class Router {
106106 * const app = new AppSyncGraphQLResolver();
107107 *
108108 * class Lambda {
109- * @app .resolver({ fieldName: 'getPost' })
109+ * @app .resolver({ fieldName: 'getPost' })
110110 * async handleGetPost(payload) {
111111 * // your business logic here
112112 * return payload;
@@ -126,38 +126,193 @@ class Router {
126126 * @param handler - The handler function to be called when the event is received.
127127 * @param options - Route options including the required fieldName and optional typeName.
128128 * @param options.fieldName - The name of the field to register the handler for.
129- * @param options.typeName - The name of the GraphQL type to use for the resolver ( defaults to ' Query') .
129+ * @param options.typeName - The name of the GraphQL type to use for the resolver, defaults to ` Query` .
130130 */
131131 public resolver < TParams extends Record < string , unknown > > (
132132 handler : ResolverHandler < TParams > ,
133133 options : GraphQlRouteOptions
134134 ) : void ;
135135 public resolver ( options : GraphQlRouteOptions ) : MethodDecorator ;
136136 public resolver < TParams extends Record < string , unknown > > (
137- handler : ResolverHandler < TParams > | GraphQlRouteOptions ,
137+ handlerOrOptions : ResolverHandler < TParams > | GraphQlRouteOptions ,
138138 options ?: GraphQlRouteOptions
139139 ) : MethodDecorator | undefined {
140- if ( typeof handler === 'function' ) {
140+ if ( typeof handlerOrOptions === 'function' ) {
141141 const resolverOptions = options as GraphQlRouteOptions ;
142142 const { typeName = 'Query' , fieldName } = resolverOptions ;
143143
144144 this . resolverRegistry . register ( {
145145 fieldName,
146- handler : handler as ResolverHandler ,
146+ handler : handlerOrOptions as ResolverHandler ,
147147 typeName,
148148 } ) ;
149149
150150 return ;
151151 }
152152
153- const resolverOptions = handler ;
154- return ( target , _propertyKey , descriptor : PropertyDescriptor ) => {
155- const { typeName = 'Query' , fieldName } = resolverOptions ;
153+ return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
154+ this . resolverRegistry . register ( {
155+ fieldName : handlerOrOptions . fieldName ,
156+ handler : descriptor ?. value ,
157+ typeName : handlerOrOptions . typeName ?? 'Query' ,
158+ } ) ;
159+
160+ return descriptor ;
161+ } ;
162+ }
156163
164+ /**
165+ * Register a handler function for the `query` event.
166+
167+ * Registers a handler for a specific GraphQL Query field. The handler will be invoked when a request is made
168+ * for the specified field in the Query type.
169+ *
170+ * @example
171+ * ```ts
172+ * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
173+ *
174+ * const app = new AppSyncGraphQLResolver();
175+ *
176+ * app.onQuery('getPost', async (payload) => {
177+ * // your business logic here
178+ * return payload;
179+ * });
180+
181+ * export const handler = async (event, context) =>
182+ * app.resolve(event, context);
183+ * ```
184+ *
185+ * As a decorator:
186+ *
187+ * @example
188+ * ```ts
189+ * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
190+ *
191+ * const app = new AppSyncGraphQLResolver();
192+ *
193+ * class Lambda {
194+ * @app .onQuery('getPost')
195+ * async handleGetPost(payload) {
196+ * // your business logic here
197+ * return payload;
198+ * }
199+ *
200+ * async handler(event, context) {
201+ * return app.resolve(event, context);
202+ * }
203+ * }
204+ *
205+ * const lambda = new Lambda();
206+ * export const handler = lambda.handler.bind(lambda);
207+ * ```
208+ *
209+ * @param fieldName - The name of the Query field to register the handler for.
210+ * @param handler - The handler function to be called when the event is received.
211+ */
212+ public onQuery < TParams extends Record < string , unknown > > (
213+ fieldName : string ,
214+ handler : ResolverHandler < TParams >
215+ ) : void ;
216+ public onQuery ( fieldName : string ) : MethodDecorator ;
217+ public onQuery < TParams extends Record < string , unknown > > (
218+ fieldName : string ,
219+ handlerOrFieldName ?:
220+ | ResolverHandler < TParams >
221+ | Pick < GraphQlRouteOptions , 'fieldName' >
222+ ) : MethodDecorator | undefined {
223+ if ( typeof handlerOrFieldName === 'function' ) {
224+ this . resolverRegistry . register ( {
225+ fieldName : fieldName ,
226+ handler : handlerOrFieldName as ResolverHandler ,
227+ typeName : 'Query' ,
228+ } ) ;
229+
230+ return ;
231+ }
232+
233+ return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
234+ this . resolverRegistry . register ( {
235+ fieldName : fieldName ,
236+ handler : descriptor ?. value ,
237+ typeName : 'Query' ,
238+ } ) ;
239+
240+ return descriptor ;
241+ } ;
242+ }
243+
244+ /**
245+ * Register a handler function for the `mutation` event.
246+ *
247+ * Registers a handler for a specific GraphQL Mutation field. The handler will be invoked when a request is made
248+ * for the specified field in the Mutation type.
249+ *
250+ * @example
251+ * ```ts
252+ * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
253+ *
254+ * const app = new AppSyncGraphQLResolver();
255+ *
256+ * app.onMutation('createPost', async (payload) => {
257+ * // your business logic here
258+ * return payload;
259+ * });
260+ *
261+ * export const handler = async (event, context) =>
262+ * app.resolve(event, context);
263+ * ```
264+ *
265+ * As a decorator:
266+ *
267+ * @example
268+ * ```ts
269+ * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
270+ *
271+ * const app = new AppSyncGraphQLResolver();
272+ *
273+ * class Lambda {
274+ * @app .onMutation('createPost')
275+ * async handleCreatePost(payload) {
276+ * // your business logic here
277+ * return payload;
278+ * }
279+ *
280+ * async handler(event, context) {
281+ * return app.resolve(event, context);
282+ * }
283+ * }
284+ *
285+ * const lambda = new Lambda();
286+ * export const handler = lambda.handler.bind(lambda);
287+ * ```
288+ *
289+ * @param fieldName - The name of the Mutation field to register the handler for.
290+ * @param handler - The handler function to be called when the event is received.
291+ */
292+ public onMutation < TParams extends Record < string , unknown > > (
293+ fieldName : string ,
294+ handler : ResolverHandler < TParams >
295+ ) : void ;
296+ public onMutation ( fieldName : string ) : MethodDecorator ;
297+ public onMutation < TParams extends Record < string , unknown > > (
298+ fieldName : string ,
299+ handlerOrFieldName ?: ResolverHandler < TParams > | string
300+ ) : MethodDecorator | undefined {
301+ if ( typeof handlerOrFieldName === 'function' ) {
302+ this . resolverRegistry . register ( {
303+ fieldName,
304+ handler : handlerOrFieldName as ResolverHandler ,
305+ typeName : 'Mutation' ,
306+ } ) ;
307+
308+ return ;
309+ }
310+
311+ return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
157312 this . resolverRegistry . register ( {
158313 fieldName,
159314 handler : descriptor ?. value ,
160- typeName,
315+ typeName : 'Mutation' ,
161316 } ) ;
162317
163318 return descriptor ;
0 commit comments