0

我正在为 GraphQL 使用 Amazon 的 Amplify 库。创建突变在响应中返回非空数据,但更新突变在响应中返回空数据。

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));

错误日志:

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)
4

2 回答 2

0

什么是问题:

我没有为更新的对象提供匹配的 ID。因此 API 获取 null id 并发送 null 响应。

解决方案:

根据文档,只需获取id现有用户并传递您要更新的参数,如下所示:

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

完整示例:

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));
于 2020-09-03T11:08:25.097 回答
0

问题在 ModelMutation.update(todo)这里todo你正在传递是null 在这里你必须传递你从响应todo中获得的模型id

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)
                    );
                }
            }

解决方案 :

  • 步骤 1 -> 在表中创建数据
  • 第 2 步 -> 获取数据(在这里你得到id数据)
  • 第 3 步 - > 在 android 端使用新数据制作新模型
  • 第 4 步 -> 在您输入的特定 id 上更新模型step no 2

您必须id从那里传入新创建的模型,它将自动采用

id当你从数据库中检索列表时你会得到

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);
                         });
于 2020-09-03T11:18:46.907 回答