Reactor
Apollo Android includes support for Reactor.
Apollo types can be converted to Reactor Mono
types using wrapper functions ReactorApollo.from(...)
in Java or using extension functions in Kotlin.
Conversion is done according to the following table:
Apollo type | Reactor type |
---|---|
ApolloCall<T> | Mono<Response<T>> |
ApolloSubscriptionCall<T> | Flux<Response<T>> |
ApolloQueryWatcher<T> | Mono<Response<T>> |
ApolloStoreOperation<T> | Mono<T> |
ApolloPrefetch | Mono<Void> |
Prerequisites
See Reactor documentation
Including in your project
Add the following dependency
:
// Reactor supportimplementation 'com.apollographql.apollo:apollo-reactor-support:x.y.z'
Usage examples
Converting ApolloCall
to an Mono
Java:
// Create a query objectEpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();// Create an ApolloCall objectApolloCall<EpisodeHeroName.Data> apolloCall = apolloClient.query(query);// Reactor MonoMono<Response<EpisodeHeroName.Data>> mono = ReactorApollo.from(apolloCall);
Kotlin:
// Create a query objectval query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())// Directly create Mono with Kotlin extensionval mono = apolloClient.reactorQuery(query)
Converting ApolloPrefetch
to a Mono<Void>
Java:
//Create a query objectEpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();//Create an ApolloPrefetch objectApolloPrefetch<EpisodeHeroName.Data> apolloPrefetch = apolloClient.prefetch(query);//Reactor Mono<Void>Mono<Void> mono = ReactorApollo.from(apolloPrefetch);
Kotlin:
// Create a query objectval query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())// Create Mono for prefetch with Kotlin extensionval mono = apolloClient.reactorPrefetch(query)
Also, don't forget to dispose of your Observer/Subscriber when you are finished:
Disposable disposable = ReactorApollo.from(query).subscribe();//Dispose of your Observer when you are done with your workdisposable.dispose();
As an alternative, multiple Disposables can be collected to dispose of at once via Disposable.Composite
:
Disposable.Composite disposables = Disposables.composite();disposables.add(ReactorApollo.from(call).subscribe());// Dispose of all collected Disposables at oncedisposables.dispose();