Skip to content

Commit dddb8ce

Browse files
committed
Use selectors in buildThunks
1 parent 6ad37da commit dddb8ce

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import type {
5353
StartQueryActionCreatorOptions,
5454
} from './buildInitiate'
5555
import { forceQueryFnSymbol, isUpsertQuery } from './buildInitiate'
56+
import type { AllSelectors } from './buildSelectors'
5657
import type { ApiEndpointQuery, PrefetchOptions } from './module'
5758
import {
5859
createAsyncThunk,
@@ -307,13 +308,15 @@ export function buildThunks<
307308
serializeQueryArgs,
308309
api,
309310
assertTagType,
311+
selectors,
310312
}: {
311313
baseQuery: BaseQuery
312314
reducerPath: ReducerPath
313315
context: ApiContext<Definitions>
314316
serializeQueryArgs: InternalSerializeQueryArgs
315317
api: Api<BaseQuery, Definitions, ReducerPath, any>
316318
assertTagType: AssertTagTypes
319+
selectors: AllSelectors
317320
}) {
318321
type State = RootState<any, string, ReducerPath>
319322

@@ -609,8 +612,10 @@ export function buildThunks<
609612
// Start by looking up the existing InfiniteData value from state,
610613
// falling back to an empty value if it doesn't exist yet
611614
const blankData = { pages: [], pageParams: [] }
612-
const cachedData = getState()[reducerPath].queries[arg.queryCacheKey]
613-
?.data as InfiniteData<unknown, unknown> | undefined
615+
const cachedData = selectors.selectQueryEntry(
616+
getState(),
617+
arg.queryCacheKey,
618+
)?.data as InfiniteData<unknown, unknown> | undefined
614619
// Don't want to use `isForcedQuery` here, because that
615620
// includes `refetchOnMountOrArgChange`.
616621
const existingData = (
@@ -720,9 +725,9 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
720725
arg: QueryThunkArg,
721726
state: RootState<any, string, ReducerPath>,
722727
) {
723-
const requestState = state[reducerPath]?.queries?.[arg.queryCacheKey]
728+
const requestState = selectors.selectQueryEntry(state, arg.queryCacheKey)
724729
const baseFetchOnMountOrArgChange =
725-
state[reducerPath]?.config.refetchOnMountOrArgChange
730+
selectors.selectConfig(state).refetchOnMountOrArgChange
726731

727732
const fulfilledVal = requestState?.fulfilledTimeStamp
728733
const refetchVal =

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ export const coreModule = ({
578578
api,
579579
serializeQueryArgs,
580580
assertTagType,
581+
selectors,
581582
})
582583

583584
const { reducer, actions: sliceActions } = buildSlice({
@@ -616,6 +617,7 @@ export const coreModule = ({
616617
infiniteQueryThunk,
617618
api,
618619
assertTagType,
620+
selectors,
619621
})
620622
safeAssign(api.util, middlewareActions)
621623

0 commit comments

Comments
 (0)