【问题标题】:Graphql Mutation: Program Control doesn't return to either onResponse or onFailure after callGraphql 突变:调用后程序控制不会返回到 onResponse 或 onFailure
【发布时间】:2020-01-19 08:21:59
【问题描述】:

我正在开发一个使用 Apollo 客户端支持在 Android 中实现 Graphql 的应用程序。我有一个在 Android Activity 中成功运行的 Graphql 突变,但是当在 Android 片段中实现相同的突变时,它根本不会被调用。我的意思是控件永远不会返回到入队调用的 onResponse 或 onFailure 方法。 在 Fragment 的 onActivityCreated 方法中调用。

我是否遗漏了一些明显的东西?

这里是突变。

UserSignIn userSignIn = UserSignIn.builder()
.email("itxxxxd@gmail.com")
.password("xxxxx")
.build();
SignInMutation signInMutation = SignInMutation.builder().input(userSignIn).build();

ApiClient.getMyApolloClient().mutate(signInMutation).enqueue(new ApolloCall.Callback<SignInMutation.Data>() {
    @Override
    public void onResponse(@NotNull Response<SignInMutation.Data> response) {
        if (response.data() != null) {
            Timber.w("SignIn Data onResponse: %s", response.data().signIn().token);
        }
        if (!response.errors().isEmpty()) {
            Timber.w("SignIn Error onResponse: %s", response.errors().get(0).message());
        }
    }
    @Override
    public void onFailure(@NotNull ApolloException e) {
        Timber.w("SignIn Error onFailure: %s", e.toString());
    }
});

谢谢。

【问题讨论】:

  • 这不是片段或活动的问题。只是我的 Fragment 在控件返回到 onResponse 或 onFailure 方法之前以异常结束。这是我错过的显而易见的事情。谢谢。

标签: android android-fragments apollo-client graphql-java


【解决方案1】:

改为在片段的onViewCreated中调用该函数

UserSignIn userSignIn = UserSignIn.builder()
    .email("itxxxxd@gmail.com")
    .password("xxxxx")
    .build();
    SignInMutation signInMutation = SignInMutation.builder().input(userSignIn).build();

    ApiClient.getMyApolloClient().mutate(signInMutation).enqueue(new ApolloCall.Callback<SignInMutation.Data>() {
        @Override
        public void onResponse(@NotNull Response<SignInMutation.Data> response) {
            if (response.data() != null) {
                Timber.w("SignIn Data onResponse: %s", response.data().signIn().token);
            }else{
               //response can be null
    }
            if (!response.errors().isEmpty()) {
                Timber.w("SignIn Error onResponse: %s", response.errors().get(0).message());
            }
        }
        @Override
        public void onFailure(@NotNull ApolloException e) {
            Timber.w("SignIn Error onFailure: %s", e.toString());
        }
    });

【讨论】:

  • 按照建议,我将调用移至 onViewCreated 但它仍然相同。未调用 Mutation。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2019-06-20
  • 1970-01-01
  • 2017-10-17
  • 2020-10-09
  • 1970-01-01
相关资源
最近更新 更多