Introduction: Why GraphQL and Swift Are a Perfect Pair
Modern iOS development is unthinkable without efficient interaction with the server. REST API is gradually giving way to a more flexible and performant technology — GraphQL. In this tutorial, we will break down how to integrate a GraphQL API into a Swift application in practice, using the Apollo iOS library.
GraphQL allows you to request exactly the data you need — no more, no less. For Swift, this means reduced traffic, faster performance, and simplified code. We will go through the entire process: from project setup to executing complex queries and mutations.
1. Project Setup and Installing Apollo iOS
The first step is preparing an Xcode project and installing dependencies. The most convenient way is via Swift Package Manager.
1.1 Creating a New Project
Open Xcode and create a new project (iOS App or macOS App). Choose SwiftUI or UIKit — it doesn't matter. Let's name it GraphQLDemo.
1.2 Adding Apollo iOS via SPM
- In Xcode, go to: File → Add Package Dependencies...
- Paste the repository URL:
https://github.com/apollographql/apollo-ios.git - Select the latest stable version and click Add Package.
- Make sure the package is added to your app's target.
1.3 Generating Schema Code
Apollo iOS uses code generation. For this, we need a schema file .graphqls (usually downloaded from the server) and query files .graphql.
// Project structure:// GraphQLDemo/// ├── GraphQL/// │ ├── schema.graphqls // Server schema// │ ├── Queries.graphql // Queries// │ └── Mutations.graphql // MutationsAfter adding the files, run code generation via the terminal:
cd /path/to/project./Pods/Apollo/apollo-ios-cli --download-schema --endpoint=https://your-server/graphql./Pods/Apollo/apollo-ios-cli --generateNow you have generated Swift files with types and queries.
2. Basic Query in Swift
Let's break down a simple query to fetch a list of users. Suppose our schema contains a User type with fields id, name, email.
2.1 Creating a .graphql File
In the GraphQL folder, create a file Queries.graphql:
query GetUsers { users { id name email }}2.2 Executing the Query in Swift
First, initialize the Apollo client:
import Apollo
class NetworkManager { static let shared = NetworkManager() let apollo: ApolloClient private init() { let url = URL(string: "https://your-server/graphql")! apollo = ApolloClient(url: url) }}Now execute the query in any ViewController or ViewModel:
import SwiftUI
struct UsersView: View { @State private var users: [GetUsersQuery.Data.User] = [] var body: some View { List(users, id: \\.id) { user in VStack(alignment: .leading) { Text(user.name) .font(.headline) Text(user.email) .font(.subheadline) .foregroundColor(.gray) } } .onAppear { loadUsers() } } private func loadUsers() { NetworkManager.shared.apollo.fetch(query: GetUsersQuery()) { result in switch result { case .success(let graphQLResult): if let usersData = graphQLResult.data?.users { self.users = usersData } case .failure(let error): print("Error: \\(error.localizedDescri