1
- import { Template } from 'aws-cdk-lib/assertions' ;
1
+ import { Match , Template } from 'aws-cdk-lib/assertions' ;
2
2
import assert from 'node:assert' ;
3
3
import { beforeEach , describe , it } from 'node:test' ;
4
4
import { App , Duration , Stack } from 'aws-cdk-lib' ;
5
5
import {
6
6
AmplifyData ,
7
7
AmplifyDataDefinition ,
8
8
} from '@aws-amplify/data-construct' ;
9
- import { resolve } from 'path' ;
10
- import { fileURLToPath } from 'url' ;
11
- import { convertJsResolverDefinition } from './convert_js_resolvers.js' ;
9
+ import { join , resolve } from 'path' ;
10
+ import { tmpdir } from 'os' ;
11
+ import { fileURLToPath , pathToFileURL } from 'url' ;
12
+ import {
13
+ convertJsResolverDefinition ,
14
+ defaultJsResolverCode ,
15
+ } from './convert_js_resolvers.js' ;
12
16
import { a } from '@aws-amplify/data-schema' ;
17
+ import { writeFileSync } from 'node:fs' ;
13
18
14
19
// stub schema for the AmplifyApi construct
15
20
// not relevant to this test suite
@@ -28,6 +33,33 @@ const createStackAndSetContext = (): Stack => {
28
33
return stack ;
29
34
} ;
30
35
36
+ void describe ( 'defaultJsResolverCode' , ( ) => {
37
+ void it ( 'returns the default JS resolver code with api id and env name in valid JS' , async ( ) => {
38
+ const code = defaultJsResolverCode ( 'testApiId' , 'testEnvName' ) ;
39
+ assert ( code . includes ( "ctx.stash.awsAppsyncApiId = 'testApiId';" ) ) ;
40
+ assert (
41
+ code . includes ( "ctx.stash.amplifyApiEnvironmentName = 'testEnvName';" )
42
+ ) ;
43
+
44
+ const tempDir = tmpdir ( ) ;
45
+ const filename = join ( tempDir , 'js_resolver_handler.js' ) ;
46
+ writeFileSync ( filename , code ) ;
47
+
48
+ // windows requires dynamic imports to use file urls
49
+ const fileUrl = pathToFileURL ( filename ) . href ;
50
+ const resolver = await import ( fileUrl ) ;
51
+ const context = { stash : { } , prev : { result : 'result' } } ;
52
+ assert . deepEqual ( resolver . request ( context ) , { } ) ;
53
+
54
+ // assert api id and env name are added to the context stash
55
+ assert . deepEqual ( context . stash , {
56
+ awsAppsyncApiId : 'testApiId' ,
57
+ amplifyApiEnvironmentName : 'testEnvName' ,
58
+ } ) ;
59
+ assert . equal ( resolver . response ( context ) , 'result' ) ;
60
+ } ) ;
61
+ } ) ;
62
+
31
63
void describe ( 'convertJsResolverDefinition' , ( ) => {
32
64
let stack : Stack ;
33
65
let amplifyApi : AmplifyData ;
@@ -158,4 +190,52 @@ void describe('convertJsResolverDefinition', () => {
158
190
159
191
template . resourceCountIs ( 'AWS::AppSync::Resolver' , 1 ) ;
160
192
} ) ;
193
+
194
+ void it ( 'adds api id and environment name to stash' , ( ) => {
195
+ const absolutePath = resolve (
196
+ fileURLToPath ( import . meta. url ) ,
197
+ '../../lib/assets' ,
198
+ 'js_resolver_handler.js'
199
+ ) ;
200
+
201
+ const schema = a . schema ( {
202
+ customQuery : a
203
+ . query ( )
204
+ . authorization ( ( allow ) => allow . publicApiKey ( ) )
205
+ . returns ( a . string ( ) )
206
+ . handler (
207
+ a . handler . custom ( {
208
+ entry : absolutePath ,
209
+ } )
210
+ ) ,
211
+ } ) ;
212
+ const { jsFunctions } = schema . transform ( ) ;
213
+ convertJsResolverDefinition ( stack , amplifyApi , jsFunctions ) ;
214
+
215
+ const template = Template . fromStack ( stack ) ;
216
+ template . hasResourceProperties ( 'AWS::AppSync::Resolver' , {
217
+ Runtime : {
218
+ Name : 'APPSYNC_JS' ,
219
+ RuntimeVersion : '1.0.0' ,
220
+ } ,
221
+ Kind : 'PIPELINE' ,
222
+ TypeName : 'Query' ,
223
+ FieldName : 'customQuery' ,
224
+ Code : {
225
+ 'Fn::Join' : [
226
+ '' ,
227
+ [
228
+ "/**\n * Pipeline resolver request handler\n */\nexport const request = (ctx) => {\n ctx.stash.awsAppsyncApiId = '" ,
229
+ {
230
+ 'Fn::GetAtt' : [
231
+ Match . stringLikeRegexp ( 'amplifyDataGraphQLAPI.*' ) ,
232
+ 'ApiId' ,
233
+ ] ,
234
+ } ,
235
+ "';\n ctx.stash.amplifyApiEnvironmentName = 'NONE';\n return {};\n};\n/**\n * Pipeline resolver response handler\n */\nexport const response = (ctx) => {\n return ctx.prev.result;\n};\n" ,
236
+ ] ,
237
+ ] ,
238
+ } ,
239
+ } ) ;
240
+ } ) ;
161
241
} ) ;
0 commit comments