/
Launch Apollo Studio

RxJava3


Apollo Android includes support for RxJava 3.x.

Apollo types can be converted to RxJava3 Observable types using wrapper functions Rx3Apollo.from(...) in Java or using extension functions in Kotlin.

Conversion is done according to the following table:

Apollo typeRxJava3 type
ApolloCall<T>Observable<Response<T>>
ApolloSubscriptionCall<T>Flowable<Response<T>>
ApolloQueryWatcher<T>Observable<Response<T>>
ApolloStoreOperation<T>Single<T>
ApolloPrefetchCompletable

Including in your project

Add the following dependency:

// RxJava3 support
implementation 'com.apollographql.apollo:apollo-rx3-support:x.y.z'

Usage examples

Converting ApolloCall to an Observable

Java:

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

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

// RxJava3 Observable
Observable<Response<EpisodeHeroName.Data>> observable3 = Rx3Apollo.from(apolloCall);

Kotlin:

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

// Directly create Observable with Kotlin extension
val observable = apolloClient.rxQuery(query)

Converting ApolloPrefetch to a Completable

Java:

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

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

//RxJava3 Completable
Completable completable3 = Rx3Apollo.from(apolloPrefetch);

Kotlin:

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

// Create Observable for prefetch with Kotlin extension
val observable = apolloClient.rxPrefetch(query)

Also, don't forget to dispose of your Observer/Subscriber when you are finished:

Disposable disposable = Rx3Apollo.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 CompositeDisposable:

CompositeDisposable disposable = new CompositeDisposable();
disposable.add(Rx3Apollo.from(call).subscribe());

// Dispose of all collected Disposables at once
disposable.clear();

For a concrete example of using Rx wrappers for apollo types, checkout the sample app in the samples module.

Edit on GitHub