GraphQL on Android with Apollo Client

Next revolution

GraphQL is a really hot topic now. While it’s adopted by backend and frontend, the natural path is to transform the mobile side also. But the task is not so trivial. After years of battling about the best mobile architecture, Android developers around the world agreed to a more or less common technological stack.

Benefits of GraphQL

GraphQL is a quite new solution with concepts varying from the current approach. Fundamental idea to perceive the results as a graph is made to cure most of the REST’s weak points.

  • Reduced size of response
  • Better performance
  • Generated documentation
  • Useful tools
  • Active community

Choose the right way

At this moment we have two ways to start consuming graph’s responses. First is to use Apollo Client written for mobile platforms. Second is to adjust the current HTTP consumer for receiving and sending query language requests.

apollo client graph ql
apollo client graph ql

Everything in single query

The main goal of this technology is to combine data from different sources in a single response. From the backend side it seems logical to have a separate table in the database and corresponding REST endpoint for a single type of information, for example a list of jobs.

  1. Fetch a list of companies
  2. Create a model for job
  3. Create a model for company
  4. Merge fields from both models in view and display

Strong typed data and model generation

Using the GraphQL, we always know the type of returned field. This way, it doesn’t lead to errors recognized from REST API, like expecting Integer and receiving String.

Cache strategy

With a single query, a user can fetch a lot of info. For example a list of jobs already with their details. It would be great to cache this list for future usage, e.g. offline work. For our convenience, Apollo Client has three steps of caching:

  • Normalized cache — stores in memory pieces of response that can be reused in future
  • Automatic Persisted Queries — immediately returns cached queries on server and thus graph doesn’t have to be searched every time

Without problems during migration

All these functionalities are awesome and mostly can convince developers that GraphQL is better and faster than REST, but what if we want to migrate an existing project?

Great documentation

This advantage doesn’t correspond directly to Apollo Client but working with well written documentation is easier and faster.

Summary

Nowadays, GraphQL seems to be a perfect technology for creating MVP. Developers can focus more on the visual layer of application and consume data right from the queries.

human-centric software design & development. check out our website: www.iteo.com