DataStore save function not updating array property correctly


Hi, I'm encountering an issue with the AWS Amplify DataStore save function when updating an object that includes an array property. When I use the DataStore save function to update an object with an array property, and the update input includes a new array value, the array property is not updated correctly and the new value is merged with any existing values.

Example: Suppose I have an object with an array property called "timeToArrive" that has the value [2, 4]. I use the DataStore save function to update the object, with an update input that includes the new value [1, 1] for the "timeToArrive" property:

const original = await DataStore.query(MyModel, id); await, updated => { updated.timeToArrive = [1, 1]; }));

Instead of setting the "timeToArrive" property to [1, 1], the DataStore save function merges the new value with the existing values, resulting in the value [2, 4, 1, 1] for the "timeToArrive" property.

Attempts to fix: I've tried using different approaches to update the array property, including copying the new values using a for loop or the spread operator, and using a custom resolver to update the object. However, none of these approaches have resolved the issue.

Expected behavior: I expect the DataStore save function to update the array property correctly, by setting the property to the new value without merging it with any existing values.

Current setup:

AWS Amplify version: 5.0.18 AWS services used: DataStore, DynamoDB Schema definition for model:

type MyModel @model { id: ID! timeToArrive: [Int] }

Any help or suggestions on how to resolve this issue would be greatly appreciated.

1 Answer

This is an expected behaviour when using AppSync's AutoMerge conflict detection strategy.

Conflict on a list: GraphQL type and database type are lists. Concatenate the incoming list with the existing list in the server. The list values in the incoming mutation will be appended to the end of the list in the server. Duplicate values will be retained.

You could consider changing the conflict resolution strategy to Optimistic Concurrency and implementing your own conflict resolution. Additionally, you may also refer the discussion on a similar Github discussion - #5541.

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions