Skip to content

Commit 633f85b

Browse files
committed
Forced into the thunk working InfQuery handling for api proof of concept
1 parent 96a60e6 commit 633f85b

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { HandledError } from '../HandledError'
5353

5454
import type { ApiEndpointQuery, PrefetchOptions } from './module'
5555
import type { UnwrapPromise } from '../tsHelpers'
56+
import type { InfiniteQueryDefinition } from '@internal/query/endpointDefinitions'
5657

5758
declare module './module' {
5859
export interface ApiEndpointQuery<
@@ -61,6 +62,12 @@ declare module './module' {
6162
Definitions extends EndpointDefinitions,
6263
> extends Matchers<QueryThunk, Definition> {}
6364

65+
export interface ApiEndpointInfiniteQuery<
66+
Definition extends InfiniteQueryDefinition<any, any, any, any, any>,
67+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
68+
Definitions extends EndpointDefinitions,
69+
> extends Matchers<QueryThunk, Definition> {}
70+
6471
export interface ApiEndpointMutation<
6572
Definition extends MutationDefinition<any, any, any, any, any>,
6673
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -433,8 +440,6 @@ export function buildThunks<
433440
} else if (endpointDefinition.query) {
434441

435442

436-
//TODO: these will come from the hook/initiate/middleware?
437-
let pages: number
438443
const oldPages: any[] = []
439444
const oldPageParams: any[] = []
440445

@@ -452,7 +457,7 @@ export function buildThunks<
452457

453458

454459
const page = await baseQuery(
455-
endpointDefinition.query(arg.originalArgs),
460+
endpointDefinition.query(param),
456461
baseQueryApi,
457462
endpointDefinition.extraOptions as any
458463
)
@@ -469,7 +474,7 @@ export function buildThunks<
469474
}
470475

471476
if ('infiniteQueryOptions' in endpointDefinition) {
472-
if (arg.direction && arg.data.pages.length) {
477+
if ('direction' in arg && arg.direction && arg.data.pages.length) {
473478

474479
const previous = arg.direction === 'backwards'
475480
const pageParamFn = previous ? getPreviousPageParam : getNextPageParam
@@ -490,6 +495,7 @@ export function buildThunks<
490495

491496
// Fetch remaining pages
492497
for (let i = 1; i < remainingPages; i++) {
498+
// @ts-ignore
493499
const param = getNextPageParam(arg.infiniteQueryOptions, result.data as InfiniteData<unknown>)
494500
result = await fetchPage(result.data as InfiniteData<unknown>, param)
495501
}
@@ -720,8 +726,8 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
720726
InfiniteQueryThunkArg,
721727
ThunkApiMetaConfig & { state: RootState<any, string, ReducerPath> }
722728
>(`${reducerPath}/executeQuery`, executeEndpoint, {
723-
getPendingMeta() {
724-
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true }
729+
getPendingMeta(queryThunkArgs) {
730+
return { startedTimeStamp: Date.now(), [SHOULD_AUTOBATCH]: true, direction: queryThunkArgs.arg.direction, data: queryThunkArgs.arg.data}
725731
},
726732
condition(queryThunkArgs, { getState }) {
727733
const state = getState()
@@ -733,6 +739,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
733739
const previousArg = requestState?.originalArgs
734740
const endpointDefinition =
735741
endpointDefinitions[queryThunkArgs.endpointName]
742+
const direction = queryThunkArgs.direction
736743

737744
// Order of these checks matters.
738745
// In order for `upsertQueryData` to successfully run while an existing request is in flight,
@@ -764,7 +771,7 @@ In the case of an unhandled error, no tags will be "provided" or "invalidated".`
764771
}
765772

766773
// Pull from the cache unless we explicitly force refetch or qualify based on time
767-
if (fulfilledVal) {
774+
if (fulfilledVal && !direction) {
768775
// Value is cached and we didn't specify to refresh, skip it.
769776
return false
770777
}

0 commit comments

Comments
 (0)