What would be the right way to change json response key value in aws appsync response mapping template?
JSON that I get looks like this:
{
"tenant_id": 1,
"id": "bd8ce6a8-8532-47ec-8b7f-dcd1f1603320",
"header": "Header name",
"visible": true
}
and what I would like to pass forward is
{
"tenantId": 1,
"id": "bd8ce6a8-8532-47ec-8b7f-dcd1f1603320",
"header": "Header name",
"visible": true
}
Schema wants tenant id in form of tenantID and lambda returns it in form of tenant_id. I could change it in lambda but I would like to know how to do it in response mapping template.
AWS AppSync lets you respond to GraphQL requests by performing operations on your resources. For each GraphQL field you wish to run a query or mutation on, a resolver must be attached in order to communicate with a data source. The communication is typically through parameters or operations that are unique to the data source.
In the left navigation pane of the AWS AppSync console, choose Schema. 2. Copy and paste the following nested JSON schema into the text box and choose Save Schema: Important: Make sure to overwrite the prefilled content in the text box with the nested JSON schema.
There are two types of resolvers in AppSync which leverage mapping templates in slightly different ways: unit resolvers and pipeline resolvers. Unit Resolvers are self contained entities which include a request and response template only. Use these for simple, single operations such as listing items from a single data source.
Do the following to get an AWS AppSync schema to handle nested JSON data in DynamoDB: Add a nested JSON data item to the DynamoDB table. Create an AppSync API and attach the data source.
You could do this via the response mapping template for the field you are resolving to in the following manner:
Consider the JSON response from your lambda to be stored in the response
variable, then you can return something like this.
$#set($result = {
"tenantId": ${response.tenant_id},
"id": "${response.id}",
"header": "${response.header}",
"visible": $response.visible
})
$util.toJson($result)
Alternatively, you could also mutate your response from the lambda by setting a tenantId
field, something like #set( $response.tenantId = $response.tenant_id )
. Let me know if you still face an issue.
Thanks, Shankar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With