1
- import {
2
- EnvironmentVariablesService ,
3
- isRecord ,
4
- } from '@aws-lambda-powertools/commons' ;
1
+ import { EnvironmentVariablesService } from '@aws-lambda-powertools/commons' ;
5
2
import type { GenericLogger } from '@aws-lambda-powertools/commons/types' ;
6
3
import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env' ;
7
4
import type {
8
5
GraphQlRouteOptions ,
9
6
GraphQlRouterOptions ,
10
- OnMutationHandler ,
11
- OnQueryHandler ,
7
+ ResolverHandler ,
12
8
} from '../types/appsync-graphql.js' ;
13
9
import { RouteHandlerRegistry } from './RouteHandlerRegistry.js' ;
14
10
15
11
/**
16
- * Class for registering routes for the `query` and `mutation` events in AWS AppSync GraphQL APIs.
12
+ * Class for registering resolvers for GraphQL events in AWS AppSync GraphQL APIs.
17
13
*/
18
14
class Router {
19
15
/**
20
- * A map of registered routes for the `query` event , keyed by their fieldNames.
16
+ * A map of registered routes for all GraphQL events , keyed by their fieldNames.
21
17
*/
22
- protected readonly onQueryRegistry : RouteHandlerRegistry ;
23
- /**
24
- * A map of registered routes for the `mutation` event, keyed by their fieldNames.
25
- */
26
- protected readonly onMutationRegistry : RouteHandlerRegistry ;
18
+ protected readonly resolverRegistry : RouteHandlerRegistry ;
27
19
/**
28
20
* A logger instance to be used for logging debug, warning, and error messages.
29
21
*
@@ -50,133 +42,39 @@ class Router {
50
42
error : console . error ,
51
43
warn : console . warn ,
52
44
} ;
53
- this . onQueryRegistry = new RouteHandlerRegistry ( {
54
- logger : this . logger ,
55
- eventType : 'onQuery' ,
56
- } ) ;
57
- this . onMutationRegistry = new RouteHandlerRegistry ( {
45
+ this . resolverRegistry = new RouteHandlerRegistry ( {
58
46
logger : this . logger ,
59
- eventType : 'onMutation' ,
60
47
} ) ;
61
48
this . isDev = this . envService . isDevMode ( ) ;
62
49
}
63
50
64
51
/**
65
- * Register a handler function for the `query` event.
66
-
67
- * Registers a handler for a specific GraphQL Query field. The handler will be invoked when a request is made
68
- * for the specified field in the Query type.
69
- *
70
- * @example
71
- * ```ts
72
- * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
52
+ * Register a resolver function for any GraphQL event.
73
53
*
74
- * const app = new AppSyncGraphQLResolver();
54
+ * Registers a handler for a specific GraphQL field. The handler will be invoked when a request is made
55
+ * for the specified field.
75
56
*
76
- * app.onQuery('getPost', async (payload) => {
77
- * // your business logic here
78
- * return payload;
79
- * });
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
57
* @example
88
58
* ```ts
89
59
* import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
90
60
*
91
61
* const app = new AppSyncGraphQLResolver();
92
62
*
93
- * app.onQuery<{ postId: string }>('getPost', async ({ postId }) => {
94
- * // postId is now typed as string
95
- * return { id: postId };
63
+ * // Register a Query resolver
64
+ * app.resolver(async (payload) => {
65
+ * // your business logic here
66
+ * return payload;
67
+ * }, {
68
+ * fieldName: 'getPost'
96
69
* });
97
-
98
- * export const handler = async (event, context) =>
99
- * app.resolve(event, context);
100
- * ```
101
- *
102
- * As a decorator:
103
- *
104
- * @example
105
- * ```ts
106
- * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
107
- *
108
- * const app = new AppSyncGraphQLResolver();
109
- *
110
- * class Lambda {
111
- * @app .onQuery('getPost')
112
- * async handleGetPost(payload) {
113
- * // your business logic here
114
- * return payload;
115
- * }
116
- *
117
- * async handler(event, context) {
118
- * return app.resolve(event, context);
119
- * }
120
- * }
121
70
*
122
- * const lambda = new Lambda();
123
- * export const handler = lambda.handler.bind(lambda);
124
- * ```
125
- *
126
- * @param fieldName - The name of the Query field to register the handler for.
127
- * @param handler - The handler function to be called when the event is received.
128
- * @param options - Optional route options.
129
- * @param options.typeName - The name of the GraphQL type to use for the resolver (defaults to 'Query').
130
- */
131
- public onQuery < TParams extends Record < string , unknown > > (
132
- fieldName : string ,
133
- handler : OnQueryHandler < TParams > ,
134
- options ?: GraphQlRouteOptions
135
- ) : void ;
136
- public onQuery (
137
- fieldName : string ,
138
- options ?: GraphQlRouteOptions
139
- ) : MethodDecorator ;
140
- public onQuery < TParams extends Record < string , unknown > > (
141
- fieldName : string ,
142
- handler ?: OnQueryHandler < TParams > | GraphQlRouteOptions ,
143
- options ?: GraphQlRouteOptions
144
- ) : MethodDecorator | undefined {
145
- if ( handler && typeof handler === 'function' ) {
146
- this . onQueryRegistry . register ( {
147
- fieldName,
148
- handler : handler as OnQueryHandler < Record < string , unknown > > ,
149
- typeName : options ?. typeName ?? 'Query' ,
150
- } ) ;
151
- return ;
152
- }
153
-
154
- return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
155
- const routeOptions = isRecord ( handler ) ? handler : options ;
156
- this . onQueryRegistry . register ( {
157
- fieldName,
158
- handler : descriptor . value as OnQueryHandler < Record < string , unknown > > ,
159
- typeName : routeOptions ?. typeName ?? 'Query' ,
160
- } ) ;
161
- return descriptor ;
162
- } ;
163
- }
164
-
165
- /**
166
- * Register a handler function for the `mutation` event.
167
- *
168
- * Registers a handler for a specific GraphQL Mutation field. The handler will be invoked when a request is made
169
- * for the specified field in the Mutation type.
170
- *
171
- * @example
172
- * ```ts
173
- * import { AppSyncGraphQLResolver } from '@aws-lambda-powertools/event-handler/appsync-graphql';
174
- *
175
- * const app = new AppSyncGraphQLResolver();
176
- *
177
- * app.onMutation('createPost', async (payload) => {
71
+ * // Register a Mutation resolver
72
+ * app.resolver(async (payload) => {
178
73
* // your business logic here
179
74
* return payload;
75
+ * }, {
76
+ * fieldName: 'createPost',
77
+ * typeName: 'Mutation'
180
78
* });
181
79
*
182
80
* export const handler = async (event, context) =>
@@ -191,9 +89,11 @@ class Router {
191
89
*
192
90
* const app = new AppSyncGraphQLResolver();
193
91
*
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 };
92
+ * app.resolver<{ postId: string }>(async ({ postId }) => {
93
+ * // postId is now typed as string
94
+ * return { id: postId };
95
+ * }, {
96
+ * fieldName: 'getPost'
197
97
* });
198
98
*
199
99
* export const handler = async (event, context) =>
@@ -209,8 +109,8 @@ class Router {
209
109
* const app = new AppSyncGraphQLResolver();
210
110
*
211
111
* class Lambda {
212
- * @app .onMutation('createPost' )
213
- * async handleCreatePost (payload) {
112
+ * @app .resolver({ fieldName: 'getPost' } )
113
+ * async handleGetPost (payload) {
214
114
* // your business logic here
215
115
* return payload;
216
116
* }
@@ -224,41 +124,43 @@ class Router {
224
124
* export const handler = lambda.handler.bind(lambda);
225
125
* ```
226
126
*
227
- * @param fieldName - The name of the Mutation field to register the handler for.
228
127
* @param handler - The handler function to be called when the event is received.
229
- * @param options - Optional route options.
230
- * @param options.typeName - The name of the GraphQL type to use for the resolver (defaults to 'Mutation').
128
+ * @param options - Route options including the required fieldName and optional typeName.
129
+ * @param options.fieldName - The name of the field to register the handler for.
130
+ * @param options.typeName - The name of the GraphQL type to use for the resolver (defaults to 'Query').
231
131
*/
232
- public onMutation < TParams extends Record < string , unknown > > (
233
- fieldName : string ,
234
- handler : OnMutationHandler < TParams > ,
235
- options ?: GraphQlRouteOptions
132
+ public resolver < TParams extends Record < string , unknown > > (
133
+ handler : ResolverHandler < TParams > ,
134
+ options : GraphQlRouteOptions
236
135
) : void ;
237
- public onMutation (
238
- fieldName : string ,
239
- options ?: GraphQlRouteOptions
240
- ) : MethodDecorator ;
241
- public onMutation < TParams extends Record < string , unknown > > (
242
- fieldName : string ,
243
- handler ?: OnMutationHandler < TParams > | GraphQlRouteOptions ,
136
+ public resolver ( options : GraphQlRouteOptions ) : MethodDecorator ;
137
+ public resolver < TParams extends Record < string , unknown > > (
138
+ handler : ResolverHandler < TParams > | GraphQlRouteOptions ,
244
139
options ?: GraphQlRouteOptions
245
140
) : MethodDecorator | undefined {
246
- if ( handler && typeof handler === 'function' ) {
247
- this . onMutationRegistry . register ( {
248
- fieldName,
249
- handler : handler as OnMutationHandler < Record < string , unknown > > ,
250
- typeName : options ?. typeName ?? 'Mutation' ,
141
+ if ( typeof handler === 'function' ) {
142
+ const resolverOptions = options as GraphQlRouteOptions ;
143
+ const typeName = resolverOptions . typeName ?? 'Query' ;
144
+
145
+ this . resolverRegistry . register ( {
146
+ fieldName : resolverOptions . fieldName ,
147
+ handler : handler as ResolverHandler < Record < string , unknown > > ,
148
+ typeName,
251
149
} ) ;
150
+
252
151
return ;
253
152
}
254
153
154
+ const resolverOptions = handler ;
255
155
return ( _target , _propertyKey , descriptor : PropertyDescriptor ) => {
256
- const routeOptions = isRecord ( handler ) ? handler : options ;
257
- this . onMutationRegistry . register ( {
258
- fieldName,
259
- handler : descriptor . value as OnMutationHandler < Record < string , unknown > > ,
260
- typeName : routeOptions ?. typeName ?? 'Mutation' ,
156
+ const typeName = resolverOptions . typeName ?? 'Query' ;
157
+
158
+ this . resolverRegistry . register ( {
159
+ fieldName : resolverOptions . fieldName ,
160
+ handler : descriptor . value ,
161
+ typeName,
261
162
} ) ;
163
+
262
164
return descriptor ;
263
165
} ;
264
166
}
0 commit comments