android - 如何在安卓客户端使用graphql订阅

标签 android graphql apollo-client graphql-subscriptions

如何在 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 服务器端点发送 rest 请求的方法:

  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 客户端中使用订阅的示例。 请帮我解决这个问题。

最佳答案

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

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

关于android - 如何在安卓客户端使用graphql订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53823099/

相关文章:

java - Android - 使用简单 XML 仅从 XML 文件中获取一些元素

c# - GraphQL + Autofac

cordova - 是否可以将 GraphQL 与 SQLite 或 PouchDB 等离线移动数据库一起使用

reactjs - Apollo 客户端 - 具有来自对象的计算值的本地字段

android - 谷歌地图精度半径

Android:从溢出菜单项启动警报对话框

javascript - NestJS - 预期未定义为 GraphQL 模式

reactjs - 使用apollo客户端,如何使用 react 变量触发重新订阅?

graphql - Apollo GraphQL (React) 跨组件数据共享

android - *BindingImpl 类 : cannot inherit from final *Binding