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 GraphQL queries, we recommend

.

Fetching queries

After defining and generating a query model, as outlined in

, you can fetch your query using ApolloClient.fetch(query:).

For more information on fetching GraphQL operations see

.

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

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!

Previous
Fetching Data
Next
Mutations
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company