Executing queries in Apollo iOS involves sending a GraphQL query to the server using ApolloClient and handling the typed response asynchronously.
Queries are executed using generated query objects (from .graphql files).
Apollo sends the request, receives the response, and maps it to Swift types.
apollo.fetch(query: GetCountriesQuery()) { result in
switch result {
case .success(let graphQLResult):
print(graphQLResult.data?.countries)
case .failure(let error):
print(error)
}
}
The result contains:
data → actual response dataerrors → GraphQL errors (if any)Example:
if let data = graphQLResult.data {
print(data.countries)
}
if let errors = graphQLResult.errors {
print(errors)
}
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)
}
}