Skip to content

Commit b71222d

Browse files
committed
Consistently use arg name
1 parent 5211b18 commit b71222d

File tree

3 files changed

+42
-47
lines changed

3 files changed

+42
-47
lines changed

docs/rtk-query/api/created-api/api-slice-utils.mdx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ Some of the TS types on this page are pseudocode to illustrate intent, as the ac
2323

2424
### `updateQueryData`
2525

26+
A Redux thunk action creator that, when dispatched, creates and applies a set of JSON diff/patch objects to the current state. This immediately updates the Redux state with those changes.
27+
2628
#### Signature
2729

2830
```ts no-transpile
2931
const updateQueryData = (
3032
endpointName: string,
31-
args: any,
33+
arg: any,
3234
updateRecipe: (draft: Draft<CachedState>) => void,
3335
updateProvided?: boolean,
3436
) => ThunkAction<PatchCollection, PartialState, any, AnyAction>
@@ -42,21 +44,19 @@ interface PatchCollection {
4244

4345
- **Parameters**
4446
- `endpointName`: a string matching an existing endpoint name
45-
- `args`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
47+
- `arg`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
4648
- `updateRecipe`: an Immer `produce` callback that can apply changes to the cached state
4749
- `updateProvided`: a boolean indicating whether the endpoint's provided tags should be re-calculated based on the updated cache. Defaults to `false`.
4850

4951
#### Description
5052

51-
A Redux thunk action creator that, when dispatched, creates and applies a set of JSON diff/patch objects to the current state. This immediately updates the Redux state with those changes.
52-
5353
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a callback function. The callback receives an Immer-wrapped `draft` of the current state, and may modify the draft to match the expected results after the mutation completes successfully.
5454

5555
The thunk returns an object containing `{patches: Patch[], inversePatches: Patch[], undo: () => void}`. The `patches` and `inversePatches` are generated using Immer's [`produceWithPatches` method](https://immerjs.github.io/immer/patches).
5656

57-
This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, args, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
57+
This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, arg, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
5858

59-
Note that the first two arguments (`endpointName` and `args`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
59+
Note that the first two arguments (`endpointName` and `arg`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
6060

6161
#### Example 1
6262

@@ -69,7 +69,7 @@ const patchCollection = dispatch(
6969
```
7070

7171
In the example above, `'getPosts'` is provided for the `endpointName`, and `undefined` is provided
72-
for `args`. This will match a query cache key of `'getPosts(undefined)'`.
72+
for `arg`. This will match a query cache key of `'getPosts(undefined)'`.
7373

7474
i.e. it will match a cache entry that may have been created via any of the following calls:
7575

