Queries

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.

Basic Queries

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
the graphql-tag library.

For instance, in GitHunt, we want to display the current user (if logged in) in
the Profile component:

The watchQuery method returns a QueryRef object which has the valueChanges
property that is an Observable.

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
complete, and loading will be set to false unless you set the watchQuery
parameters notifyOnNetworkStatusChange to true. Once
the query has completed, it will also contain a data object with
currentUser, the field we've picked out in CurrentUserForProfile.

We can expect the data.currentUser to change as the logged-in-ness of the
client and what it knows about the current user changes over time. That
information is stored in Apollo Client's global cache, so if some other query
fetches new information about the current user, this component will update to
remain consistent.

It's also possible to fetch data only once. The query method of Apollo
service returns an Observable that also resolves with the same result as
above.

What is QueryRef

As you know, Apollo.query method returns an Observable that emits a result,
just once. Apollo.watchQuery also does the same, except it can emit multiple
results. (The GraphQL query itself is still only sent once, but the watchQuery
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.

In 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, subscribe().

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 QueryRef.

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. More about that in
Static Typing.

Providing options

watchQuery and 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.

Using with AsyncPipe

In Angular, the simplest way of displaying data that comes from Observable is to
put AsyncPipe on top of the property inside the UI. You can also achieve this
with Apollo.

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
under the data field, so you can not directly access one of the properties of
that object.

This is why we created SelectPipe. The only argument it receives is the name
of the property you want to get from data.