Prerequisites

Download your schema

The first step is to construct a GraphQL schema file for your content type and include this file in your project. This schema file is a JSON file named schema.json that contains the results of introspection queries and is used by Apollo iOS for the code generation process.

apolloClient has generic fetch(query:) methods that take a query argument conforming to the Apollo.GraphQLQuery protocol. To generate Swift code for your query models (you need not manually create them), download the GraphQL schema for your content model using Apollo CLI, and place it in the root directory of your Xcode project.

This step allows you to check whether the version of Apollo installed on your system is compatible with the current version of the framework installed in your project. If it isn’t, you will receive a warning.

Now, whenever you build your project, it executes apollo-codegen, which ensures that you will always have an updated set of Swift models and queries.

Creating ApolloClient

After downloading the schema and creating the queries, create a single shared instance of ApolloClient and point it at the GraphQL server.

To do this, define a global variable in AppDelegate.swift by using an immediately invoked closure as follows:

Queries are represented as instances of generated classes conforming to the GraphQLQuery protocol. When you pass a query object to the ApolloClient.fetch() method, upon a successful API response, the method returns typed results.