Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/source/data/queries.mdx

Copy link
Copy Markdown
Member Author

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 dataState in the examples until we have that completely flushed out.

Original file line number Diff line number Diff line change
Expand Up @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L253

Headings in tutorial sections should use imperative verbs for a more direct, action-oriented tone. ```suggestion ## Fetch data in response to user interaction ```

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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L257

Use the imperative mood for a more direct and authoritative tone. Use contractions for better readability. ```suggestion Use the [<code>useLazyQuery</code>](../api/react/useLazyQuery) hook to manually execute queries. Unlike <code>useQuery</code>, <code>useLazyQuery</code> doesn't immediately execute its associated query. Instead, it returns an execution function that you call whenever you need to execute the query. ```

Here's an example:

Expand All @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L282

Use the word "array" instead of "tuple" for simplicity and approachability. ```suggestion The first item in <code>useLazyQuery</code>'s returned array is the execution function, and the second item is an object that contains information about the executed query, such as the <code>loading</code>, <code>error</code>, <code>data</code>, and <code>dataState</code> properties. ```

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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L286

Use contractions to improve readability and create a more approachable tone. ```suggestion Unlike <code>useQuery</code>, options provided to <code>useLazyQuery</code> that change on re-renders don't automatically execute the query. Instead, <code>useLazyQuery</code> waits to execute the query using the updated options until the execution function is called again. ```

The following is an example that changes the fetch policy depending on whether the user is online or offline:

Expand All @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L309

Use contractions and the present tense for conciseness and clarity. ```suggestion The changed options are immediately applied to the underlying <code>ObservableQuery</code> (accessible by the <code>observable</code> property) even though the query isn't executed. Inspecting the options on the <code>observable</code> returns the updated options. This means the updated options are used for other APIs (such as <code>refetch</code>), even before calling the execution function again. ```

</Note>

Expand All @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L317

Use bold formatting for the names of clickable UI elements like buttons. ```suggestion The following is an example that gets a specific dog's photo when clicking the **Get photo** button: ```

```jsx
function DogPhoto() {
Expand All @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L339

The sentence is rephrased for better clarity and to avoid repetition. ```suggestion When using TypeScript with a query that has required variables, you must provide those variables in the <code>options</code> object passed to the execution function. If the <code>options</code> argument isn't provided, or if the <code>variables</code> property is missing required variables, TypeScript shows an error. ```

</Note>

Expand All @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L347

Use bold formatting for the names of clickable UI elements like buttons. ```suggestion The following is an example that gets the selected dog's photo when clicking the **Get photo** button. ```

```jsx
function DogPhoto() {
Expand Down Expand Up @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L430

The sentence is rephrased for better clarity and flow. ```suggestion In cases where you don't need to keep your component in sync with query state, use <code>client.query()</code> directly. This approach avoids unnecessary component re-renders for data you don't use. ```

Using `useLazyQuery` in those situations should be considered an antipattern.

Check warning on line 432 in docs/source/data/queries.mdx

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L432

Use a more direct and authoritative tone. Avoid passive phrasing like "should be considered". ```suggestion Using <code>useLazyQuery</code> in these situations is an antipattern. ```

<ExpansionPanel title="Example">
Comment thread
jerelmiller marked this conversation as resolved.

Expand Down Expand Up @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L521

Use active voice ('occurs' instead of 'is raised') and prefer using 'Apollo' over 'we' for clarity. ```suggestion <code>data</code> might be <code>undefined</code> if a [network error](./error-handling#network-errors) occurs. Apollo recommends checking if <code>data</code> is <code>undefined</code> before you attempt to use it, in case an error occurs during query execution. ```

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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L531

Placing 'However' at the beginning of the clause improves sentence flow. ```suggestion Apollo Client ensures the rejected promise doesn't throw an unhandled rejection error when you don't add a rejection handler to the promise. However, this 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. ```

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not completely correct - calling promise.then(onlyAResolvedHandler) will also cause an error to be thrown, not only promise.then(resolvedHandler, rejectionHandler) or promise.catch(rejectionHandler)


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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L535

Use active voice ('you start' instead of 'is started') for a more direct and reader-centric tone. ```suggestion The promise returned by the execution function includes a <code>.retain()</code> method. When called, it ensures the query continues running even if the component unmounts or you start a new query before the last one finishes. ```

```jsx
function GetDogs() {
Expand All @@ -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

View check run for this annotation

Apollo Librarian / AI Style Review

docs/source/data/queries.mdx#L556

Use active voice ('You can shorten' instead of 'can be shortened') for a more direct and reader-centric tone. ```suggestion The <code>retain()</code> method returns the original promise. You can shorten the previous example to a single line: ```

```ts
const { data } = await getDogs().retain();
Expand Down
Loading