Launch Apollo Studio

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 typeReactor type
ApolloCall<T>Mono<Response<T>>
ApolloSubscriptionCall<T>Flux<Response<T>>
ApolloQueryWatcher<T>Mono<Response<T>>
ApolloStoreOperation<T>Mono<T>
ApolloPrefetchMono<Void>

Prerequisites

See Reactor documentation

Including in your project

Add the following dependency:

// Reactor support
implementation 'com.apollographql.apollo:apollo-reactor-support:x.y.z'

Usage examples

Converting ApolloCall to an Mono

Java:

// Create a query object
EpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();

// Create an ApolloCall object
ApolloCall<EpisodeHeroName.Data> apolloCall = apolloClient.query(query);

// Reactor Mono
Mono<Response<EpisodeHeroName.Data>> mono = ReactorApollo.from(apolloCall);

Kotlin:

// Create a query object
val query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())

// Directly create Mono with Kotlin extension
val mono = apolloClient.reactorQuery(query)

Converting ApolloPrefetch to a Mono<Void>

Java:

//Create a query object
EpisodeHeroName query = EpisodeHeroName.builder().episode(Episode.EMPIRE).build();

//Create an ApolloPrefetch object
ApolloPrefetch<EpisodeHeroName.Data> apolloPrefetch = apolloClient.prefetch(query);

//Reactor Mono<Void>
Mono<Void> mono = ReactorApollo.from(apolloPrefetch);

Kotlin:

// Create a query object
val query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())

// Create Mono for prefetch with Kotlin extension
val 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 work
disposable.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 once
disposables.dispose();
Edit on GitHub