@@ -96,7 +96,7 @@ const patchCollection = dispatch(
9696
```
9797

9898
In the example above, `'getPostById'` is provided for the `endpointName`, and `1` is provided
99-
for `args`. This will match a query cache key of `'getPostById(1)'`.
99+
for `arg`. This will match a query cache key of `'getPostById(1)'`.
100100

101101
i.e. it will match a cache entry that may have been created via any of the following calls:
102102

@@ -114,27 +114,27 @@ dispatch(api.endpoints.getPostById.initiate(1, { ...options }))
114114

115115
### `upsertQueryData`
116116

117+
A Redux thunk action creator that, when dispatched, acts as an artificial API request to upsert a value into the cache.
118+
117119
#### Signature
118120

119121
```ts no-transpile
120-
const upsertQueryData = <T>(endpointName: string, args: any, newEntryData: T) =>
122+
const upsertQueryData = <T>(endpointName: string, arg: any, newEntryData: T) =>
121123
ThunkAction<Promise<CacheEntry<T>>, PartialState, any, UnknownAction>
122124
```
123125

124126
- **Parameters**
125127
- `endpointName`: a string matching an existing endpoint name
126-
- `args`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
128+
- `arg`: an argument matching that used for a previous query call, used to determine which cached dataset needs to be updated
127129
- `newEntryValue`: the value to be written into the corresponding cache entry's `data` field
128130

129131
#### Description
130132

131-
A Redux thunk action creator that, when dispatched, acts as an artificial API request to upsert a value into the cache.
132-
133133
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), the appropriate query arg values to construct the desired cache key, and the data to upsert.
134134

135135
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.
136136

137-
The thunk executes _asynchronously_, and returns a promise that resolves when the store has been updated.
137+
The thunk executes _asynchronously_, and returns a promise that resolves when the store has been updated. This includes executing the `transformResponse` callback if defined for that endpoint.
138138

139139
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.
140140

@@ -148,27 +148,27 @@ await dispatch(
148148

149149
### `patchQueryData`
150150

151+
A Redux thunk action creator that, when dispatched, applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
152+
151153
#### Signature
152154

153155
```ts no-transpile
154156
const patchQueryData = (
155157
endpointName: string,
156-
args: any
158+
arg: any
157159
patches: Patch[],
158160
updateProvided?: boolean
159161
) => ThunkAction<void, PartialState, any, UnknownAction>;
160162
```
161163

162164
- **Parameters**
163165
- `endpointName`: a string matching an existing endpoint name
164-
- `args`: a cache key, used to determine which cached dataset needs to be updated
166+
- `arg`: a cache key, used to determine which cached dataset needs to be updated
165167
- `patches`: an array of patches (or inverse patches) to apply to cached state. These would typically be obtained from the result of dispatching [`updateQueryData`](#updatequerydata)
166168
- `updateProvided`: a boolean indicating whether the endpoint's provided tags should be re-calculated based on the updated cache. Defaults to `false`.
167169

168170
#### Description
169171

170-
A Redux thunk action creator that, when dispatched, applies a JSON diff/patch array to the cached data for a given query result. This immediately updates the Redux state with those changes.
171-
172172
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), the appropriate query arg values to construct the desired cache key, and a JSON diff/patch array as produced by Immer's `produceWithPatches`.
173173

174174
This is typically used as the second step in implementing optimistic updates. If a request fails, the optimistically-applied changes can be reverted by dispatching `patchQueryData` with the `inversePatches` that were generated by `updateQueryData` earlier.
@@ -279,6 +279,8 @@ const api = createApi({
279279

280280
### `prefetch`
281281

282+
A Redux thunk action creator that can be used to manually trigger pre-fetching of data.
283+
282284
#### Signature
283285

284286
```ts no-transpile
@@ -298,8 +300,6 @@ const prefetch = (endpointName: string, arg: any, options: PrefetchOptions) =>
298300

299301
#### Description
300302

301-
A Redux thunk action creator that can be used to manually trigger pre-fetching of data.
302-
303303
The thunk action creator accepts three arguments: the name of the endpoint we are updating (such as `'getPost'`), any relevant query arguments, and a set of options used to determine if the data actually should be re-fetched based on cache staleness.
304304

305305
React Hooks users will most likely never need to use this directly, as the `usePrefetch` hook will dispatch the thunk action creator result internally as needed when you call the prefetching function supplied by the hook.
@@ -312,6 +312,8 @@ dispatch(api.util.prefetch('getPosts', undefined, { force: true }))
312312

313313
### `selectInvalidatedBy`
314314

315+
A selector function that can select query parameters to be invalidated.
316+
315317
#### Signature
316318

317319
```ts no-transpile
@@ -334,8 +336,6 @@ function selectInvalidatedBy(
334336

335337
#### Description
336338

337-
A function that can select query parameters to be invalidated.
338-
339339
The function accepts two arguments
340340

341341
- the root state and
@@ -360,6 +360,8 @@ const entries = api.util.selectInvalidatedBy(state, [
360360

361361
### `invalidateTags`
362362

363+
A Redux action creator that can be used to manually invalidate cache tags for [automated re-fetching](../../usage/automated-refetching.mdx).
364+
363365
#### Signature
364366

365367
```ts no-transpile
@@ -379,8 +381,6 @@ const invalidateTags = (
379381

380382
#### Description
381383

382-
A Redux action creator that can be used to manually invalidate cache tags for [automated re-fetching](../../usage/automated-refetching.mdx).
383-
384384
The action creator accepts one argument: the cache tags to be invalidated. It returns an action with those tags as a payload, and the corresponding `invalidateTags` action type for the api.
385385

386386
Dispatching the result of this action creator will [invalidate](../../usage/automated-refetching.mdx#invalidating-cache-data) the given tags, causing queries to automatically re-fetch if they are subscribed to cache data that [provides](../../usage/automated-refetching.mdx#providing-cache-data) the corresponding tags.
@@ -400,6 +400,8 @@ dispatch(
400400

401401
### `selectCachedArgsForQuery`
402402

403+
A selector function that can select arguments for currently cached queries.
404+
403405
#### Signature
404406

405407
```ts no-transpile
@@ -415,8 +417,6 @@ function selectCachedArgsForQuery(
415417

416418
#### Description
417419

418-
A function that can select arguments for currently cached queries.
419-
420420
The function accepts two arguments
421421

422422
- the root state and

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export type PatchQueryDataThunk<
156156
PartialState,
157157
> = <EndpointName extends QueryKeys<Definitions>>(
158158
endpointName: EndpointName,
159-
args: QueryArgFrom<Definitions[EndpointName]>,
159+
arg: QueryArgFrom<Definitions[EndpointName]>,
160160
patches: readonly Patch[],
161161
updateProvided?: boolean,
162162
) => ThunkAction<void, PartialState, any, UnknownAction>
@@ -166,7 +166,7 @@ export type UpdateQueryDataThunk<
166166
PartialState,
167167
> = <EndpointName extends QueryKeys<Definitions>>(
168168
endpointName: EndpointName,
169-
args: QueryArgFrom<Definitions[EndpointName]>,
169+
arg: QueryArgFrom<Definitions[EndpointName]>,
170170
updateRecipe: Recipe<ResultTypeFrom<Definitions[EndpointName]>>,
171171
updateProvided?: boolean,
172172
) => ThunkAction<PatchCollection, PartialState, any, UnknownAction>
@@ -176,7 +176,7 @@ export type UpsertQueryDataThunk<
176176
PartialState,
177177
> = <EndpointName extends QueryKeys<Definitions>>(
178178
endpointName: EndpointName,
179-
args: QueryArgFrom<Definitions[EndpointName]>,
179+
arg: QueryArgFrom<Definitions[EndpointName]>,
180180
value: ResultTypeFrom<Definitions[EndpointName]>,
181181
) => ThunkAction<
182182
QueryActionCreatorResult<
@@ -229,11 +229,11 @@ export function buildThunks<
229229
type State = RootState<any, string, ReducerPath>
230230

231231
const patchQueryData: PatchQueryDataThunk<EndpointDefinitions, State> =
232-
(endpointName, args, patches, updateProvided) => (dispatch, getState) => {
232+
(endpointName, arg, patches, updateProvided) => (dispatch, getState) => {
233233
const endpointDefinition = endpointDefinitions[endpointName]
234234

235235
const queryCacheKey = serializeQueryArgs({
236-
queryArgs: args,
236+
queryArgs: arg,
237237
endpointDefinition,
238238
endpointName,
239239
})
@@ -246,7 +246,7 @@ export function buildThunks<
246246
return
247247
}
248248

249-
const newValue = api.endpoints[endpointName].select(args)(
249+
const newValue = api.endpoints[endpointName].select(arg)(
250250
// Work around TS 4.1 mismatch
251251
getState() as RootState<any, any, any>,
252252
)
@@ -255,7 +255,7 @@ export function buildThunks<
255255
endpointDefinition.providesTags,
256256
newValue.data,
257257
undefined,
258-
args,
258+
arg,
259259
{},
260260
assertTagType,
261261
)
@@ -266,11 +266,11 @@ export function buildThunks<
266266
}
267267

268268
const updateQueryData: UpdateQueryDataThunk<EndpointDefinitions, State> =
269-
(endpointName, args, updateRecipe, updateProvided = true) =>
269+
(endpointName, arg, updateRecipe, updateProvided = true) =>
270270
(dispatch, getState) => {
271271
const endpointDefinition = api.endpoints[endpointName]
272272

273-
const currentState = endpointDefinition.select(args)(
273+
const currentState = endpointDefinition.select(arg)(
274274
// Work around TS 4.1 mismatch
275275
getState() as RootState<any, any, any>,
276276
)
@@ -282,7 +282,7 @@ export function buildThunks<
282282
dispatch(
283283
api.util.patchQueryData(
284284
endpointName,
285-
args,
285+
arg,
286286
ret.inversePatches,
287287
updateProvided,
288288
),
@@ -317,26 +317,21 @@ export function buildThunks<
317317
}
318318

319319
dispatch(
320-
api.util.patchQueryData(
321-
endpointName,
322-
args,
323-
ret.patches,
324-
updateProvided,
325-
),
320+
api.util.patchQueryData(endpointName, arg, ret.patches, updateProvided),
326321
)
327322

328323
return ret
329324
}
330325

331326
const upsertQueryData: UpsertQueryDataThunk<Definitions, State> =
332-
(endpointName, args, value) => (dispatch) => {
327+
(endpointName, arg, value) => (dispatch) => {
333328
return dispatch(
334329
(
335330
api.endpoints[endpointName] as ApiEndpointQuery<
336331
QueryDefinition<any, any, any, any, any>,
337332
Definitions
338333
>
339-
).initiate(args, {
334+
).initiate(arg, {
340335
subscribe: false,
341336
forceRefetch: true,
342337
[forceQueryFnSymbol]: () => ({

packages/toolkit/src/query/core/module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export interface ApiModules<
150150
util: {
151151
/**
152152
* A thunk that (if dispatched) will return a specific running query, identified
153-
* by `endpointName` and `args`.
153+
* by `endpointName` and `arg`.
154154
* If that query is not running, dispatching the thunk will result in `undefined`.
155155
*
156156
* Can be used to await a specific query triggered in any way,
@@ -160,7 +160,7 @@ export interface ApiModules<
160160
*/
161161
getRunningQueryThunk<EndpointName extends QueryKeys<Definitions>>(
162162
endpointName: EndpointName,
163-
args: QueryArgFrom<Definitions[EndpointName]>,
163+
arg: QueryArgFrom<Definitions[EndpointName]>,
164164
): ThunkWithReturnValue<
165165
| QueryActionCreatorResult<
166166
Definitions[EndpointName] & { type: 'query' }
@@ -237,9 +237,9 @@ export interface ApiModules<
237237
*
238238
* The thunk executes _synchronously_, and returns an object containing `{patches: Patch[], inversePatches: Patch[], undo: () => void}`. The `patches` and `inversePatches` are generated using Immer's [`produceWithPatches` method](https://immerjs.github.io/immer/patches).
239239
*
240-
* This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, args, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
240+
* This is typically used as the first step in implementing optimistic updates. The generated `inversePatches` can be used to revert the updates by calling `dispatch(patchQueryData(endpointName, arg, inversePatches))`. Alternatively, the `undo` method can be called directly to achieve the same effect.
241241
*
242-
* Note that the first two arguments (`endpointName` and `args`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
242+
* Note that the first two arguments (`endpointName` and `arg`) are used to determine which existing cache entry to update. If no existing cache entry is found, the `updateRecipe` callback will not run.
243243
*
244244
* @example
245245
*

0 commit comments

Comments
 (0)