RxJava 支持功能
如果您正在使用Apollo Kotlin 在一个Java 项目 或者使用 RxJava 的 Kotlin 项目中,您可以使用 Apollo 的 RxJava 扩展功能。
为此,将apollo-rx2-support
或 apollo-rx3-support
依赖项添加到项目中:
build.gradle[.kts]
dependencies {// ...// For RxJava 2implementation("com.apollographql.apollo3:apollo-rx2-support:3.8.5")// For RxJava 3implementation("com.apollographql.apollo3:apollo-rx3-support:3.8.5")}
执行操作
使用rxSingle
/ rxFlowable
扩展来执行 GraphQL 操作,并获取 RxJava observables:
import com.apollographql.apollo3.rx2.Rx2Apollo;// (...)// QueryApolloCall<MyQuery.Data> queryCall = client.query(new MyQuery());Single<ApolloResponse<MyQuery.Data>> queryResponse = Rx2Apollo.single(queryCall);queryResponse.subscribe( /* ... */ );// MutationApolloCall<MyMutation.Data> mutationCall = client.mutation(new MyMutation("my-parameter"));Single<ApolloResponse<MyMutation.Data>> mutationResponse = Rx2Apollo.single(mutationCall);mutationResponse.subscribe( /* ... */ );// SubscriptionApolloCall<MySubscription.Data> subscriptionCall = client.subscription(new MySubscription());Flowable<ApolloResponse<MySubscription.Data>> subscriptionResponse = Rx2Apollo.flowable(subscriptionCall);subscriptionResponse.subscribe( /* ... */ );
// Queryclient.query(MyQuery()).rxSingle().subscribe(/* ... */)// Mutationclient.mutation(MyMutation("my-parameter")).rxSingle().subscribe(/* ... */)// Subscriptionclient.subscription(MySubscription()).rxFlowable().subscribe(/* ... */)