Docs
Launch GraphOS Studio

Queries


Queries are the most commonly used . A is used to fetch data from a . 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 queries, we recommend reading this guide.

Fetching queries

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

For more information on fetching see "Fetching Data".

Watching queries

Queries can also be watched with ApolloClient.watch(query:). Watching a 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 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 '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!

Previous
Fetching Data
Next
Mutations
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company