如何在android客户端中使用graphql订阅
如何在Apollo Android客户端中使用graphq订阅。 现在我的服务器上有了代码:如何在android客户端中使用graphql订阅,android,graphql,apollo-client,graphql-subscriptions,Android,Graphql,Apollo Client,Graphql Subscriptions,如何在Apollo Android客户端中使用graphq订阅。 现在我的服务器上有了代码: type Subscription { targetLocationUpdated(id: String!): Target } 和我的解析器的代码: Subscription: { locationAdded: { subscribe: () => pubsub.asyncIterator(LOCATION_ADDED), }, tar
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客户端使用订阅的示例。
请帮助我解决这个问题。我参考了这个链接,它可能会帮助你或其他人
ApolloSubscriptionCall subscriptionCall=application.apolloClient()
.认购(新的认购书(认购书全名));
一次性用品。添加(RX2 APOLLO.from)(subscriptionCall)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(
新的可处置订户(){
@覆盖公共void onNext(响应){
commentsListViewAdapter.addItem(response.data().commentAdded().content());
Toast.makeText(githUnterryDetailActivity.this,“收到订阅响应”,Toast.LENGTH\u SHORT)
.show();
}
@覆盖公共无效onError(可丢弃的e){
Log.e(标记,e.getMessage(),e);
Toast.makeText(githUnterryDetailActivity.this,“订阅失败”,Toast.LENGTH_SHORT.show();
}
@重写公共void onComplete(){
Log.d(标记为“认购耗尽”);
Toast.makeText(githUnterryDetailActivity.this,“订阅完成”,Toast.LENGTH_SHORT.show();
}
}
)
);
谢谢,我会试试。你能解决这个问题吗。
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();
}
}
)
);