1
votes

I'm trying to read an item with ID of X from DynamoDB (Using Appsync graphql) and I want it to create a default item if there is none.

This seems like it should be a normal use case. But the solutions I've tried have all been pretty bad:

  1. I tried to create a Pipeline resolver that would first get the item, then in a second function create an item if there was no item in the result from the previous function. This had with returning the read item.

  2. I tried making a PutAction with the condition that an item with this ID doesn't work. This does what I need it to, but I can't change the response from an error warning, no matter what I do to the response mapping template.

So how does one efficiently create a "read - or create if it does not exist" resolver for DynamoDb?

1

1 Answers

1
votes

It turns out that I was close to the solution. According to this documentation: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html#aws-appsync-resolver-mapping-template-reference-dynamodb-condition-handling

  1. Create a putItem resolver that conditionally checks if there is an item with the same unique identifier (in DynamoDB that's usually a primary key and a sort key combination)
  2. If the resolver determines the read object to not be different from the intended new object a warning will not be sent. So we can simply remove ALL fields from the comparison.

Example:

{
    "version" : "2017-02-28",
    "operation" : "PutItem",
    "key" : {
        "id" : { "S" : "${ctx.args.id}" }
    },
    "condition" : {
      "expression" : "attribute_not_exists(id)",
      "equalsIgnore": [ "__typename", "_version", "_lastChangedAt", "_createdAt", "name", "owner"]
   },
    "attributeValues": {
        "name": { "S" : "User Username" }
    }
}