Skip to content
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

Document changes in #8875 #8878

Merged
merged 3 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions docs/shared/mutation-result.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ The instance of Apollo Client that executed the mutation.

Can be useful for manually executing followup operations or writing data to the cache.

</td>
</tr>

<tr>
<td>

###### `reset`

`() => void`
</td>

<td>

A function that you can call to reset the mutation's result to its initial, uncalled state.

</td>
</tr>
</tbody>
Expand Down
12 changes: 6 additions & 6 deletions docs/source/api/react/hooks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function useLazyQuery<TData = any, TVariables = OperationVariables>(
query: DocumentNode,
options?: LazyQueryHookOptions<TData, TVariables>,
): [
(options?: QueryLazyOptions<TVariables>) => void,
(options?: QueryLazyOptions<TVariables>) => Promise<LazyQueryResult<TData, TVariables>>,
LazyQueryResult<TData, TVariables>
] {}
```
Expand All @@ -166,15 +166,15 @@ function useLazyQuery<TData = any, TVariables = OperationVariables>(

<QueryOptions3 />

### Result
### Result tuple

**Execute function**
**Execute function (first tuple item)**

| Param | Type | Description |
| ---------------- | ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| Execute function | (options?: QueryLazyOptions&lt;TVariables&gt;) => void | Function that can be triggered to execute the suspended query. After being called, `useLazyQuery` behaves just like `useQuery`. |
| Execute function | `(options?: QueryLazyOptions<TVariables>) => Promise<LazyQueryResult<TData, TVariables>>` | Function that can be triggered to execute the suspended query. After being called, `useLazyQuery` behaves just like `useQuery`. The `useLazyQuery` function returns a promise that fulfills with a query result when the query succeeds or fails. |

**Result object**
**`LazyQueryResult<TData, TVariables>` object (second tuple item)**

<QueryResult3 />

Expand Down Expand Up @@ -242,7 +242,7 @@ function useMutation<TData = any, TVariables = OperationVariables>(

<MutationOptions3 />

### Result
### `MutationTuple<TData, TVariables>` result tuple

<MutationResult3 />

Expand Down
35 changes: 35 additions & 0 deletions docs/source/data/mutations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,41 @@ if (error) return `Submission error! ${error.message}`;

> The `useMutation` hook also supports `onCompleted` and `onError` options if you prefer to use callbacks. [See the API reference.](../api/react/hooks/#options-2)

### Resetting mutation status

The mutation result object returned by `useMutation` includes a [`reset` function](#reset):

```js
const [login, { data, loading, error, reset }] = useMutation(LOGIN_MUTATION);
```

Call `reset` to reset the mutation's result to its initial state (i.e., _before_ the mutate function was called). You can use this to enable users to dismiss mutation result data or errors in the UI.

> Calling `reset` does _not_ remove any cached data returned by the mutation's execution. It only affects the state associated with the `useMutation` hook, causing the corresponding component to rerender.

```js{2,15}
function LoginPage () {
const [login, { error, reset }] = useMutation(LOGIN_MUTATION);

return (
<>
<form>
<input class="login"/>
<input class="password"/>
<button onclick={login}>Login</button>
</form>
{
error &&
<LoginFailedMessageWindow
message={error.message}
onDismiss={() => reset()}
/>
}
</>
);
}
```

## Updating local data

When you execute a mutation, you modify back-end data. Usually, you then want to update your _locally cached_ data to reflect the back-end modification. For example, if you execute a mutation to add an item to your to-do list, you also want that item to appear in your cached copy of the list.
Expand Down