aws-amplify / amplify-swift

A declarative library for application development using cloud services.
Apache License 2.0
455 stars 196 forks source link

how to use update mutation on ios graphQL with swift #1075

Closed arnaudcisse closed 1 year ago

arnaudcisse commented 3 years ago

Hello, I am really struggling to run an update mutation on swift ios with amplify AWS (GraphQL API)

I am sure this question will help a lot of beginners on AWS, as it is directly related to the tutorial.

The tutorial from: (https://docs.amplify.aws/lib/graphqlapi/mutate-data/q/platform/ios)](url) says that to update/create a todo, you should:

func updateTodo() {
// Retrieve your Todo using Amplify.API.query
    var todo = Todo(name: "my first todo", description: "todo description")
    todo.description = "updated description"
    Amplify.API.mutate(request: .update(todo)) { event in
        switch event {
        case .success(let result):
            switch result {
            case .success(let todo):
                print("Successfully created todo: \(todo)")
            case .failure(let error):
                print("Got failed result with \(error.errorDescription)")
            }
        case .failure(let error):
            print("Got failed event with error \(error)")
        }
    }
}

So I understand that I first need to get the todo using a get (with an ID) or a list (with a predicate) query, then updating, so something along the line of the following:

func updateTodo(){
        Amplify.API.query(request: .get(Todo.self, byId: "FE978EC7-615F-4B2A-AB1C-0902B186377E")) { event in
            switch event {
            case .success(let result):
                switch result {
                case .success(var todoFound):
                    print("retrieved the todo of description \(todoFound?.description)")

                    todoFound?.description = "new todo description"

                    Amplify.API.mutate(request: .update(todoFound!)) { event in
                        switch event {
                        case .success(let result):
                            switch result {
                            case .success(let newTodo):

                                print("the new todo description is \(newTodo.description)")

                            case .failure(let graphQLError):
                                print("Failed to create graphql \(graphQLError)")
                            }
                        case .failure(let apiError):
                            print("Failed to create a todo", apiError)
                        }
                    }

                case .failure(let error):
                    print("Got failed result with \(error.errorDescription)")
                }
            case .failure(let error):
                print("Got failed event with error \(error)")
            }
        }
    }

However that does not work, do you know what I am doing wrong (the todo has the same description, it has not been updated!)

Many thanks for someone that can help me and please update the tutorial on AWS amplify!

Which AWS Services are you utilizing? Amplify GraphQL API

Provide code snippets (if applicable)

Environment(please complete the following information):

Device Information (please complete the following information): iOS

ruiguoamz commented 3 years ago

Hi, @arnaudcisse

Thanks for reaching out.

I tried out the code snippet you provided to get and update a model instance, it works for me. Can you double confirm that the get and update dose not work on your App?

Can you make sure several things?

  1. Does the Todo instance with the ID you try to retrieve exists?
  2. Can you make sure the description you try to update is different than the old one?

If the problem persists, can you provide more information? Like which Amplify version are you using? Which device are you using? How did you provision the backend? etc. Thanks

arnaudcisse commented 3 years ago

Hi ruigoamz,

So I think I solved the issue, it had to follow this thread: https://github.com/aws-amplify/amplify-cli/issues/4111

Basically, I think I could not update because I did not provide a version number. And amplify sent a success message, but the todo description was not updated. So following the thread, I removed the api, then added it again but disabling the datastore and removing the conflict description and now the todo descriptions are updating.

I think there should be some error message pointing this out. Let me know if you can't reproduce the issue!

palpatim commented 3 years ago

Leaving this open so @aws-amplify/ios-team can review docs. The _version requirement should only be in place when the API has conflict resolution enabled, which is typically something we do for DataStore. However, if a customer enables conflict resolution and continues to use the API category, we need to ensure that we've properly guided customers to provide a _version.

alionthego commented 3 years ago

I want to know why we need to retrieve the Item with a query just to update a parameter? It seems a bit silly. Why can't I just pass the item id and the parameter I want to update??? I understand you guys build this stuff to work with a local datastore but please understand that not everybody wants to give up complete client side control of their app. I just want to simply update one field in my database and for that I have to do it in two parts. Firstly to send a query to retrieve the entire item and then secondly modify one field and then do an update mutation. Makes no sense.

diegocstn commented 3 years ago

@alionthego you can read here a quite detailed explanation about why at the moment providing the full model is necessary.

alionthego commented 3 years ago

thanks for the link to the explanation. It's still not clear why this can't be done with some logic in the SDK. Currently you can modify one field only from a lambda function. In any case thanks for the link

royjit commented 3 years ago

Leaving this open for the amplify team to update the doc as mentioned https://github.com/aws-amplify/amplify-ios/issues/1075#issuecomment-789112124