Skip to content

Commit 80b7656

Browse files
authored
Merge pull request #4177 from juliengbt/feature/4149-is-prefetch-query
feat: add isPrefetch property in query action
2 parents fd24f6f + 6ef71d0 commit 80b7656

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ You must add the middleware for RTK-Query to function correctly!`,
301301
forceRefetch,
302302
subscriptionOptions,
303303
[forceQueryFnSymbol]: forceQueryFn,
304-
} = {},
304+
...rest
305+
} = {}
305306
) =>
306307
(dispatch, getState) => {
307308
const queryCacheKey = serializeQueryArgs({
@@ -311,6 +312,7 @@ You must add the middleware for RTK-Query to function correctly!`,
311312
})
312313

313314
const thunk = queryThunk({
315+
...rest,
314316
type: 'query',
315317
subscribe,
316318
forceRefetch: forceRefetch,

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -609,11 +609,12 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
609609
const force = hasTheForce(options) && options.force
610610
const maxAge = hasMaxAge(options) && options.ifOlderThan
611611

612-
const queryAction = (force: boolean = true) =>
613-
(api.endpoints[endpointName] as ApiEndpointQuery<any, any>).initiate(
614-
arg,
615-
{ forceRefetch: force },
616-
)
612+
const queryAction = (force: boolean = true) => {
613+
const options = { forceRefetch: force, isPrefetch: true }
614+
return (
615+
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
616+
).initiate(arg, options)
617+
}
617618
const latestStateValue = (
618619
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
619620
).select(arg)(getState())

packages/toolkit/src/query/tests/buildThunks.test.tsx

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { configureStore } from '@reduxjs/toolkit'
1+
import { configureStore, isAllOf } from '@reduxjs/toolkit'
22
import { createApi } from '@reduxjs/toolkit/query/react'
33
import { renderHook, waitFor } from '@testing-library/react'
4-
import { withProvider } from '../../tests/utils/helpers'
4+
import { actionsReducer, withProvider } from '../../tests/utils/helpers'
55
import type { BaseQueryApi } from '../baseQueryTypes'
66

77
test('handles a non-async baseQuery without error', async () => {
@@ -200,3 +200,52 @@ describe('re-triggering behavior on arg change', () => {
200200
}
201201
})
202202
})
203+
204+
describe('prefetch', () => {
205+
const baseQuery = () => ({ data: null })
206+
const api = createApi({
207+
baseQuery,
208+
endpoints: (build) => ({
209+
getUser: build.query<any, any>({
210+
query: (obj) => obj,
211+
}),
212+
}),
213+
})
214+
215+
const store = configureStore({
216+
reducer: { [api.reducerPath]: api.reducer, ...actionsReducer },
217+
middleware: (gDM) => gDM().concat(api.middleware),
218+
})
219+
it('should attach isPrefetch if prefetching', async () => {
220+
store.dispatch(api.util.prefetch('getUser', 1, {}))
221+
222+
await Promise.all(store.dispatch(api.util.getRunningQueriesThunk()))
223+
224+
const isPrefetch = (
225+
action: any,
226+
): action is { meta: { arg: { isPrefetch: true } } } =>
227+
action?.meta?.arg?.isPrefetch
228+
229+
expect(store.getState().actions).toMatchSequence(
230+
api.internalActions.middlewareRegistered.match,
231+
isAllOf(api.endpoints.getUser.matchPending, isPrefetch),
232+
isAllOf(api.endpoints.getUser.matchFulfilled, isPrefetch),
233+
)
234+
235+
// compare against a regular initiate call
236+
await store.dispatch(
237+
api.endpoints.getUser.initiate(1, { forceRefetch: true }),
238+
)
239+
240+
const isNotPrefetch = (action: any): action is unknown =>
241+
!isPrefetch(action)
242+
243+
expect(store.getState().actions).toMatchSequence(
244+
api.internalActions.middlewareRegistered.match,
245+
isAllOf(api.endpoints.getUser.matchPending, isPrefetch),
246+
isAllOf(api.endpoints.getUser.matchFulfilled, isPrefetch),
247+
isAllOf(api.endpoints.getUser.matchPending, isNotPrefetch),
248+
isAllOf(api.endpoints.getUser.matchFulfilled, isNotPrefetch),
249+
)
250+
})
251+
})

0 commit comments

Comments
 (0)