DataStore save function not updating array property correctly

1

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 DataStore.save(MyModel.copyOf(original, 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개 답변
0

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.

AWS
지원 엔지니어
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