Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appsync response mapping template json key name change

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.

like image 493
Toni Kuutti Avatar asked Jul 28 '18 20:07

Toni Kuutti


People also ask

How do I respond to GraphQL requests in AppSync?

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.

How to create nested JSON Schema in AWS AppSync?

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.

What are the different types of resolvers in AppSync?

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.

How to handle nested JSON data in DynamoDB using AWS AppSync?

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.


Video Answer


1 Answers

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

like image 182
Shankar Raju Avatar answered Nov 15 '22 07:11

Shankar Raju