Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

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 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!

Previous
Fetching Data
Next
Mutations
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company