android - 如何在 android 客户端中使用 graphql 订阅
问题描述
如何在 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 客户端中使用订阅的示例。请帮我解决这个问题。
解决方案
我提到了这个链接,它可以帮助你或其他人。
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();
}
}
)
);
推荐阅读
- ajax - ajax中url的路径中有两个斜杠
- javascript - 更改迷你图工具提示条件
- c# - FileStream.SetLength:新旧长度之间的流内容是否真的未定义,还是为零?
- angular - 表单数组。找不到带路径的控件
- c - C获取范围之间的奇数并将其存储在int数组中
- perl - Perl Config::Tiny->read() 不处理 CRLF
- javascript - 检查圆与圆扇区的周长之间的碰撞
- python - Python 函数:返回 x < y
- python - 使用自定义转换器子类对 sklearn 管道进行评分时出现 AttributeError,但在拟合时却没有
- javascript - 将php变量传递给javascript参数