Introduction to GraphQL and Swift
GraphQL is a modern query language for APIs, developed by Facebook. Unlike REST, where you get a fixed data structure, GraphQL allows the client (your Swift application) to request only the fields that are actually needed. This is especially relevant for mobile developers working with iOS and macOS: less data means less traffic, faster loading, better UX.
In this tutorial, we will walk through how to connect a GraphQL API to your Swift project, perform basic queries and mutations. We will use the official client Apollo iOS — the most popular and well-supported library for working with GraphQL in the Apple ecosystem.
By the end of this article, you will be able to:
- Set up Apollo iOS in an Xcode project
- Write and execute a GraphQL query
- Send a mutation to change data
- Handle responses and errors
1. Project Setup and Installing Apollo iOS
1.1. Creating a New Project
Open Xcode and create a new project (iOS App or macOS App). Choose the Swift language and SwiftUI or UIKit interface — it doesn't matter, as networking does not depend on the UI framework.
1.2. Installation via Swift Package Manager
Apollo iOS is easily added via SPM. Go to File → Add Packages... and paste the URL:
https://github.com/apollographql/apollo-ios.gitSelect the latest stable version (at the time of writing — 1.15.0) and add the package to your target.
Also install the Apollo Code Generation CLI — a tool for generating Swift types from a GraphQL schema. The easiest way is to install it via Homebrew:
brew install apollo-ios-cliIf Homebrew is not installed, you can use npm:
npm install -g apollo-ios-cli1.3. Initializing Configuration
In the terminal, from the root of your project, run:
apollo-ios-cli init --module-name YourAppName --schema-namespace GraphQLThis command will create an apollo-codegen-config.json file and a GraphQL folder where your .graphql files will be stored.
Now add the server schema to the project. If the server provides schema.json or schema.graphqls, copy it to the GraphQL folder. If not, you can use the built-in command to download it:
apollo-ios-cli fetch-schema --endpoint=https://your-graphql-endpoint.com/graphql2. Writing Your First GraphQL Query
2.1. Creating a .graphql File
In the GraphQL folder, create a file Queries.graphql and write a simple query. Let's assume we have an API for fetching a list of users:
query GetUsers { users { id name email }}2.2. Generating Swift Code
Run the code generation from the terminal:
apollo-ios-cli generateAfter successful generation, a file API.swift will appear in the Sources/API folder (or the one you specified) with types like GetUsersQuery, GetUsersQuery.Data, etc.
2.3. Executing the Query from Swift
Now let's create an instance of the Apollo client and execute the query. In your ContentView.swift (or any other file), write:
import Apolloimport SwiftUI
class NetworkManager { static let shared = NetworkManager() private(set) lazy var apollo = ApolloClient(url: URL(string: "https://your-graphql-endpoint.com/graphql")!)}
struct ContentView: View { @State private var users: [GetUsersQuery.Data.User] = [] var body: some View { List(users, id: \\.id) { user in VStack(alignment: .leading) { Text(user.name)