Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS amplify graphql appsync - how to replace array instead of append?

I am using AWS amplify with graphql and appsync. I have a field called myArray that is an array of strings. I would like to replace the value of myArray with a new array.

When I try to do this, graphql adds my input to the existing array, instead of replacing the array with my input.

How can I replace my array with a new array, instead of appending to the existing array. Here is my code and the result:

// My query:

    updateFrame(input: {id: "94cf1635-a336-4619-99eb-53c081f57bea", myArray: ["new value"]}) {
      id
      testOrder
    }
  }```
  

// Mutated data
```{
  "data": {
    "updateFrame": {
      "id": "94cf1635-a336-4619-99eb-53c081f57bea",
      "myArray": [
        "oldValue",
        "oldValue",
        "new value"
      ]
    }
  }```[
}
like image 366
BitFunny Avatar asked Apr 18 '26 14:04

BitFunny


1 Answers

I've run into this as well. Note, my confidence that this is a correct answer is not 100%:

  • If you have DataStore enabled, the backend's logic for conflict resolution may be attempting to merge the data, rather than replace it.
  • By including a value for "_version", data will be overwritten as you wish.
  • You have to make sure you are using the current/latest value for "_version". If you are lower or higher, it will either fail or save/return the original value.
  • IMO, the documentation about mixing DataStore with GraphQL is somewhat opaque in parts. So I have a nagging worry that doing this (mixing DataStore with GraphQL queries) is unsupported. I.e., that just because it works it isn't always going to work.
like image 124
Alex Avatar answered Apr 20 '26 09:04

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!