Definition

Executing queries in Apollo iOS involves sending a GraphQL query to the server using ApolloClient and handling the typed response asynchronously.


Core Concept

Queries are executed using generated query objects (from .graphql files).

Apollo sends the request, receives the response, and maps it to Swift types.


Basic Execution

apollo.fetch(query: GetCountriesQuery()) { result in
    switch result {
    case .success(let graphQLResult):
        print(graphQLResult.data?.countries)
    case .failure(let error):
        print(error)
    }
}

Result Structure

The result contains:

Example:

if let data = graphQLResult.data {
    print(data.countries)
}

if let errors = graphQLResult.errors {
    print(errors)
}

Using Variables

apollo.fetch(query: GetCountryQuery(code: "PK")) { result in
    switch result {
    case .success(let graphQLResult):
        print(graphQLResult.data?.country?.name)
    case .failure(let error):
        print(error)
    }
}