-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Misc get started tweaks and more robust useLazyQuery documentation
#12833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
20e4e4f
4a940d9
50548c3
07f9d59
045d547
f1982aa
581d9c4
c4d1850
95b4233
69a2ff4
472e84b
1977dbe
c47e0e2
cbe685a
f965159
2f60fc2
5156b9c
19b1368
b2b37e6
839cffd
bd78991
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,11 +250,11 @@ | |
|
|
||
| For more information, see [Handling operation errors](./error-handling/). | ||
|
|
||
| ## Fetching in response to user interaction | ||
|
Check warning on line 253 in docs/source/data/queries.mdx
|
||
|
|
||
| When React renders a component that calls `useQuery`, Apollo Client automatically executes the corresponding query. But what if you want to execute a query in response to a user interaction, such as a user clicking a button? | ||
|
|
||
| The [`useLazyQuery`](../api/react/useLazyQuery) hook is suited for manually executing queries. Unlike `useQuery`, when you use `useLazyQuery`, it does not immediately execute its associated query. Instead, it returns an execution function that you call whenever you need to execute the query. | ||
|
Check warning on line 257 in docs/source/data/queries.mdx
|
||
|
|
||
| Here's an example: | ||
|
|
||
|
|
@@ -279,11 +279,11 @@ | |
| } | ||
| ``` | ||
|
|
||
| The first item in `useLazyQuery`'s return tuple is the execution function, and the second item is an object that contains information about the executed query, such as the `loading`, `error`, `data`, and `dataState` properties. | ||
|
Check notice on line 282 in docs/source/data/queries.mdx
|
||
|
|
||
| ### Re-rendering with new options | ||
|
|
||
| Unlike `useQuery`, options provided to `useLazyQuery` that change on re-renders do not automatically execute the query. Instead, `useLazyQuery` waits to execute the query using the updated options until the execution function is called again. | ||
|
Check notice on line 286 in docs/source/data/queries.mdx
|
||
|
|
||
| The following is an example that changes the fetch policy depending on whether the user is online or offline: | ||
|
|
||
|
|
@@ -306,7 +306,7 @@ | |
|
|
||
| <Note> | ||
|
|
||
| The changed options are immediately applied to the underlying `ObservableQuery` (accessible by the `observable` property) even though the query is not executed. Inspecting the options on the `observable` returns the updated options. This means the updated options will be used for other APIs (such as `refetch`), even before calling the execution function again. | ||
|
Check warning on line 309 in docs/source/data/queries.mdx
|
||
|
|
||
| </Note> | ||
|
|
||
|
|
@@ -314,7 +314,7 @@ | |
|
|
||
| You provide `variables` to the execution function when executing the query. | ||
|
|
||
| The following is an example that gets a specific dog's photo when clicking the "Get photo" button: | ||
|
Check warning on line 317 in docs/source/data/queries.mdx
|
||
|
|
||
| ```jsx | ||
| function DogPhoto() { | ||
|
|
@@ -336,7 +336,7 @@ | |
|
|
||
| <Note> | ||
|
|
||
| When using TypeScript, the `variables` option is required along with any required variables when the query provided to `useLazyQuery` contains required variables. If the options argument is not provided to the execution function, or the `variables` option is missing required variables, you see a TypeScript error. | ||
|
Check warning on line 339 in docs/source/data/queries.mdx
|
||
|
|
||
| </Note> | ||
|
|
||
|
|
@@ -344,7 +344,7 @@ | |
|
|
||
| You change variables by calling the execution function with updated variables. | ||
|
|
||
| The following is an example that gets the selected dog's photo when clicking the "Get photo" button. | ||
|
Check warning on line 347 in docs/source/data/queries.mdx
|
||
|
|
||
| ```jsx | ||
| function DogPhoto() { | ||
|
|
@@ -427,9 +427,9 @@ | |
|
|
||
| Use the `data`, `error`, and other properties returned by `useLazyQuery` to sync the query state with your component. Avoid using your own state setters from React's `useState` hook with data resolved from the promise. This ensures your component stays up-to-date with cache changes as they occur throughout your application.<br /><br /> | ||
|
|
||
| In cases where you don't need to keep your component in sync with query state, use `client.query()` directly because it won't unnecessarily render your component for data that you don't use. | ||
|
Check notice on line 430 in docs/source/data/queries.mdx
|
||
|
|
||
| Using `useLazyQuery` in those situations should be considered an antipattern. | ||
|
Check warning on line 432 in docs/source/data/queries.mdx
|
||
|
|
||
| <ExpansionPanel title="Example"> | ||
|
jerelmiller marked this conversation as resolved.
|
||
|
|
||
|
|
@@ -518,15 +518,21 @@ | |
|
|
||
| <Tip> | ||
|
|
||
| `data` might be `undefined` in the event a [network error](./error-handling#network-errors) is raised. We recommend checking if `data` is `undefined` before attempting to use it in case an error occurs during query execution. | ||
|
Check warning on line 521 in docs/source/data/queries.mdx
|
||
|
|
||
| </Tip> | ||
|
|
||
| #### Retaining query results | ||
|
|
||
| In-flight queries executed by `useLazyQuery` are aborted when the component unmounts, causing the promise to reject. In some cases, you might find this behavior undesirable and would prefer to let the query run to completion. | ||
| In-flight queries executed by `useLazyQuery` are aborted when the component unmounts or another query is started when calling the execution function, causing the promise to reject. In some cases, you might find this behavior undesirable and would prefer to let the query run to completion. | ||
|
|
||
| <Note> | ||
|
|
||
| Apollo Client ensures the rejected promise doesn't throw an unhandled rejection error when you don't add a rejection handler to the promise. This however means that aborted errors are silent and might go unnoticed. If you want to be notified when the request is aborted, provide a rejection handler for the promise. | ||
|
Check notice on line 531 in docs/source/data/queries.mdx
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not completely correct - calling |
||
|
|
||
| </Note> | ||
|
|
||
| The promise returned by the execution function includes a `.retain()` method. When called, it ensures the query continues running even when the component unmounts or a new query is started before the last one finished. | ||
|
Check warning on line 535 in docs/source/data/queries.mdx
|
||
|
|
||
| ```jsx | ||
| function GetDogs() { | ||
|
|
@@ -547,7 +553,7 @@ | |
| } | ||
| ``` | ||
|
|
||
| The `retain()` method returns the original promise. The previous example can be shortened to a single line: | ||
|
Check warning on line 556 in docs/source/data/queries.mdx
|
||
|
|
||
| ```ts | ||
| const { data } = await getDogs().retain(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I've avoided using
dataStatein the examples until we have that completely flushed out.