Skip to content

Commit

Permalink
📝 Merge API slice util docs
Browse files Browse the repository at this point in the history
- Merge 'Cache Management Utils' & 'Miscellaneous Utils'
  into 'API Slice Utils'
- Add re-direct from `cache-management-utils` to `api-slice-utils`
  • Loading branch information
Shrugsy committed Oct 29, 2021
1 parent 0ca9b98 commit 55560b2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
---
id: cache-management-utils
title: 'API Slices: Cache Management'
sidebar_label: Cache Management Utils
id: api-slice-utils
title: 'API Slices: Utilities'
sidebar_label: API Slice Utilities
hide_title: true
---

 

# API Slices: Cache Management Utilities
# API Slices: Utilities

The API slice object includes cache management utilities that are used for implementing [optimistic updates](../../usage/manual-cache-updates.mdx#optimistic-updates). These are included in a `util` field inside the slice object.
The API slice object includes various utilities that can be used for cache management,
such as implementing [optimistic updates](../../usage/manual-cache-updates.mdx#optimistic-updates),
as well implementing [server side rendering](../../usage/server-side-rendering.mdx).

These are included in a `util` field inside the slice object.

### `updateQueryData`

Expand Down Expand Up @@ -197,3 +201,51 @@ Note that [hooks](./hooks.mdx) also track state in local component state and mig
```ts no-transpile
dispatch(api.util.resetApiState())
```

## `getRunningOperationPromises`

#### Signature

```ts no-transpile
getRunningOperationPromises: () => Array<Promise<unknown>>
```

#### Description

A function that returns all promises for running queries and mutations.

This is useful for SSR scenarios to await everything triggered in any way, including via hook calls,
or manually dispatching `initiate` actions.

```ts no-transpile title="Awaiting all currently running queries & mutations example"
await Promise.all(api.util.getRunningOperationPromises())
```

## `getRunningOperationPromise`

#### Signature

```ts no-transpile
getRunningOperationPromise: <EndpointName extends QueryKeys<Definitions>>(
endpointName: EndpointName,
args: QueryArgFrom<Definitions[EndpointName]>
) =>
| QueryActionCreatorResult<Definitions[EndpointName]>
| undefined

getRunningOperationPromise: <EndpointName extends MutationKeys<Definitions>>(
endpointName: EndpointName,
fixedCacheKeyOrRequestId: string
) =>
| MutationActionCreatorResult<Definitions[EndpointName]>
| undefined
```
#### Description
A function that returns a single promise for a given endpoint name + argument combination,
if it is currently running. If it is not currently running, the function returns `undefined`.
This is primarily added to add experimental support for suspense in the future.
It enables writing custom hooks that look up if RTK Query has already got a running promise
for a certain endpoint/argument combination, and retrieving that promise to `throw` it.
68 changes: 0 additions & 68 deletions docs/rtk-query/api/created-api/miscellaneous-utils.mdx

This file was deleted.

37 changes: 13 additions & 24 deletions docs/rtk-query/api/created-api/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type Api = {
injectEndpoints: (options: InjectEndpointsOptions) => UpdatedApi
enhanceEndpoints: (options: EnhanceEndpointsOptions) => UpdatedApi

// Cache management utilities
// Utilities
utils: {
updateQueryData: UpdateQueryDataThunk
patchQueryData: PatchQueryDataThunk
Expand All @@ -54,18 +54,16 @@ type Api = {
string
>
resetApiState: SliceActions['resetApiState']
getRunningOperationPromises: () => Array<Promise<unknown>>
getRunningOperationPromise: <EndpointName extends QueryKeys<Definitions>>(
endpointName: EndpointName,
args: QueryArgFrom<Definitions[EndpointName]>
) =>
| QueryActionCreatorResult<Definitions[EndpointName]>
| MutationActionCreatorResult<Definitions[EndpointName]>
| undefined
}

// Miscellaneous utilities
getRunningOperationPromises: () => Array<Promise<unknown>>
getRunningOperationPromise: <EndpointName extends QueryKeys<Definitions>>(
endpointName: EndpointName,
args: QueryArgFrom<Definitions[EndpointName]>
) =>
| QueryActionCreatorResult<Definitions[EndpointName]>
| MutationActionCreatorResult<Definitions[EndpointName]>
| undefined

// Internal actions
internalActions: InternalActions

Expand Down Expand Up @@ -110,25 +108,16 @@ Each API slice object has `injectEndpoints` and `enhanceEndpoints` functions to
:::
## Cache Management Utilities
## API Slice Utilities
The `util` field includes various utility functions that can be used to manage the cache, including
manually updating query cache data, triggering pre-fetching of data, manually invalidating tags,
and manually resetting the api state.
:::info API Reference
- [API Slices: Cache Management Utilities](./cache-management-utils.mdx)
:::
## Miscellaneous Utilities
Miscellaneous utilities are provided that can be used in various scenarios, including SSR.
and manually resetting the api state, as well as other utility functions that can be used in
various scenarios, including SSR.
:::info API Reference
- [API Slices: Miscellaneous Utilities](./miscellaneous-utils.mdx)
- [API Slices: Utilities](./api-slice-utils.mdx)
:::
Expand Down
2 changes: 1 addition & 1 deletion docs/rtk-query/usage/manual-cache-updates.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ unless you encounter the need to do so.

However, in some cases, you may want to update the cache manually. When you wish to update cache
data that _already exists_ for query endpoints, you can do so using the
[`updateQueryData`](../api/created-api/cache-management-utils.mdx#updatequerydata) thunk action
[`updateQueryData`](../api/created-api/api-slice-utils.mdx#updatequerydata) thunk action
available on the `util` object of your created API.

Anywhere you have access to the `dispatch` method for the store instance, you can dispatch the
Expand Down
1 change: 1 addition & 0 deletions website/_redirects
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@

# Relocated content
/rtk-query/usage/optimistic-updates /rtk-query/usage/manual-cache-updates#optimistic-updates
/rtk-query/api/created-api/cache-management-utils /rtk-query/api/created-api/api-slice-utils
3 changes: 1 addition & 2 deletions website/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@
"rtk-query/api/created-api/redux-integration",
"rtk-query/api/created-api/endpoints",
"rtk-query/api/created-api/code-splitting",
"rtk-query/api/created-api/cache-management-utils",
"rtk-query/api/created-api/miscellaneous-utils",
"rtk-query/api/created-api/api-slice-utils",
"rtk-query/api/created-api/hooks"
]
}
Expand Down

0 comments on commit 55560b2

Please sign in to comment.