Skip to content

Commit 265095a

Browse files
GiamPy5Falqui, Giampaolo
andauthored
feat: Adds support for JavaScript AppSync Functions (#49)
Co-authored-by: Falqui, Giampaolo <giampaolo.falqui@aptiv.com>
1 parent 6db4b68 commit 265095a

File tree

3 files changed

+98
-13
lines changed

3 files changed

+98
-13
lines changed

examples/complete/main.tf

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ resource "aws_route53_zone" "this" {
4646
}
4747

4848
resource "aws_route53_record" "api" {
49-
zone_id = data.aws_route53_zone.this[0].zone_id
49+
zone_id = try(data.aws_route53_zone.this[0].zone_id, aws_route53_zone.this[0].zone_id)
5050
name = "api.${local.domain}"
5151
type = "CNAME"
5252
ttl = "300"
@@ -141,16 +141,26 @@ module "appsync" {
141141

142142
functions = {
143143
None = {
144-
kind = "PIPELINE"
145-
type = "Query"
146-
data_source = "None"
147-
request_mapping_template = <<EOF
148-
{
149-
"payload": $util.toJson($context.args)
150-
}
151-
EOF
144+
kind = "PIPELINE"
145+
type = "Query"
146+
data_source = "None"
147+
request_mapping_template = <<EOF
148+
{
149+
"payload": $util.toJson($context.args)
150+
}
151+
EOF
152+
152153
response_mapping_template = "$util.toJson($context.result)"
153154
}
155+
156+
Javascript = {
157+
data_source = "lambda1"
158+
max_batch_size = 10
159+
runtime = {
160+
name = "APPSYNC_JS"
161+
}
162+
code = file("src/function.js")
163+
}
154164
}
155165

156166
datasources = {
@@ -262,6 +272,19 @@ EOF
262272
"None",
263273
]
264274
}
275+
276+
"Query.user" = {
277+
kind = "PIPELINE"
278+
type = "Query"
279+
field = "user"
280+
runtime = {
281+
name = "APPSYNC_JS"
282+
}
283+
code = file("src/function.js")
284+
functions = [
285+
"Javascript",
286+
]
287+
}
265288
}
266289
}
267290

@@ -275,6 +298,13 @@ resource "random_pet" "this" {
275298

276299
resource "aws_cognito_user_pool" "this" {
277300
name = "user-pool-${random_pet.this.id}"
301+
302+
account_recovery_setting {
303+
recovery_mechanism {
304+
name = "verified_email"
305+
priority = 1
306+
}
307+
}
278308
}
279309

280310
resource "aws_cognito_user_pool_client" "this" {

examples/complete/src/function.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* These are available AWS AppSync utilities that you can use in your request and response handler.
3+
* For more information about the utilities that are currently implemented, see
4+
* https://docs.aws.amazon.com/en_us/appsync/latest/devguide/resolver-reference-overview-js.html#utility-resolvers.
5+
*/
6+
import { util } from '@aws-appsync/utils';
7+
8+
/**
9+
* This function handles an incoming request, then converts it into a request
10+
* object for the selected data source operation.
11+
* You can find more code samples here: https://github.com/aws-samples/aws-appsync-resolver-samples.
12+
* @param ctx - Contextual information for your resolver invocation.
13+
* @returns - A data source request object.
14+
*/
15+
export function request(ctx) {
16+
return {};
17+
}
18+
19+
/**
20+
* This function handles the response from the data source.
21+
* You can find more code samples here: https://github.com/aws-samples/aws-appsync-resolver-samples.
22+
* @param ctx - Contextual information for your resolver invocation.
23+
* @returns - A result that is passed to the next function, or the response handler of the pipeline resolver.
24+
*/
25+
export function response(ctx) {
26+
return {};
27+
}

main.tf

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,44 @@ resource "aws_appsync_resolver" "this" {
221221

222222
# Functions
223223
resource "aws_appsync_function" "this" {
224-
for_each = var.create_graphql_api ? var.functions : {}
224+
for_each = { for k, v in var.functions : k => v if var.create_graphql_api == true }
225225

226226
api_id = aws_appsync_graphql_api.this[0].id
227227
data_source = lookup(each.value, "data_source", null)
228228
name = each.key
229229
description = lookup(each.value, "description", null)
230-
function_version = lookup(each.value, "function_version", "2018-05-29")
230+
function_version = lookup(each.value, "function_version", try(each.value.code == null, false) ? "2018-05-29" : null)
231+
max_batch_size = lookup(each.value, "max_batch_size", null)
231232

232-
request_mapping_template = lookup(each.value, "request_mapping_template", null)
233-
response_mapping_template = lookup(each.value, "response_mapping_template", null)
233+
request_mapping_template = lookup(each.value, "request_mapping_template", try(each.value.runtime.name == "APPSYNC_JS", false) ? null : "{}")
234+
response_mapping_template = lookup(each.value, "response_mapping_template", try(each.value.runtime.name == "APPSYNC_JS", false) ? null : "{}")
235+
236+
code = try(each.value.code, null)
237+
238+
dynamic "sync_config" {
239+
for_each = try([each.value.sync_config], [])
240+
241+
content {
242+
conflict_detection = try(sync_config.value.conflict_detection, "NONE")
243+
conflict_handler = try(sync_config.value.conflict_handler, "NONE")
244+
245+
dynamic "lambda_conflict_handler_config" {
246+
for_each = try([each.value.sync_config.lambda_conflict_handler_config], [])
247+
content {
248+
lambda_conflict_handler_arn = try(lambda_conflict_handler_config.value.lambda_conflict_handler_arn, null)
249+
}
250+
}
251+
}
252+
}
253+
254+
dynamic "runtime" {
255+
for_each = try([each.value.runtime], [])
256+
257+
content {
258+
name = runtime.value.name
259+
runtime_version = try(runtime.value.runtime_version, "1.0.0")
260+
}
261+
}
234262

235263
depends_on = [aws_appsync_datasource.this]
236264
}

0 commit comments

Comments
 (0)