How I can update boolean field in DynamoDb with Mapping template? - amazon-web-services

I have next code for update item in DB with mapping template:
$!{expSet.put("available", ":available")}
$!{expValues.put(":available", { "BOOL": $ctx.args.available })}
when I send available = false - it's ok, but if available = true I get error
"Unable to parse the JSON document: 'Unexpected character (':' (code
58)): was expecting double-quote to start field name
schema in GraphQl
type Item {
....
available: Boolean!
....
}
What I do wrong ?

Your UpdateItem request mapping template should look something like:
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : { "S" : "${context.arguments.id}" }
},
"update" : {
"expression" : "SET #available = :available",
"expressionNames": {
"#available" : "available"
},
"expressionValues": {
":available" : { "BOOL": ${context.arguments.available} }
}
}
}

Related

Trying to remove item from a list in AWS AppSync in VTL

I am trying to make a custom resolver in VTL that will remove a certain string from a dynamodb list. I was trying to use $util.list.copyAndRemoveAll to remove the friend from the list. But I am not quite sure how to use it and the documentation isn't very clear to me and I don't even know if I am using the function correctly within VTL. https://docs.aws.amazon.com/appsync/latest/devguide/list-helpers-in-util-list.html
Thank you in advance.
GraphQL mutation
type Mutation {
removeFromIncomingFriendList(pk: String!, sk: String!, friend: String!): String!
}
#user_incoming_friend_requests is a list of usernames of users that have sent friend requests.
{
"version" : "2018-05-29",
"operation" : "UpdateItem",
"key" : {
"pk" : { "S" : "USER#USERNAME" },
"sk" : { "S" : "METADATA#USERNAME" },
},
"update": {
"expression" : "SET #user_incoming_friend_requests = :newList ADD version :plusOne",
"expressionNames": {
"#user_incoming_friend_requests" : "user_incoming_friend_requests",
},
"expressionValues" : {
":newList": $util.list.copyAndRemoveAll("#user_incoming_friend_requests", [${context.arguments.friend}])
":plusOne" : {"N":1}
}
}
}

how to update an attribute in a nested array object DynamoDb AWS

I want to update the choices attribute directly through AWS API Gateway.
{
"id" : "1",
"general" : {
"questions : [
"choices" : ["1","2","3"]
]
}
}
Here is my resolver mapping template
#set($inputRoot = $input.path('$'))
{
"TableName" : "models",
"Key" : {
"accountId" : {
"S": "$inputRoot.accountId"
},
"category" : {
"S" : "model"
}
},
"UpdateExpression" : "SET general.questions = :questions",
"ExpressionAttributeValues" : {
":questions" : {
"L" : [
#foreach($elem in $inputRoot.questions)
{
"M" : {
"choices" : {
"L" : [
#foreach($elem1 in $elem.choices)
{"S" : "$elem1"}
#if(foreach.hasNext),#end
#end
]
}
}
}
#if($foreach.hasNext),#end
#end
]
}
}
}
But I am getting Internal server error 500 on execution.
Gateway response body: {"message": "Internal server error"}
Does DynamoDb support updating this expression or is there any error in mapping template ? If so what should be the mapping template for the object I am trying to update.

AppSync to DynamoDB update query mapping error

