Apollo iOS Library for Working with GraphQL in Swift
Apollo iOS is a powerful and flexible library for working with GraphQL in the Apple ecosystem. It provides iOS and macOS developers with a convenient tool for executing queries, mutations, and subscriptions to GraphQL servers. Unlike REST API, GraphQL allows the client to request only the necessary data, which reduces traffic volume and speeds up application performance. Apollo iOS fully supports Swift, including modern language features such as async/await, Combine, and SwiftUI.
Key Features of Apollo iOS
- Code Generation: Automatic creation of type-safe data models based on the GraphQL schema.
- Caching: Built-in support for a normalized cache to optimize repeated queries.
- Swift Concurrency Support: Full integration with async/await for asynchronous operations.
- WebSocket Subscriptions: Implementation of real-time updates using GraphQL subscriptions.
- Combine and SwiftUI: Ability to use with Apple's reactive frameworks.
- Multiplatform: Works on iOS, macOS, tvOS, and watchOS.
Installing the Library
To install Apollo iOS, use Swift Package Manager. Add the package in Xcode via the menu File → Add Packages and specify the URL:
https://github.com/apollographql/apollo-ios.gitVersion: latest stable (at the time of writing — 1.15.0).
Setting Up Apollo Client
The first step is to create an instance of ApolloClient with the URL of your GraphQL server. Example of basic setup:
import Apollo
class NetworkService { static let shared = NetworkService() private(set) lazy var apollo: ApolloClient = { let url = URL(string: "https://api.example.com/graphql")! return ApolloClient(url: url) }()}Executing Queries
Apollo iOS generates code based on the GraphQL schema. First, define the query in a .graphql file:
query GetUser($id: ID!) { user(id: $id) { id name email posts { title } }}After code generation, execute the query:
import Apollo
func fetchUser(id: String) async throws -> GetUserQuery.Data.User? { let query = GetUserQuery(id: id) let result = try await NetworkService.shared.apollo.fetch(query: query) return result.data?.user}Mutations
To modify data, use mutations. Example of creating a new post:
mutation CreatePost($title: String!, $content: String!) { createPost(input: { title: $title, content: $content }) { id title createdAt }}Executing the mutation in Swift:
func createPost(title: String, content: String) async throws -> CreatePostMutation.Data.CreatePost? { let mutation = CreatePostMutation(title: title, content: content) let result = try await NetworkService.shared.apollo.perform(mutation: mutation) return result.data?.createPost}Working with Subscriptions
For real-time updates, use subscriptions. Example of subscribing to new messages:
subscription OnNewMessage { newMessage { id text sender { name } }}Implementation in Swift using Combine:
import Combine
var cancellables = Set<AnyCancellable>()
func subscribeToMessages() { let subscription = OnNewMessageSubscription() NetworkService.shared.apollo.subscribe(subscription: subscription) .sink { completion in print("Subscription