Skip to content

Commit cda3258

Browse files
authored
Merge pull request #1573 from Shrugsy/docs/document-arg-in-transformResponse
2 parents 43bba18 + 8359318 commit cda3258

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ export type QueryDefinition<
143143
/* transformResponse only available with `query`, not `queryFn` */
144144
transformResponse?(
145145
baseQueryReturnValue: BaseQueryResult<BaseQuery>,
146-
meta: BaseQueryMeta<BaseQuery>
146+
meta: BaseQueryMeta<BaseQuery>,
147+
arg: QueryArg
147148
): ResultType | Promise<ResultType>
148149

149150
extraOptions?: BaseQueryExtraOptions<BaseQuery>
@@ -210,7 +211,8 @@ export type MutationDefinition<
210211
/* transformResponse only available with `query`, not `queryFn` */
211212
transformResponse?(
212213
baseQueryReturnValue: BaseQueryResult<BaseQuery>,
213-
meta: BaseQueryMeta<BaseQuery>
214+
meta: BaseQueryMeta<BaseQuery>,
215+
arg: QueryArg
214216
): ResultType | Promise<ResultType>
215217

216218
extraOptions?: BaseQueryExtraOptions<BaseQuery>
@@ -405,7 +407,8 @@ In some cases, you may want to manipulate the data returned from a query before
405407
See also [Customizing query responses with `transformResponse`](../usage/customizing-queries.mdx#customizing-query-responses-with-transformresponse)
406408

407409
```ts title="Unpack a deeply nested collection" no-transpile
408-
transformResponse: (response) => response.some.deeply.nested.collection
410+
transformResponse: (response, meta, arg) =>
411+
response.some.deeply.nested.collection
409412
```
410413

411414
### `extraOptions`

docs/rtk-query/usage/customizing-queries.mdx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,48 @@ Individual endpoints on [`createApi`](../api/createApi.mdx) accept a [`transform
103103
By default, the payload from the server is returned directly.
104104

105105
```ts
106-
function defaultTransformResponse(baseQueryReturnValue: unknown) {
106+
function defaultTransformResponse(
107+
baseQueryReturnValue: unknown,
108+
meta: unknown,
109+
arg: unknown
110+
) {
107111
return baseQueryReturnValue
108112
}
109113
```
110114

111115
To change it, provide a function that looks like:
112116

113117
```ts title="Unpack a deeply nested collection" no-transpile
114-
transformResponse: (response) => response.some.deeply.nested.collection
118+
transformResponse: (response, meta, arg) =>
119+
response.some.deeply.nested.collection
115120
```
116121

117-
`transformResponse` is also called with the `meta` property returned from the `baseQuery`, which can be used while determining the transformed response. The value for `meta` is dependent on the `baseQuery` used.
122+
`transformResponse` is called with the `meta` property returned from the `baseQuery` as it's second
123+
argument, which can be used while determining the transformed response. The value for `meta` is
124+
dependent on the `baseQuery` used.
118125

119126
```ts title="transformResponse meta example" no-transpile
120-
transformResponse: (response: { sideA: Tracks; sideB: Tracks }, meta) => {
127+
transformResponse: (response: { sideA: Tracks; sideB: Tracks }, meta, arg) => {
121128
if (meta?.coinFlip === 'heads') {
122129
return response.sideA
123130
}
124131
return response.sideB
125132
}
126133
```
127134

135+
`transformResponse` is called with the `arg` property provided to the endpoint as it's third
136+
argument, which can be used while determining the transformed response. The value for `arg` is
137+
dependent on the `endpoint` used, as well as the argument used when calling the query/mutation.
138+
139+
```ts title="transformResponse arg example" no-transpile
140+
transformResponse: (response: Posts, meta, arg) => {
141+
return {
142+
originalArg: arg,
143+
data: response,
144+
}
145+
}
146+
```
147+
128148
While there is less need to store the response in a [normalized lookup table](https://redux.js.org/recipes/structuring-reducers/normalizing-state-shape) with RTK Query managing caching data, `transformResponse` can be leveraged to do so if desired.
129149

130150
```ts title="Normalize the response data" no-transpile

docs/rtk-query/usage/mutations.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const api = createApi({
5050
body: patch,
5151
}),
5252
// Pick out data and prevent nested properties in a hook or selector
53-
transformResponse: (response: { data: Post }) => response.data,
53+
transformResponse: (response: { data: Post }, meta, arg) => response.data,
5454
invalidatesTags: ['Post'],
5555
// onQueryStarted is useful for optimistic updates
5656
// The 2nd parameter is the destructured `MutationLifecycleApi`

docs/rtk-query/usage/queries.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const api = createApi({
5757
// note: an optional `queryFn` may be used in place of `query`
5858
query: (id) => ({ url: `post/${id}` }),
5959
// Pick out data and prevent nested properties in a hook or selector
60-
transformResponse: (response: { data: Post }) => response.data,
60+
transformResponse: (response: { data: Post }, meta, arg) => response.data,
6161
providesTags: (result, error, id) => [{ type: 'Post', id }],
6262
// The 2nd parameter is the destructured `QueryLifecycleApi`
6363
async onQueryStarted(
@@ -163,7 +163,11 @@ Here is an example of a `PostDetail` component:
163163

164164
```tsx title="Example"
165165
export const PostDetail = ({ id }: { id: string }) => {
166-
const { data: post, isFetching, isLoading } = useGetPostQuery(id, {
166+
const {
167+
data: post,
168+
isFetching,
169+
isLoading,
170+
} = useGetPostQuery(id, {
167171
pollingInterval: 3000,
168172
refetchOnMountOrArgChange: true,
169173
skip: false,

0 commit comments

Comments
 (0)