0

For an 'id' in my DynamoDB table (e.g. e5eb02ae-04d5-4331-91e6-11efaaf12ea5), i have a column called Weather

['sun', 'rain', 'snow', etc...]

I need to update Weather when, say ['hail'] arrives. Right now, my update() below, replaces the the entire array. All i want to do is append 'hail' to the end of that list.

    const updateinfo = {
       id: "e5eb02ae-04d5-4331-91e6-11efaaf12ea5",   
       Weather: "hail"
    }
    try {
        await API.graphql(graphqlOperation (UpdateInfo, { input: updateinfo }))  //mutation
        console.log('success')
       }
       catch (err) {
         console.log(err)
       }

How do i do this, ensuring that my list is just appended to with the info, WITHOUT having to get() and pull down ['sun', 'rain', 'snow', etc...], then adding 'hail' and creating a new array? Thats then a GetInfo() and an UpdateInfo() which is double the effort/call/expense.

chai86
  • 425
  • 2
  • 14
  • 34

1 Answers1

0

I posted a response to a similar question in another question. The answer to this is to use a DynamoDB function called list_append.

list_append (operand, operand)

This function evaluates to a list with a new element added to it. You can append the new element to the start or the end of the list by reversing the order of the operands.

You can look here for a detailed response on how to do it using AWS AppSync and AWS Amplify.

https://stackoverflow.com/a/57088993/11681626

Nathan Quinn
  • 656
  • 6
  • 6
  • Hi Nathan. I'm not using "expression" or "expressionValues" as i'm not directly interacting with DynamoDB. I'm using AWS AppSync and i have a mutation called UpdateInfo that was automatically generated by AWS to use. How can list_append() be used in this case? – chai86 Jul 18 '19 at 21:42
  • 1
    Seems like you're using AWS Amplify to interact with AWS AppSync? From the looks of your code it seems like that's true. You can read about adding a custom resolver to do this using an AWS Amplify project [here](https://aws-amplify.github.io/docs/cli-toolchain/graphql?sdk=js#custom-resolvers). This is an advanced use case that the code generation tool doesn't currently support. You will need to add some files manually, but it's definitely doable. Happy to assist if you need more help. – Nathan Quinn Jul 18 '19 at 21:52
  • 1
    I updated [the post with your original question](https://stackoverflow.com/a/57088993/11681626) to contain a detailed example using AWS Amplify as well. Hope this helps! – Nathan Quinn Jul 19 '19 at 06:24