【问题标题】:GraphQL mutation returning null on updateGraphQL 突变在更新时返回 null
【发布时间】:2020-12-22 09:33:35
【问题描述】:

我正在为 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)

【问题讨论】:

  • response3.getData() failed then chained .getId() operation on null 也失败了
  • @xadm 我知道您的担忧,但在成功更新数据后它应该返回响应,否则它不会更新 DynamoDB 中的数据。
  • @Jameson 你对这个问题有什么想法吗??
  • 不要假设任何事情...... console.log 整个响应,只是调试它,一步一步
  • @xadm 也使整个响应为空。如果我删除 console.log 语句它可以工作,但它不会将数据更新到 DynameDB。很抱歉,我没有假设我已经在我的表格中手动检查了。

标签: android graphql aws-amplify aws-appsync amplify


【解决方案1】:

出了什么问题:

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

解决方案:

根据documentation, 只需获取现有用户的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));

【讨论】:

    【解决方案2】:

    问题就在这里 ModelMutation.update(todo) 这里todo 你经过的是null 在这里,您必须将 todo 模型传递给您从 response

    获得的 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 步 -> 更新您在 step no 2 中获得的特定 id 上的模型

    你必须在新创建的模型中传递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);
                             });
    

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 2023-01-09
      • 2019-06-28
      • 1970-01-01
      • 2021-06-25
      • 2021-04-01
      • 2019-11-25
      • 2018-06-18
      • 2021-10-09
      相关资源
      最近更新 更多