Does ModelMutations.update() from flutter to DynamoDB require a primary key?

0

https://docs.amplify.aws/lib/graphqlapi/mutate-data/q/platform/flutter/#run-a-mutation

Referencing the official, I've been trying to .update() an item I was successfully able to add through the .create(). The mutation response that I receive shows me a successful attempt, but there are no actual updates being made.

Tried to make simpler changes, such as switching bools, but same result.

Then I remembered that making changes from lambda required the .key() method to check the primary key of the item to be updated. Is there a same check mechanism from flutter/Dart? If so, what would the implementation of that look like?

1개 답변
0
수락된 답변

Thank you for reaching out us regarding the above query. I would like to share that, for performing update/delete mutation, primary_key for the existing record would be needed for the mutation to be successful.

For example, consider the below sample schema, in which 'id' would be used as the primary key :

type Todo @model {
  id: ID!
  name: String!
  description: String
}

Now, in my test setup, i have performed and tested the update mutation in two ways:

1. Making the .create and .update mutation in a same function:

//Creating a record
Todo todo = Todo(name: 'my first todo', description: 'todo description');
final request = ModelMutations.create(todo);
final response = await Amplify.API.mutate(request: request).response;
Todo? createdTodo = response.data;

//Updating a record using the returned response for .create()
final todoWithNewName = createdTodo.copyWith(name: 'new todo');
final request1 = ModelMutations.update(todoWithNewName);
final response1 = await Amplify.API.mutate(request: request1).response;
Todo? updatedTodo = response1.data;

2. Making .update mutation in a separate function

//At first, fetching the existing record with id
final request = ModelQueries.get(Todo.classType,"<passing_record_id>");
final response = await Amplify.API.query(request: request).response;
Todo? createdTodo = response.data;

//then, updating the fetched record 
final todoWithNewName = createdTodo.copyWith(name: 'updated todo');
final request1 = ModelMutations.update(todoWithNewName);
final response1 = await Amplify.API.mutate(request: request1).response;
Todo? updatedTodo = response1.data;

While making the request in both the case, I was able to observe that the update mutation was performed as expected. However, when making the request with incorrect record (i.e, wrong value of id or without id), i observed that it was returning null in the response. Kindly note that the above code is only shared for reference purposes.

Additionally, please note that the AWS API plugins do not support conflict detection. If AppSync returns errors about missing _version and _lastChangedAt fields, or unhandled conflicts, then we will have to disable the conflict detection in our API.

Please refer to the below links that might be useful to move past this issue:

Having said that, in case you face further challenges, please feel free to open a support case with AWS using the following link.

AWS
지원 엔지니어
답변함 일 년 전
  • thank you and sorry, I had this one solved, but forgot to delete.

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

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

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

관련 콘텐츠