Kristaps Grinbergs iOS, tvOS, watchOS, macOS in Swift

Using GraphQL in iOS with Swift

28 Aug 2019

GraphQL is an alternative to the commonly used REST API approach. When using GraphQL you only get one single endpoint to the server and through that endpoint you need to specify the exact data you need. It was created by Facebook in 2012 and open-sourced in 2015.

Using GraphQL in your Swift applications is not that hard. The easiest way is to use Apollo iOS client which recently has seen a lot of great improvements and bug fixes. Apollo iOS is a strongly-typed, caching GraphQL client for iOS, macOS, iPadOS, watchOS, and tvOS. It is written in Swift programming language.

GraphQL vs REST

REST API exposes one endpoint for each specific type of information. For example:

  • /visitors - list of all visitors

  • /visitors/ID/ - concrete visitor details with ID

Imagine you would like to get a visitor by his name. At first, you need to get the visitors list and search by name. Once you find specific visitor ID you can get the rest of the visitor details from another endpoint.

Using GraphQL you could do this with just one request to the server. You just need to specify what kind of data would you like to get in declarative GraphQL syntax:

  {
    allVisitors(name: "John Appleseed") {
      id
      name
      reasonOfVisit
      isServed
      signInTime
      feeedback
      ...
    } 
  }

This is just one simple example. In reality, you could get multiple levels of data with just one call to the server.

Setting up GraphQL using Swift in iOS

You can use GraphQL in your iOS, iPadOS, macOS, watchOS and tvOS apps. The easiest way is to use Apollo iOS SDK which is a strongly-typed, caching GraphQL client. It is written in Swift programming language.

Installation

To set up Apollo client you need to install Apollo framework and generate schema file which describes how you can get information from the server and add to your Xcode app target.

Then you can create .graphql files with your queries or mutations on how you would like to get or change data from the server. After that, you need to add code generation build step in Xcode build process to generate type-safe Swift code to use for communication with your server.

Build your Xcode app target and add generated API file to use it.

Communicate with the server

Once the installation process is done you can set up ApolloClient instance and point to your GraphQL server.

  import Apollo

  let graphQLClient = ApolloClient(url: URL(string: "http://localhost:8080/graphql")!)

Then you can use graphQLClient to fetch data and process query results.

  let allVisitorsQuery = AllVisitorsQuery(name: "John")
  graphQLClient.fetch(query: allVisitorsQuery) { result in
    switch result {
    case .failure(let error):
      print("Something bad happened \(error)")
    case .success(let graphQLResult):
      guard let john = graphQLResult.data?.visitor else {
        return
      }
      
      print(john)  
  }

This is just a simple example in some of the next posts we will look into how we can mutate data and even use Websockets subscriptions.

GraphQL pros and cons

Every technology has its pros and cons. You need to understand if the tradeoffs are worth switching away from regular REST API.

Pros

GraphQL makes it faster to implement, change and maintain your applications. You don’t need to communicate every smaller detail with your backend or even create new endpoints.

Using Apollo iOS framework helps a lot to use GraphQL in your Swift applications. It does support iOS, iPadOS, watchOS, macOS and tvOS. You can use Swift type safety and other great features like Result type and more.

Lately, Apollo iOS framework has seen a lot of great updates and more bugs are being fixed. More and more companies switch to use GraphQL not only in web, but their mobile applications too.

Cons

Using GraphQL is not that straight forward especially if you are used to REST API approach. Switching your thinking and adapting a new way can take some time. It also adds an extra layer between server and your apps which you need to maintain in the future.

With Apollo iOS SDK you need to be very careful with caching. There are several ways to deal with cache and when to not cache at all. Just to be sure to check out official documentation before or if you run into any data inconsistency issues.

Apollo helps a lot to use GraphQL with Swift, but one downside is optionals. If you have the freedom to tell what kind of data you want from the server that comes with optionality tradeoff. It is a minor issue, just you need to deal with that when you construct your models and get data back from the server.

TL;DR

GraphQL is a modern way to communicate with your server. It can replace commonly used REST API approach. With GraphQL you communicate with the backend via just one endpoint using declarative syntax.

Using GraphQL in your Swift applications for iOS, iPadOS, watchOS, macOS and tvOS is easy using Apollo framework. It is officially supported by GraphQL community and lately, it has been well maintained and supported.

Using GraphQL in iOS with Swift