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 Answer
0
Accepted Answer

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
SUPPORT ENGINEER
answered a year ago
  • thank you and sorry, I had this one solved, but forgot to delete.

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