Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating model attribute using Amplify.API.Query #3526

Open
ip5038 opened this issue Feb 16, 2024 · 3 comments
Open

Updating model attribute using Amplify.API.Query #3526

ip5038 opened this issue Feb 16, 2024 · 3 comments
Labels
api Issues related to the API category bug Something isn't working

Comments

@ip5038
Copy link

ip5038 commented Feb 16, 2024

Describe the bug

I'm fetching a model via Amplify.API.query and updating one of the model attributes. Then I'm saving that model again, but it doesn't update the attribute.

Here is what happens
Mutation is triggered by user clicking on a button. On each click Amplify.Datastore.save(theModel) is called.

  1. First click does nothing
  2. Second click updates the _version but not the attribute.
  3. Third click updates the version and the attribute (as expected).

The model I am trying to do this on has secondary index, but I also have another model (which doesn't have secondary index) and it doesn't work either. I update both models in the same way, but no luck.

I am not comfortable sharing the schema. So here is a sample schema with the same structure and GSI setup

enum MyEnum {
Enum_Val_A
Enum_Val_B
Enum_Val_C
Enum_Val_D
}

type MyModel @model @auth(rules: [{allow: public}]) {
id: ID!
attribute_A: ID! @Index(name: "myGSI", sortKeyFields: ["attribute_C"], queryField: "myQueryField")
attribute_B: ID!
attribute_C: MyEnum
attribute_D: ID
}

What I have tried to fix
I have conflict resolution enabled . I tried auto-merge, optimistic, and disabled the conflict resolution but same issue each time.

Steps To Reproduce

Steps to reproduce the behavior:
I am going to assume you have the models and backend setup, so I will only list the swift code execution needed.

!!!!! NOTE !!!!!!
This only happens if I clear the datastore before clicking the button. I need to clear datastore when new user logs in.

Before getting started 
1. Clear the amplify datastore (each time you restart the app).

Setup a button to trigger these steps each time its clicked:
2. Query the model by Id using Amplify.API.Query
3. Update one of the model attributes
4. Save that model via Amplify.Datastore.save()


Here is the code for the steps above
_update via DataStore_

 var model = Amplify.API.query(MyModel.self, byId: "the_id")
 model.attribute_C = ENUM_VAL_B; // This is new value for enum. 
 Amplify.DataStore.save(model)

I also tried using Amplify.API.mutate instead of datastore, but it did not work.
_update via API mutate_

 var model = Amplify.API.query(MyModel.self, byId: "the_id")
 model.attribute_C = ENUM_VAL_B; // This is new value for enum. 
 Amplify.API.mutate(model)

Expected behavior

The attribute on the model fetched via Amplify.API.query should be updated on first try.

Amplify Framework Version

12.6.0

Amplify Categories

API, DataStore

Dependency manager

Cocoapods

Swift version

6.9.2

CLI version

12.6.0

Xcode version

Version 15.2 (15C500b)

Relevant log output

No response

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

iOS 17.0

Device

iPhone 15 Pro Simulator

Specific to simulators

No. It happens on real device as well.

Additional context

No response

@harsh62 harsh62 added api Issues related to the API category bug Something isn't working labels Feb 16, 2024
@harsh62
Copy link
Member

harsh62 commented Feb 16, 2024

@ip5038 Thanks for raising the issue. Our team will look into the issue and provide an update as soon as we can.

@ip5038
Copy link
Author

ip5038 commented Mar 1, 2024

I wasn't able to find the fix for this issue, but the workaround to this is using a custom lambda conflict resolver. Add a custom lambda resolver via amplify cli and you can configure how you want to deal with conflicts. In my case, I just resolved the conflict without doing checks for versions or anything.

Amplify docs has information on how to setup custom lambda resolver.
This will also come in handy if you want to learn more about conflict resolutions including custom lambda resolutions: https://docs.aws.amazon.com/appsync/latest/devguide/conflict-detection-and-sync.html

@thisisabhash
Copy link
Member

thisisabhash commented Mar 13, 2024

Hello, Do you know the Amplify version being used?
Latest version is 2.27.2 https://github.com/aws-amplify/amplify-swift/releases/tag/2.27.2

When you save the model after the update, are you seeing any errors being logged into the console? Are you able to receive .syncQueriesReady and .ready Datastore event? https://docs.amplify.aws/react/build-a-backend/more-features/datastore/datastore-events/#syncqueriesready

You can use
Amplify.Logging.logLevel = .verbose to enable verbose logging

It'd be great if you could attach the logs in the issue after removing sensitive information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issues related to the API category bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants