GraphQL API with Swift: Step-by-Step Practical Tutorial

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

Introduction to GraphQL and Swift

\n

GraphQL is a query language for APIs that allows clients to request exactly the data they need, and nothing extra. For iOS and macOS developers, GraphQL integration opens up new possibilities for creating efficient and flexible mobile applications. In this tutorial, we will practically walk through how to connect a GraphQL API to a Swift project, perform basic queries and mutations, and handle potential errors.

\n

Unlike traditional REST, GraphQL allows requesting multiple resources in a single query, which is especially important for mobile devices with limited bandwidth. We will use the popular Apollo iOS library — the most stable and well-documented solution for working with GraphQL in Swift.

\n\n

Project Setup and Installing Apollo iOS

\n

Step 1: Creating a Project and Installing Dependencies

\n

Open Xcode and create a new iOS (or macOS) project. To work with GraphQL, we will install Apollo iOS via Swift Package Manager (SPM).

\n
    \n
  1. In Xcode, select File → Add Packages
  2. \n
  3. Paste the repository URL: https://github.com/apollographql/apollo-ios.git
  4. \n
  5. Select the latest stable version (at the time of writing — 1.15+)
  6. \n
  7. Add the package to your target
  8. \n
\n\n

Step 2: Generating Code from the GraphQL Schema

\n

Apollo iOS uses code generation: you describe queries in .graphql files, and the utility creates typed Swift code. To do this, you need to:

\n
    \n
  1. Create a GraphQL folder in your project
  2. \n
  3. Place the schema.graphqls file (your API schema) there — usually obtainable via introspection
  4. \n
  5. Create a query file, for example GetUser.graphql:
  6. \n
\n\n
query GetUser($id: ID!) {\n  user(id: $id) {\n    id\n    name\n    email\n    posts {\n      title\n    }\n  }\n}
\n\n

Then add a Build Phase to run the code generator. The easiest way is to install the Apollo CLI via Homebrew:

\n
brew install apollo-ios-cli
\n\n

And run the command in the project root:

\n
./apollo-ios-cli generate
\n\n

After this, generated Swift files with data models will appear in the Sources/API folder.

\n\n

Performing Basic GraphQL Queries

\n

Setting Up the Apollo Client

\n

Create an instance of the Apollo client, specifying the URL of your GraphQL endpoint. This is usually done in AppDelegate or a separate service class:

\n\n
import Apollo\n\nclass NetworkService {\n    static let shared = NetworkService()\n    private(set) lazy var apollo: ApolloClient = {\n        let url = URL(string: "https://your-api.com/graphql")!\n        return ApolloClient(url: url)\n    }()\n}
\n\n

Fetching Data (Query)

\n

Now let's execute a query to fetch a user. The generated code will create a GetUserQuery structure:

\n\n
import Foundation\n\nclass UserViewModel: ObservableObject {\n    @Published var user: User?\n    @Published var errorMessage: String?\n    \n    func fetchUser(id: String) {\n        NetworkService.shared.apollo.fetch(query: GetUserQuery(id: id)) { [weak self] result in\n            DispatchQueue.main.async {\n                switch result {\n                case .success(let graphQLResult):\n                    if let userData = graphQLResult.data?.user {\n                        self?.user = User(\n                            id: userData.id,\n                            name: userData.name,\n                            email: userData.email\n                        )\n                    }\n                case .failure(let error):\n                    self?.errorMessage = "Error: \\(error.localizedDescription)"\n                }\n            }\n        }\n    }\n}
\n\n

Note: we use @Published for reactive UI updates in

Blogs

Book Recommendations