Mutiny
Apollo Android includes support for Mutiny.
Apollo types can be converted to Mutiny Uni
types using wrapper functions MutinyApollo.from(...)
in Java or using extension functions in Kotlin.
Conversion is done according to the following table:
Apollo type | Mutiny type |
---|---|
ApolloCall<T> | Uni<Response<T>> |
ApolloSubscriptionCall<T> | Multi<Response<T>> |
ApolloQueryWatcher<T> | Uni<Response<T>> |
ApolloStoreOperation<T> | Uni<T> |
ApolloPrefetch | Uni<Void> |
Prerequisites
See Mutiny documentation
Including in your project
Add the following dependency
:
Groovy
1// Mutiny support
2implementation 'com.apollographql.apollo:apollo-mutiny-support:x.y.z'
Usage examples
Converting ApolloCall
to an Uni
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// Mutiny Uni
8Uni<Response<EpisodeHeroName.Data>> uni = MutinyApollo.from(apolloCall);
Kotlin:
Kotlin
1// Create a query object
2val query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())
3
4// Directly create Uni with Kotlin extension
5val uni = apolloClient.mutinyQuery(query)
Converting ApolloPrefetch
to a Uni<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//Mutiny Uni<Void>
8Uni<Void> uni = MutinyApollo.from(apolloPrefetch);
Kotlin:
Kotlin
1// Create a query object
2val query = EpisodeHeroNameQuery(episode = Episode.EMPIRE.toInput())
3
4// Create Uni for prefetch with Kotlin extension
5val uni = apolloClient.mutinyPrefetch(query)
Also, don't forget to cancel of your Observer/Subscriber when you are finished:
Java
1Cancellable cancellable = ReactorApollo.from(query).subscribe().with(item -> log("👍 " + item))
2
3//Cancel of your Observer when you are done with your work
4cancellable.cancel();