GraphQL for Swift: Apollo iOS — Client for iOS and macOS

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Apollo iOS: Powerful GraphQL Client for Swift Applications



Apollo iOS is a modern, type-safe library for working with GraphQL APIs on Apple platforms: iOS, macOS, tvOS, and watchOS. Developed by Apollo GraphQL, it provides developers with convenient tools for executing queries, mutations, and subscriptions, automatically generating strictly typed Swift code based on your GraphQL schema.



The library is ideal for building modern mobile and desktop applications that require flexible data handling, caching, and high performance. Apollo iOS eliminates manual JSON parsing and ensures complete type safety at compile time.



Key Features

  • Automatic code generation — based on .graphql files and server schema, typed data models, queries, and mutations are created.
  • Normalized caching — built-in cache automatically updates the UI when data changes, avoiding object duplication.
  • WebSocket subscription support — real-time updates for chats, notifications, and other dynamic data.
  • Integration with SwiftUI and Combine — native asynchronous Publisher and ObservableObject for reactive programming.
  • Multithreading — all network requests execute in the background, keeping the main thread free for UI.
  • File upload support — works with multipart requests for sending images and documents.


Installation via Swift Package Manager

Add Apollo iOS to your project through Xcode or Package.swift:

// Package.swiftdependencies: [    .package(url: "https://github.com/apollographql/apollo-ios.git",              from: "1.0.0")]


Example: Setting up a client and executing a query

Let's create a client instance and execute a simple GraphQL query to fetch a list of users:

import Apollo

// 1. Create the clientlet url = URL(string: "https://api.example.com/graphql")!let store = ApolloStore()let client = ApolloClient(url: url, store: store)

// 2. Define the query (file Users.graphql)// query GetUsers { users { id name email } }

// 3. Execute the queryclient.fetch(query: GetUsersQuery()) { result in switch result { case .success(let graphQLResult): if let users = graphQLResult.data?.users { for user in users { print("\\(user.name) — \\(user.email)") } } case .failure(let error): print("Error: \\(error.localizedDescription)") }}


Working with Mutations (Data Modification)

Example of a mutation to create a new user:

// mutation CreateUser($name: String!, $email: String!) {//   createUser(name: $name, email: $email) { id name email }// }

let mutation = CreateUserMutation(name: "Ivan Petrov", email: "ivan@example.com")

client.perform(mutation: mutation) { result in switch result { case .success(let graphQLResult): if let newUser = graphQLResult.data?.createUser { print("User created: \\(newUser.id)") } case .failure(let error): print("Creation error: \\(error)") }}


Subscriptions (Real-time Updates)

For real-time data, use subscriptions. Requires WebSocket transport:

import ApolloWebSocket

let webSocketTransport = WebSocketTransport( request: URLRequest(url: URL(string: "wss://api.example.com/graphql")!))let store = ApolloStore()let client = ApolloClient(networkTransport: webSocketTransport, store: store)

// subscription OnUserAdded { userAdded { id name } }let subscription =

Recommendations