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