On this page, you can learn how to use Apollo to attach GraphQL query results to your Angular UI. This guide assumes some familiarity with GraphQL itself. You can read about GraphQL queries themselves in detail at graphql.org.
One of our core values is "it's just GraphQL." When using Apollo Client, you don't have to learn anything special about the query syntax, since everything is just standard GraphQL. Anything you can type into the GraphQL query IDE, you can also put into your Apollo Client code.
When we are using a basic query, we can use the
Apollo.watchQuery method in a
very simple way. We simply need to parse our query into a GraphQL document using
gql tag from
For instance, in our example, we want to display a list of posts in
watchQuery method returns a
QueryRef object which has the
property that is an
We can see that the result object contains
loading, a Boolean indicating if
the query is "in-flight." The observable will only emit once when the query is
loading will be set to false unless you set the
notifyOnNetworkStatusChange to true. Once
the query has completed, it will also contain a
data object with
posts, the field we've picked out in
It's also possible to fetch data only once. The
query method of
service returns an
Observable that also resolves with the same result as
Imagine you have two views (routes), one of them has the
Posts component. When you switch between views, you'll notice that the list of posts loads instantly the second time. This is the Apollo cache at work!
What is QueryRef
As you know,
Apollo.query method returns an Observable that emits a result,
Apollo.watchQuery also does the same, except it can emit multiple
results. (The GraphQL query itself is still only sent once, but the
observable can also update if, for example, another query causes the object to
be updated within Apollo Client's global cache.)
So why doesn't
Apollo.watchQuery expose an Observable?
Apollo service and ApolloClient share pretty much the same API. It makes things easy to understand and use. No reason to change it.
ApolloClient.watchQuery returns an Observable, but not a standard one, it
contains many useful methods (like
refetch()) to manipulate the watched query.
A normal Observable, has only one method,
To use that Apollo's Observable in RxJS, we would have to drop those methods. Since they are necessary to use Apollo to its full potential, we had to come up with a solution.
This is why we created
The API of
QueryRef is very simple. It has the same methods as the Apollo
Observable we talked about. To subscribe to query results, you have to access its
valueChanges property which exposes a clean RxJS Observable.
It's worth mentioning that
QueryRef accepts two generic types.
query methods expect one argument, an object with options. If
you want to configure the query, you can provide any available option in the
same object where the
query key lives.
If your query takes variables, this is the place to pass them in:
Using with AsyncPipe
In Angular, the simplest way of displaying data that comes from Observable is to
AsyncPipe on top of the property inside the UI. You can also achieve this
Note: Using async pipe more than once in your template will trigger the query for each pipe. To avoid this situation, subscribe to the data in the component, and display the data from the component's property.
An Observable returned by
watchQuery().valueChanges holds the actual result
data field, so you can not directly access one of the properties of
The result of the query has this structure:
Without using the
map operator, you would get the whole object instead of only the
Updating cached query results
Caching query results is handy and easy to do, but sometimes you want to make sure that cached data is up to date with your server. Apollo Client supports two strategies for this: polling and refetching.
Polling provides near-real-time synchronization with your server by causing a query to execute periodically at a specified interval. To enable polling for a query, pass a
pollInterval configuration option to the
Apollo.watchQuery with an interval in milliseconds:
By setting the
500, you'll fetch the list of posts from the server every 0.5 seconds. Note that if you set
0, the query will not poll.
You can also start and stop polling dynamically with the
stopPollingfunctions that are available in the
QueryRefobject returned by
Refetching enables you to refresh query results in response to a particular user action, as opposed to using a fixed interval.
Let's add a button to our
Posts component that calls our query's refetch function whenever it's clicked.
You can optionally provide a new
variablesobject to the refetch function. If you don't (as is the case in the following example), the query uses the same variables that it used in its previous execution.
refresh method and notice that the UI updates with a new dog photo. Refetching is an excellent way to guarantee fresh data, but it introduces some complexity with loading state. In the next section, we'll cover strategies for handling complex loading and error state.
Inspecting error states
You can customize your query error handling by providing the
errorPolicy configuration option to
Apollo.query. The default value is
none, which tells Apollo Angular to treat all GraphQL errors as runtime errors. In this case, Apollo Angular discards any query response data returned by the server and sets the error property in the result object to true.
If you set
all, Apollo Angular does not discard query response data, allowing you to render partial results.