Queries
Using React Hooks
Queries work just like in JavaScript. You can declaratively fetch data using the useQuery
hook.
Basics
Create a query module with graphql-ppx
. We'll use this throughout the examples on this page.
To execute the query, just call the use
hook in a component like so.
Typical Usage
While we wish it was always as easy as above, there are many situations where you may be fetching new data while rendering a cached results, etc. Here's a slightly more complex example:
Lazy Fetching of Data
Live Queries
It's possible to update a query by attaching a subscription. Please see the subscriptions section on how to do this.
Directly Via the Apollo Client
There are many instances where you might want to query outside the context of React. You can use the query
method directly on the client to do this.
If you need to react to changes in some data in the cache, not just a one-off fetch, you can use the watchQuery
method.