Apollo Kotlin - getting started with GraphQL on Android
Using GraphQL on Android has been a niche topic for quite a while and that might be a bit surprising, especially considering the prevalence of Android devices and the steadily growing popularity of GraphQL. Still even a quick google search will show you REST still dominates in the Android community. That said, something not being popular isn't a reason to discount it out of hand and since it's a new year, why not check out something different instead of going with the flow.
Who doesn't like GraphQL?!
If you’re here you’re probably quite familiar with the advantages of GraphQL so you might be wondering why the Android community has been slow in getting on the GraphQL hypetrain. While there are a number of factors, one is pretty glaring, the number of available tools. Thankfully there are some new libraries popping up here and there, but it's safe to say that if you’re trying to work with GraphQL on Android right now, Apollo's Android library is the only way.
So it looks like we’re stuck picking it, but what is it exactly? Well GraphQL was released publicly all the way back in 2015 and in early 2017 Apollo released Apollo Android v. 0.2 which provided some basic support for GraphQL model generation. It's been a few years since then and it has grown quite a bit larger, version 1.0 introduced Kotlin code generation and 2.0 introduced multiplatform query and subscription support. To get a better look at it's state let’s check out the main features:
- multiplatform: the library is designed with Android in mind, but can be used in any Java/Kotlin app
- makes full use of GraphQL: from basics like queries, mutations and subscriptions to more advanced stuff like query batching, persistent queries and an AST parser
- 3 levels of catching: HTTP Response Cache (for caching raw http responses), Normalized Disk Cache (per node caching of responses in SQL) and Normalized InMemory Cache (Optimized Guava memory cache for in memory caching)
- RXJava support
On that last one if you’re familiar with Java you’ve probably heard about RXJava, if not it's an extremely useful implementation of Reactive Extensions that has been popularized by Netflix. While Android Apollo generates Kotlin/Java models from GraphQL queries, RXJava (to quote the official description here) is a library for composing asynchronous and event-based programs by using observable sequences. That’s just the technical terms, to use simpler language it reverses the usual process where you request data, to an event based process where you're notified about data in the stream. Since you’re using GraphQL for requests (or rather queries) anyway it's simply very handy for use with subscriptions. That’s obviously not the only advantage and you can find a ton of info on the uses of RXJava so let’s just say it's popular for a good reason.
Apollo Android -> Apollo Kotlin
That’s not all, about a month ago Apollo Android made version 3.0.0 public. While the most noticeable difference was clearly the name change from Apollo Android to Apollo Kotlin, the changelog was a bit longer than that. Aside from the usual bugfixes and an update to Kotlin version 1.6.10 we can see a few new features:
- extended multiplatform support for Android, JS, iOS, MacOS and linux
- coroutine APIs for easier concurrency
- response based code generation engine
- SQLite batching
Your own niche
I hope that gave you a somewhat decent overview of the Apollo Kotlin library and maybe piqued your interest at least a tiny bit. As I mentioned previously it's not a very popular topic, so if you’re looking for your own niche this might be it. If not, the growing popularity of GraphQL will certainly keep influencing the Android community anyway, so it might be a good idea to beat others to it and start early. While Apollo Kotlin is the go-to tool now, no one can say that has to be the case forever. Maybe it will soon face competition from some new libraries and who can say one of those libraries can't be created by you?