6

This might be a stupid question, but I really cannot find a way to do that.

So, I have DynamoDB tables and I have schema in AppSync api. In a table, for each row, there is a field which has a list as its value. So how can I append multiple items into this list without replacing the existing items? How should I write the resolver of that mutation?

Here is the screenshot of my table:

table pic

And you can see there are multiple programs in the list.

How can I just append two more programs.

Here is a new screenshot of my resolver: screenshot of resolver I want to add a existence check method in UpdateItem operation. But the current code does not work. The logic I want is that use the "contains" method to see whether the "toBeAddedProgramId" already exists. But the question is, how to extract the current program id list from User table and how to make the program id list a "list" type (since the contains method only take String set and String).

I hope this question makes sense. Thanks so much guys.

Best, Harrison

Harrison Song
  • 61
  • 1
  • 4

1 Answers1

7

To append items to a list, you should use the DynamoDB UpdateItem operation. Here is an example if you're using DynamoDB directly

In AWS AppSync, you can use the DynamoDB data source and specify the DynamoDB UpdateItem operation in your request mapping template.

Your UpdateItem request template could look like the following (modify it to serve your needs):

{
    "version" : "2017-02-28",
    "operation" : "UpdateItem",
    "key" : {
        "id" : { "S" : "${context.arguments.id}" }
    },
    "update" : {
        "expression" : "SET #progs = list_append(#progs, :vals)",
        "expressionNames": {
            "#progs" : "programs"
        },
        "expressionValues": {
            ":vals" : {
                "L": [
                    { "M" : { "id": { "S": "49f2c...." }}},
                    { "M" : { "id": { "S": "931db...." }}}
                ]
            }
        }
    }
}

We have a tutorial here that goes into more details if you are interested in learning more

sam
  • 542
  • 5
  • 11
Tinou
  • 5,908
  • 4
  • 21
  • 24
  • Thanks so much for your answer. I have gone through the tutorial. In this example, the value of :val is specified in the resolver. But in my mutation, I just pass in a list as an argument, and there always be an error said " xxx must be a list", which it already. So I am so sticker here. – Harrison Song Jun 24 '18 at 18:48
  • could you paste the evaluated request template (you can retrieve it by enabling API logs on the API settings page) and the exact error message that came back? OR if you give me a request id, an approximate timestamp and the region where your API is so I can take a look into it. – Tinou Jun 26 '18 at 04:18
  • Thanks so much for the reply Tinou. I changed some code so that I cannot reproduce the error. But the new code has an error related to similar question. Please see the new screen shot. Thanks so much! – Harrison Song Jun 27 '18 at 03:21
  • Thanks for the example. I got it to run with your snipped. But you have a little json syntax mistake in your list.To use this example everyone anyway have to edit/modify it as you wrote. – Hannes Aug 30 '19 at 06:51