Skip to content

Commit 23ab99a

Browse files
committed
Fix updateQueryData for infinite queries
1 parent 78ff764 commit 23ab99a

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ export type QueryKeys<Definitions extends EndpointDefinitions> = {
154154
? K
155155
: never
156156
}[keyof Definitions]
157+
158+
export type InfiniteQueryKeys<Definitions extends EndpointDefinitions> = {
159+
[K in keyof Definitions]: Definitions[K] extends InfiniteQueryDefinition<
160+
any,
161+
any,
162+
any,
163+
any,
164+
any
165+
>
166+
? K
167+
: never
168+
}[keyof Definitions]
169+
157170
export type MutationKeys<Definitions extends EndpointDefinitions> = {
158171
[K in keyof Definitions]: Definitions[K] extends MutationDefinition<
159172
any,

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import type {
1919
AssertTagTypes,
2020
EndpointDefinition,
2121
EndpointDefinitions,
22+
InfiniteQueryArgFrom,
2223
InfiniteQueryDefinition,
2324
MutationDefinition,
25+
PageParamFrom,
2426
QueryArgFrom,
2527
QueryDefinition,
2628
ResultTypeFrom,
@@ -40,6 +42,7 @@ import type {
4042
InfiniteQueryConfigOptions,
4143
QueryCacheKey,
4244
InfiniteQueryDirection,
45+
InfiniteQueryKeys,
4346
} from './apiState'
4447
import { QueryStatus } from './apiState'
4548
import type {
@@ -194,13 +197,39 @@ export type PatchQueryDataThunk<
194197
updateProvided?: boolean,
195198
) => ThunkAction<void, PartialState, any, UnknownAction>
196199

200+
type AllQueryKeys<Definitions extends EndpointDefinitions> =
201+
| QueryKeys<Definitions>
202+
| InfiniteQueryKeys<Definitions>
203+
197204
export type UpdateQueryDataThunk<
198205
Definitions extends EndpointDefinitions,
199206
PartialState,
200-
> = <EndpointName extends QueryKeys<Definitions>>(
207+
> = <EndpointName extends AllQueryKeys<Definitions>>(
201208
endpointName: EndpointName,
202-
arg: QueryArgFrom<Definitions[EndpointName]>,
203-
updateRecipe: Recipe<ResultTypeFrom<Definitions[EndpointName]>>,
209+
// TODO Find a way to deduplicate this ugly conditional type
210+
arg: Definitions[EndpointName] extends InfiniteQueryDefinition<
211+
any,
212+
any,
213+
any,
214+
any,
215+
any
216+
>
217+
? InfiniteQueryArgFrom<Definitions[EndpointName]>
218+
: QueryArgFrom<Definitions[EndpointName]>,
219+
updateRecipe: Recipe<
220+
Definitions[EndpointName] extends InfiniteQueryDefinition<
221+
any,
222+
any,
223+
any,
224+
any,
225+
any
226+
>
227+
? InfiniteData<
228+
ResultTypeFrom<Definitions[EndpointName]>,
229+
PageParamFrom<Definitions[EndpointName]>
230+
>
231+
: ResultTypeFrom<Definitions[EndpointName]>
232+
>,
204233
updateProvided?: boolean,
205234
) => ThunkAction<PatchCollection, PartialState, any, UnknownAction>
206235

packages/toolkit/src/query/tests/infiniteQueries.test.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,14 +523,14 @@ describe('Infinite queries', () => {
523523
)
524524

525525
const res1 = storeRef.store.dispatch(
526-
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}),
526+
pokemonApiWithRefetch.endpoints.getInfinitePokemon.initiate('fire', {}),
527527
)
528528

529529
const entry1InitialLoad = await res1
530530
checkResultData(entry1InitialLoad, [[{ id: '0', name: 'Pokemon 0' }]])
531531

532532
const res2 = storeRef.store.dispatch(
533-
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {
533+
pokemonApiWithRefetch.endpoints.getInfinitePokemon.initiate('fire', {
534534
direction: 'forward',
535535
}),
536536
)
@@ -541,4 +541,32 @@ describe('Infinite queries', () => {
541541
[{ id: '1', name: 'Pokemon 1' }],
542542
])
543543
})
544+
545+
test('Works with cache manipulation utils', async () => {
546+
const res1 = storeRef.store.dispatch(
547+
pokemonApi.endpoints.getInfinitePokemon.initiate('fire', {}),
548+
)
549+
550+
const entry1InitialLoad = await res1
551+
checkResultData(entry1InitialLoad, [[{ id: '0', name: 'Pokemon 0' }]])
552+
553+
storeRef.store.dispatch(
554+
pokemonApi.util.updateQueryData('getInfinitePokemon', 'fire', (draft) => {
555+
draft.pages.push([{ id: '1', name: 'Pokemon 1' }])
556+
draft.pageParams.push(1)
557+
}),
558+
)
559+
560+
const entry1Updated = pokemonApi.endpoints.getInfinitePokemon.select(
561+
'fire',
562+
)(storeRef.store.getState())
563+
564+
expect(entry1Updated.data).toEqual({
565+
pages: [
566+
[{ id: '0', name: 'Pokemon 0' }],
567+
[{ id: '1', name: 'Pokemon 1' }],
568+
],
569+
pageParams: [0, 1],
570+
})
571+
})
544572
})

0 commit comments

Comments
 (0)