GraphQL with Apollo Android , why it’s worth taking a look.

Hamdi Boumaiza
Mar 3 · 5 min read
Photo by fotografierende on Unsplash

Introduction

So I’ve heard about GraphQL a while ago working in Proxym from my colleague Ramy, I was intrigued to learn about this new way of consuming APIs because so far and in the android club, the REST approach is the dominant player.

What is GraphQL

The short answer : GraphQL is a query language and server-side runtime for APIs that prioritizes giving clients exactly the data they request and no more.

GraphQL operations

GraphQL has three primary operations:

  1. Mutation for writing data
  2. Subscription for automatically receiving real-time data over time.

Why GraphQL

GraphQL is designed to make APIs fast, flexible, and developer-friendly. GraphQL lets developers construct requests that pull data from multiple data sources in a single API call. in other words get many resources in a single request and ask for what you need, get exactly that.

Main Difference between GraphQL and REST

The biggest problem with REST APIs is the nature of multiple endpoints. A medium size application can easily have over 100 endpoints, these require clients to do multiple round-trips to get their data.

GraphQL with android

In order to use GraphQL in an android application , we will need to use the Apollo Library which is a strongly typed client that generates Java and Kotlin models from GraphQL queries, so let’s talk about the needed steps to run Apollo in an android project.

Apollo Setup

We will start by adding this plugin at the project level

//project dependencies
dependencies {
classpath "com.apollographql.apollo:apollo-gradle-plugin:2.4.6"
}
plugins {
id 'com.apollographql.apollo'
}
dependencies {
def apollo = "2.4.6"
implementation "com.apollographql.apollo:apollo-runtime:$apollo"
implementation "com.apollographql.apollo:apollo-coroutines-support:$apollo"

}

Model generation

Apollo has this neat feature where you can enable generating Kotlin models , you can enable it by adding the code below in the module level of the gradle file.

apollo {
generateKotlinModels.set(true)
}

Adding the Schema

For generating the java models, a GraphQL schema and a query is required. The schema usually can downloaded from your API playground , in the android application , Schema should be added under the main folder like this example below “https://github.com/HamdiBoumaiza/ApolloRickAndMorty/blob/master/app/src/main/graphql/com/hb/rickandmortyapollo/schema.sdl

Adding Queries

The following is an example of a sample query for GraphQL , I used the Rick and Morty API

query GetCharacters($page: Int) {
characters(page: $page) {
info {
pages, count, next
}
results {
id, name, image,
episode {
id, name
}
}
}
}

Building the Apollo Client

Creating a basic Apollo client is very easy as you can see below , the Client can be configured in many ways, for example you can add an OkHttpClient or add an interceptor.

private fun apolloClient()=
ApolloClient.builder().serverUrl(BASE_URL).build()

Executing a query

After setting up the Client, we can now execute our queries and as you can see below we just have to use the query function and pass the query generated class to it as an argument

apolloClient().query(GetCharactersQuery(Input.optional(page)))

Final thoughts

After reading this and other articles about GraphQL ,there might be a question that pop into your mind , why is REST still the dominant player ?

Nerd For Tech

From Confusion to Clarification

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store