【问题标题】:How to use graphql subscriptions in android client如何在 android 客户端中使用 graphql 订阅
【发布时间】:2019-05-18 06:50:13
【问题描述】:

如何在 Apollo Android 客户端中使用 graphq 订阅。 现在我的服务器上有代码:

type Subscription {    
     targetLocationUpdated(id: String!): Target
}

我的解析器代码:

Subscription: {
    locationAdded: {
        subscribe: () => pubsub.asyncIterator(LOCATION_ADDED),
    },
    targetLocationUpdated: {
        subscribe: withFilter(
            () => pubsub.asyncIterator('TARGET_UPDATED'),
            (payload, variables) => {
                console.log(payload.targetLocationUpdated.id === variables.id);
                return payload.targetLocationUpdated.id === variables.id;
            }
        )
    }
}

我的 Android 客户端具有向我的 graphql 服务器端点发送休息请求的方法:

  public static ApolloClient getMyApolloClient() {


    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(loggingInterceptor)
            .build();
    myApolloClient = ApolloClient.builder()
            .serverUrl(BASE_URL)
            .okHttpClient(okHttpClient)
            .build();
    return myApolloClient;
}
}

但我不知道如何在 android 客户端上使用订阅。 在官方 appolo 文档中,我没有找到在 android 客户端中使用订阅的示例。 请帮我解决这个问题。

【问题讨论】:

  • 你能解决这个问题吗?

标签: android graphql apollo-client graphql-subscriptions


【解决方案1】:

我参考了这个链接,它可能对你或其他人有所帮助。

GitHuntEntryDetailActivity.java

ApolloSubscriptionCall<RepoCommentAddedSubscription.Data> subscriptionCall = application.apolloClient()
    .subscribe(new RepoCommentAddedSubscription(repoFullName));

disposables.add(Rx2Apollo.from(subscriptionCall)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeWith(
        new DisposableSubscriber<Response<RepoCommentAddedSubscription.Data>>() {
          @Override public void onNext(Response<RepoCommentAddedSubscription.Data> response) {
            commentsListViewAdapter.addItem(response.data().commentAdded().content());
            Toast.makeText(GitHuntEntryDetailActivity.this, "Subscription response received", Toast.LENGTH_SHORT)
                .show();
          }

          @Override public void onError(Throwable e) {
            Log.e(TAG, e.getMessage(), e);
            Toast.makeText(GitHuntEntryDetailActivity.this, "Subscription failure", Toast.LENGTH_SHORT).show();
          }

          @Override public void onComplete() {
            Log.d(TAG, "Subscription exhausted");
            Toast.makeText(GitHuntEntryDetailActivity.this, "Subscription complete", Toast.LENGTH_SHORT).show();
          }
        }
    )
);

【讨论】:

  • 谢谢,我试试看。
猜你喜欢
  • 2020-08-27
  • 2023-04-03
  • 2022-12-12
  • 2023-01-31
  • 2019-01-14
  • 2017-12-20
  • 2019-07-19
  • 2022-06-18
  • 2021-07-28
相关资源
最近更新 更多