Docs
Try Apollo Studio

Queries


Queries are the most commonly used GraphQL operations. A query is used to fetch data from a GraphQL server. Apollo iOS allows you to fetch a query operation from a server using a type-safe, generated query model. In Apollo iOS, queries can also be watched for local changes to their data, allowing you to react to changes.

For more information about GraphQL queries, we recommend reading this guide.

Fetching queries

After defining and generating a query model, as outlined in "Defining operations", you can fetch your query using ApolloClient.fetch(query:).

For more information on fetching GraphQL operations see "Fetching Data".

Watching queries

Queries can also be watched with ApolloClient.watch(query:). Watching a query is very similar to fetching a query. The main difference is that you don't just receive an initial result, but your result handler will be invoked whenever relevant data in the cache changes:

let watcher = apollo.watch(query: HeroNameQuery()) { result in
guard let data = try? result.get().data else { return }
print(data.hero?.name) // Luke Skywalker
}

If any of the data for the query changes in the local cache, the result handler will be invoked again.

When you call ApolloClient.watch(query:) a GraphQLQueryWatcher is returned. Changes to the query's data will be watched until cancel() is called on the watcher.

NOTE: Remember to call cancel() on a watcher when its parent object is deallocated, or you will get a memory leak!

Edit on GitHub
Previous
Fetching Data
Next
Mutations