- 最新
- 投票最多
- 评论最多
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:
- https://docs.amplify.aws/lib/graphqlapi/getting-started/q/platform/flutter/#configure-api
- https://docs.amplify.aws/lib/graphqlapi/mutate-data/q/platform/flutter/
- https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/flutter/
- GitHub Issue Discussion: https://github.com/aws-amplify/amplify-flutter/issues/1434
Having said that, in case you face further challenges, please feel free to open a support case with AWS using the following link.
thank you and sorry, I had this one solved, but forgot to delete.