Making GraphQL calls in iOS using Mercurius and Fastify backend
--
Mercurius and Fastify can also be used by the Apollo iOS client
Originally published at https://fek.io.
Make sure to add the following to this post
I have been researching using Mercurius and Fastify for a GraphQL backend for iOS. I think a lot of iOS developers are already familiar with using Apollo as a GraphQL backend, but I have a couple of Fastify services that I was already hosting as REST endpoints. In a previous post I wrote about adding a GraphQL endpoint to Fastify. In this post I wanted to write about how you can add iOS code generation to your existing iOS apps using Mercurius and Fastify as the backend.
Consuming GraphQL in iOS
The easiest way to consume GrapghQL on iOS is use the Apollo Client for iOS. This client library comes with a bunch of tools including code generation, and can be installed using CocoaPods, Swift Package Manager to your Xcode project or SPM with your Package.swift. For this post I am just going to use SPM with the Xcode project.
One of the problems I cam across in getting this working is that the Apollo docs do not show everything you need to get this working for your application. I hope to show how you can install and use these tools to make GraphQL queries.
Installing the Apollo-ios-cli
In the Apollo documentation, they suggest installing the command line tool using the right mouse click menu in the project explorer. As of Xcode 14.3, this option no longer works. I had to install the executable for the apollo-ios-cli directly in a path that my terminal could access. I installed the apollo-ios-cli
directly in my '/usr/local/bin' directory on my Mac. You can download the executable from the releases
link on the GitHub page for Apollo-ios. Download the release of apollo-ios-cli.tar.qz
for the version you plan on using of the library. At the time of writing this post, that was version 1.1.1. The library and cli have to be the same version.
You can confirm that the tool is installed and working by using the following command in the terminal:
$ apollo-ios-cli --version