You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rtk-query/api/created-api/api-slice-utils.mdx
+80Lines changed: 80 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -197,6 +197,86 @@ dispatch(
197
197
patchCollection.undo()
198
198
```
199
199
200
+
### `upsertQueryEntries`
201
+
202
+
A standard Redux action creator that accepts an array of individual cache entry descriptions, and immediately upserts them into the store. This is designed to efficiently bulk-insert many entries at once.
203
+
204
+
#### Signature
205
+
206
+
```ts no-transpile
207
+
/**
208
+
* A typesafe single entry to be upserted into the cache
-`entries`: an array of objects that contain the data needed to upsert individual cache entries:
225
+
-`endpointName`: the name of the endpoint, such as `"getPokemon"`
226
+
-`arg`: the full query key argument needed to identify this cache entry, such as `"pikachu"` (same as you would pass to a `useQuery` hook or `api.endpoints.someEndpoint.select()`)
227
+
-`value`: the data to be upserted into this cache entry, exactly as formatted.
228
+
229
+
#### Description
230
+
231
+
This method is designed as a more efficient approach to bulk-inserting many entries at once than many individual calls to `upsertQueryData`. As a comparison:
232
+
233
+
-`upsertQueryData`:
234
+
- upserts one cache entry at a time
235
+
- Is async
236
+
- Dispatches 2 separate actions, `pending` and `fulfilled`
237
+
- Runs the `transformResponse` callback if defined for that endpoint, as well as the `merge` callback if defined
238
+
-`upsertQueryEntries`:
239
+
- upserts many cache entries at once, and they may be for any combination of endpoints defined in the API
240
+
- Is a single synchronous action
241
+
- Does _not_ run `transformResponse`, so the provided `value` fields must already be in the final format expected for that endpoint. However, it will still run the `merge` callback if defined
242
+
243
+
Currently, this method has two main use cases. The first is prefilling the cache with data retrieved from storage on app startup. The second is to act as a "pseudo-normalization" tool. [RTK Query is _not_ a "normalized" cache](../../usage/cache-behavior.mdx#no-normalized-or-de-duplicated-cache). However, there are times when you may want to prefill other cache entries with the contents of another endpoint, such as taking the results of a `getPosts` list endpoint response and prefilling the individual `getPost(id)` endpoint cache entries.
244
+
245
+
If no cache entry for that cache key exists, a cache entry will be created and the data added. If a cache entry already exists, this will _overwrite_ the existing cache entry data.
246
+
247
+
If dispatched while an actual request is in progress, both the upsert and request will be handled as soon as they resolve, resulting in a "last result wins" update behavior.
0 commit comments