I have the following DynamoDB mapping template, to update an existing DynamoDB item:
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id": $util.dynamodb.toDynamoDBJson($ctx.args.application.id),
"tenant": $util.dynamodb.toDynamoDBJson($ctx.identity.claims['http://domain/tenant'])
},
"update" : {
"expression" : "SET #sourceUrl = :sourceUrl, #sourceCredential = :sourceCredential, #instanceSize = :instanceSize, #users = :users",
"expressionNames" : {
"#sourceUrl" : "sourceUrl",
"#sourceCredential" : "sourceCredential",
"#instanceSize" : "instanceSize",
"#users" : "users"
},
"expressionValues" : {
":sourceUrl" : $util.dynamodb.toDynamoDbJson($ctx.args.application.sourceUrl),
":sourceCredential" : $util.dynamodb.toDynamoDbJson($ctx.args.application.sourceCredential),
":instanceSize" : $util.dynamodb.toDynamoDbJson($ctx.args.application.instanceSize),
":users" : $util.dynamodb.toDynamoDbJson($ctx.args.application.users)
}
},
"condition" : {
"expression" : "attribute_exists(#id) AND attribute_exists(#tenant)",
"expressionNames" : {
"#id" : "id",
"#tenant" : "tenant"
}
}
}
But I'm getting the following error:
message: "Unable to parse the JSON document: 'Unrecognized token '$util': was expecting ('true', 'false' or 'null')↵ at [Source: (String)"{↵ "version" : "2017-02-28",↵ "operation" : "UpdateItem",↵ "key" : {↵ "id": {"S":"abc-123"},↵ "tenant": {"S":"test"}↵ },↵ "update" : {↵ "expression" : "SET #sourceUrl = :sourceUrl, #sourceCredential = :sourceCredential, #instanceSize = :instanceSize, #users = :users",↵ "expressionNames" : {↵ "#sourceUrl" : "sourceUrl",↵ "#sourceCredential" : "sourceCredential",↵ "#instanceSize" : "instanceSize",↵ "#users" : "users"↵ }"[truncated 400 chars]; line: 17, column: 29]'"
I've tried removing parts, and it seems to be related to the expressionValues, but I can't see anything wrong with the syntax.
Seems like you misspelled the toDynamoDBJson method
Replace
$util.dynamodb.toDynamoDbJson($ctx.args.application.sourceUrl)
with
$util.dynamodb.toDynamoDBJson($ctx.args.application.sourceUrl)
Note the uppercase B in toDynamoDBJson.

update dynamodb list item

This seems like it should be really simple but I haven't found any examples or documentation. I've got a dynamodb table that looks like this:
record 1: {name, email, items[{product}, {item2}, {item3]}
record 2: (name, email, items[{product}, {item2}, {item3]}
I need to be able to update items elements, i.e., update item1 object in record 1. I can do this with the following code by hardcoding the list array element, but I can't figure out how to pass the item number into the update expression :
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id" : { "S" : "${context.arguments.input.id}" }
},
"update" : {
"expression" : "SET #items[0].#product= :productVal",
"expressionNames" : {
"#product": "product",
},
"expressionValues" : {
":productVal": { "S" : "${context.arguments.input.product}" },
}
}
Have you tried something like:
"update" : {
"expression" : "SET #items[:idx].#product= :productVal",
"expressionNames" : {
"#product": "product",
},
"expressionValues" : {
":productVal": { "S" : "${context.arguments.input.product}" },
":idx": { "N" : 0 }
}
}

Appsync dynamodb request mapping - default variables

I'm looking a way for setting default variables for appsync dynamodb resolvers, if one of variables from query is not provided.
Let's say we have simple db request mapping
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
## If your table's hash key is not named 'id', update it here. **
"id" : { "S" : "$utils.autoId()" }
},
"attributeValues" : {
## Add an item for each field you would like to store to Amazon DynamoDB. **
"key" : { "S" : "${context.arguments.id}" },
"name" : { "S" : "${context.arguments.name}" }
}
}
I found a way to do this using conditional statements
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
## If your table's hash key is not named 'id', update it here. **
"id" : { "S" : "$utils.autoId()" }
},
"attributeValues" : {
## Add an item for each field you would like to store to Amazon DynamoDB. **
"key" : { "S" : "${context.arguments.id}" },
#if( ($context.arguments.name))
"name" : { "S" : "${context.arguments.name}" }
#else
"name" : { "S" : "default" }
#end
}
}
Above way we can also exclude one of attributes if it is not provided
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
## If your table's hash key is not named 'id', update it here. **
"id" : { "S" : "$utils.autoId()" }
},
"attributeValues" : {
## Add an item for each field you would like to store to Amazon DynamoDB. **
"key" : { "S" : "${context.arguments.id}" },
#if( ($context.arguments.name))
"name" : { "S" : "${context.arguments.name}" }
#end
}
}
If $context.arguments.name is not provided then name attribute isn't inserted into dynamodb.
But is there any smarter way to do above?
If you don't want to specify keys explicitly, you can do something like:
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
## If your table's hash key is not named 'id', update it here. **
"id" : { "S" : "$utils.autoId()" }
},
#set( $expValues = {} )
#foreach( $entry in $context.arguments.entrySet() )
$!{expValues.put("${entry.key}", { "S" : "${entry.value}" })}
#end
#if( !${expValues.isEmpty()} )
"attributeValues" : $utils.toJson($expValues)
#end
}