February 14, 2017

How to use GraphQL in your iOS app

Graphcool

Graphcool

This is a guest post by Nikolas Burk, who works on iOS and GraphQL education at Graphcool, an awesome GraphQL backend platform.

Learn Apollo is an interactive tutorial that explains how to develop applications using a GraphQL backend and Apollo client. It’s the fastest way to get started with Apollo & GraphQL, guiding you through practical examples using the client technology of your choice — be it React, React Native, Angular or Vue.

Note: If you have never heard of GraphQL before, it’s an API design paradigm that was introduced by Facebook and eliminates many shortcomings of today’s de-facto standard REST. Apollo is a client-side data fetching library that allows you to easily work with a GraphQL API and benefit from built-in features like caching, subscriptions, optimistic UI, pagination and server-side rendering without the overhead of implementing these yourself!

After the successful launch of Learn Apollo for React & React Native, we’re happy to introduce the iOS track for Learn Apollo. Similar to the other tracks you can find on the site, the goal is to build a fully-fledged Pokédex application that demonstrates major functionalities of the interaction with a GraphQL backend using an Apollo client — only this time it’s from within an iOS app using Swift! 🚀

The Apollo iOS client

The Apollo iOS Client is the best way to work with a GraphQL API from a native iOS app. Developers using the client will benefit from two major features:

  • Type generation based on GraphQL queries and mutations
  • Normalized cache & automatic UI updates

Type Generation

The first and presumably most important feature of the Apollo iOS client is the generation of native Swift types that reflect the data requirements of your specific application. The types are generated based on all the queries and mutations that are specified within the app’s Xcode project.

This approach leverages Swift’s static type system and, unlike most other data fetching approaches, provides compile-time safety for the developer in terms of what data they can expect from the backend. In Swift, this means that developers can get rid of all the optional properties they’d have to use when parsing the untyped JSON into their model objects, thus providing extra safety while retaining flexibility when it comes to changing data requirements.

Normalized Caching & Automatic UI updates

The second major feature of the Apollo iOS client is a normalized cache that allows for automatic UI updates. The responses that the client receives from the server are flattened and put into the client-side store. Similar to the Apollo client for other platforms, queries are watched in a reactive way so that an update function automatically gets executed whenever data associated with a watched query changes in the store — no matter where that change came from!

Throughout the tutorial you’ll build a fully-functional iOS app with GraphQL & Apollo

Why should an iOS developer care about GraphQL?

At Graphcool, we strongly believe that GraphQL is the API design paradigm of the future! Despite its young age, it already created an incredible impact on the way how web developers write their applications and interface with backends.

“We were frustrated with the differences between the data we wanted to use in our apps and the server queries they required. We don’t think of data in terms of resource URLs, secondary keys, or join tables; we think about it in terms of a graph of objects and the models we ultimately use in our apps like NSObjects or JSON.” — Lee Byron (Co-Inventor of GraphQL, Facebook)

Though GraphQL has been widely adopted in the web development community so far, Facebook initially developed it to deal with networking issues they primarily saw on mobile devices — with the goal of making the data transfer as efficient as possible while still enabling developers to iterate quickly. This is why we think that there is an enormous potential for GraphQL in the native mobile development space! We’re very excited to see more mobile developers using GraphQL, and want to build an ecosystem that makes it as easy as possible for every native developer to integrate GraphQL in their applications.

Written by

Graphcool

Graphcool

Read more by Graphcool