简体   繁体   中英

AWS AppSync conditional update with dynamodb resolver

Trying to conditional update an item in dynamodb using Appsync dynamodb resolver. When testing conditional update through App sync queries panel, by providing incorrect username, even then the operation goes through and database is updated with the new value of price and modificationDate. Here's the code for the resolver:

{
    "version" : "2017-02-28",
    "operation" : "UpdateItem",
    "key" : {
        "id": $util.dynamodb.toDynamoDBJson($ctx.args.input.id)
    },
    "update" : {
        "expression" : "SET price = :price, modificationDate = :newDate",
        "expressionValues": {
            ":price" : { "N": $context.arguments.input.price },
            ":newDate": $util.dynamodb.toDynamoDBJson($util.time.nowISO8601())
        }
    },
    "condition" : {
        "expression"       : "username = :expectedOwner",
        "expressionValues" : {
            ":expectedOwner" : { "S" : "${context.identity.username}" }
        }
    }
}

Is there any mistake in the conditional expression?

At the top of my head your mapping template looks correct. Couple of things to check:

-> Change "id": $util.dynamodb.toDynamoDBJson($ctx.args.input.id) -> "id": $util.dynamodb.toStringJson($ctx.args.input.id)

-> Below is an example of the mapping template that works with the Conditional Check using Cognito User Pool Auth. It should be the same for Cognito Identity/AWS IAM as well.

 {
     "version" : "2017-02-28",
     "operation" : "UpdateItem",
     "key" : {
        "id": $util.dynamodb.toStringJson($ctx.args.id)
      },
      "update" : {
          "expression" : "SET title = :title, modificationDate = :newDate, content = :content",
          "expressionValues": {
             ":title" : $util.dynamodb.toStringJson($ctx.args.title),
             ":newDate": $util.dynamodb.toStringJson($util.time.nowISO8601()),
             ":content": $util.dynamodb.toStringJson($ctx.args.content)
          }
      },
      "condition" : {
         "expression"       : "#sub = :expectedOwner",
         "expressionNames"  : { "#sub": "sub" },
         "expressionValues" : {
            ":expectedOwner" : { "S" : "${context.identity.sub}" }
         }
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM