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
:
Groovy
1// Reactor support
2implementation 'com.apollographql.apollo:apollo-reactor-support:x.y.z'
Usage examples
Converting ApolloCall
to an Mono
Java:
Java
1// Create a query object
2EpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();
3
4// Create an ApolloCall object
5ApolloCall<EpisodeHeroName.Data> apolloCall = apolloClient.query(query);
6
7// Reactor Mono
8Mono<Response<EpisodeHeroName.Data>> mono = ReactorApollo.from(apolloCall);
Kotlin:
Kotlin
1// Create a query object
2val query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())
3
4// Directly create Mono with Kotlin extension
5val mono = apolloClient.reactorQuery(query)
Converting ApolloPrefetch
to a Mono<Void>
Java:
Java
1//Create a query object
2EpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();
3
4//Create an ApolloPrefetch object
5ApolloPrefetch<EpisodeHeroName.Data> apolloPrefetch = apolloClient.prefetch(query);
6
7//Reactor Mono<Void>
8Mono<Void> mono = ReactorApollo.from(apolloPrefetch);
Kotlin:
Kotlin
1// Create a query object
2val query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())
3
4// Create Mono for prefetch with Kotlin extension
5val mono = apolloClient.reactorPrefetch(query)
Also, don't forget to dispose of your Observer/Subscriber when you are finished:
Java
1Disposable disposable = ReactorApollo.from(query).subscribe();
2
3//Dispose of your Observer when you are done with your work
4disposable.dispose();
As an alternative, multiple Disposables can be collected to dispose of at once via Disposable.Composite
:
Java
1Disposable.Composite disposables = Disposables.composite();
2disposables.add(ReactorApollo.from(call).subscribe());
3
4// Dispose of all collected Disposables at once
5disposables.dispose();