0

I am using Amazon's Amplify library for GraphQL. Create mutations are returning non-null data in the response, but update mutations return null data in the response.

Amplify.API.query(ModelQuery.list(Login.class), response -> {
    boolean isThere = false;
    for (Login login : response.getData()) {
        if (login.getLoginEmail().equals(loginEmail)) {
            isThere = true;
            break;
        }
    }
    if (isThere) {
        Log.e("MyAmplifyApp", "Update Query");
        Amplify.API.mutate(ModelMutation.update(todo),
            response3 -> Log.i("MyAmplifyApp", "Updated Todo with id: " + response3.getData().getId()),
            error -> Log.e("MyAmplifyApp", "Update failed", error)
        );
    } else {
        Log.e("MyAmplifyApp", "Insert Query");
        Amplify.API.mutate(ModelMutation.create(todo),
            response2 -> Log.i("MyAmplifyApp", "Added Todo with id: " + response2.getData().getId()),
            error -> Log.e("MyAmplifyApp", "Create failed", error)
        );
    }
}, error -> Log.e("MyAmplifyApp", "Query failure", error));

Error Log:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.amplifyframework.datastore.generated.model.Login.getId()' on a null object reference
        at com.example.todo.Activity.LoginActivity.lambda$null$0(LoginActivity.java:722)
        at com.example.todo.Activity.-$$Lambda$LoginActivity$gU4azCKLr7DOG8SII3C8XdBDaxk.accept(lambda)
        at com.amplifyframework.api.aws.AppSyncGraphQLOperation$OkHttpCallback.onResponse(AppSyncGraphQLOperation.java:140)
        at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:760)
Jameson
  • 6,400
  • 6
  • 32
  • 53
Tushar Lathiya
  • 940
  • 9
  • 26

2 Answers2

0

What was issue:

I didn't supply the matching ID for the updated object. So API gets null id and sends null response.

Solution:

Per the documentation, just get id of existing user and pass with parameters which are you wanting to update, as below:

Login update = todo.copyOfBuilder()
    .loginName(NEW NAME)
    .id(id)
    .build();

Full Example:

Amplify.API.query(ModelQuery.list(Login.class), response -> {
    String id = null;
    boolean isThere = false;
    for (Login login : response.getData()) {
        if (login.getLoginEmail().equals(loginEmail)) {
            id = login.getId();
            isThere = true;
            break;
        }
    }
    if (isThere) {
        Log.e("MyAmplifyApp", "Update Query");
        Login update = todo.copyOfBuilder()
            .loginName(NEW NAME)
            .id(id)
            .build();
        Amplify.API.mutate(ModelMutation.update(update),
            response3 -> Log.i("MyAmplifyApp", "Updated Todo with id: " + response3.getData().getId()),
            error -> Log.e("MyAmplifyApp", "Update failed", error)
        );
    } else {
        Log.e("MyAmplifyApp", "Insert Query");
        Amplify.API.mutate(ModelMutation.create(todo),
            response2 -> Log.i("MyAmplifyApp", "Added Todo with id: " + response2.getData().getId()),
            error -> Log.e("MyAmplifyApp", "Create failed", error)
        );
    }
}, error -> Log.e("MyAmplifyApp", "Query failure", error));
Jameson
  • 6,400
  • 6
  • 32
  • 53
Tushar Lathiya
  • 940
  • 9
  • 26
0

problem is here ModelMutation.update(todo) here todo you are passing is null here you have to pass todo model with id which you get from response

if(isThere){
                    Log.e("MyAmplifyApp", "Update Query");
                    Amplify.API.mutate(
                            ModelMutation.update(todo),
                            response3 -> Log.i("MyAmplifyApp", "Updated Todo with id: " + response3.getData().getId()),
                            error -> Log.e("MyAmplifyApp", "Update failed", error)
                    );
                }else {
                    Log.e("MyAmplifyApp", "Insert Query");
                    Amplify.API.mutate(
                            ModelMutation.create(todo),
                            response2 -> Log.i("MyAmplifyApp", "Added Todo with id: " + response2.getData().getId()),
                            error -> Log.e("MyAmplifyApp", "Create failed", error)
                    );
                }
            }

Solution :

  • Step 1 -> create data in table
  • Step 2 -> get data (Here you get id with data )
  • Step 3 -> at android side make new model with new data
  • Step 4 -> update model on perticular id which you get in step no 2

you have to pass id in newly created model from there it will automatically take

id you will get when you retrieve list from database

Todo todo1 = Todo.builder()
             .name("My updated todo")
             .id(todo.getId())
             .build();
Amplify.API.mutate(
              ModelMutation.update(todo1),
               updateSprint -> {
//       Log.e( "Updatesdfsdfsdf--->", updateSprint.getData().getId()+"");
                                },
               error -> {
                         Log.e(TAG, "Error", error);
                         });
Manthan Patel
  • 1,784
  • 19
  • 23