Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ export type InfiniteQuerySubState<
D extends InfiniteQueryDefinition<any, any, any, any, any>
? QuerySubState<D, InfiniteData<ResultTypeFrom<D>, PageParamFrom<D>>> & {
direction?: InfiniteQueryDirection
initialPageParam?: PageParamFrom<D>
}
: never

Expand Down
9 changes: 7 additions & 2 deletions packages/toolkit/src/query/core/buildSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type {
QueryArgFromAnyQueryDefinition,
DataFromAnyQueryDefinition,
InfiniteQueryThunk,
InfiniteQueryThunkArg,
MutationThunk,
Patch,
QueryThunk,
Expand Down Expand Up @@ -237,8 +238,12 @@ export function buildSlice({
const endpointDefinition = definitions[meta.arg.endpointName]

if (isInfiniteQueryDefinition(endpointDefinition) && 'direction' in arg) {
;(substate as InfiniteQuerySubState<any>).direction =
arg.direction as InfiniteQueryDirection
const infiniteSubstate = substate as InfiniteQuerySubState<any>
infiniteSubstate.direction = arg.direction as InfiniteQueryDirection
const { initialPageParam } = arg as InfiniteQueryThunkArg<any>
if (initialPageParam !== undefined) {
infiniteSubstate.initialPageParam = initialPageParam
}
}
})
}
Expand Down
10 changes: 7 additions & 3 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type {
QueryCacheKey,
InfiniteQueryDirection,
InfiniteQueryKeys,
InfiniteQuerySubState,
} from './apiState'
import { QueryStatus, STATUS_UNINITIALIZED } from './apiState'
import type {
Expand Down Expand Up @@ -764,10 +765,13 @@ export function buildThunks<
// Start by looking up the existing InfiniteData value from state,
// falling back to an empty value if it doesn't exist yet
const blankData = { pages: [], pageParams: [] }
const cachedData = selectors.selectQueryEntry(
const cachedEntry = selectors.selectQueryEntry(
getState(),
arg.queryCacheKey,
)?.data as InfiniteData<unknown, unknown> | undefined
) as InfiniteQuerySubState<any>
const cachedData = cachedEntry.data as
| InfiniteData<unknown, unknown>
| undefined

// When the arg changes or the user forces a refetch,
// we don't include the `direction` flag. This lets us distinguish
Expand Down Expand Up @@ -796,7 +800,7 @@ export function buildThunks<
// Otherwise, fetch the first page and then any remaining pages

const { initialPageParam = infiniteQueryOptions.initialPageParam } =
arg as InfiniteQueryThunkArg<any>
cachedEntry

// If we're doing a refetch, we should start from
// the first page we have cached.
Expand Down
28 changes: 28 additions & 0 deletions packages/toolkit/src/query/tests/infiniteQueries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2154,6 +2154,34 @@ describe('infinite query page fetching and refetching', () => {
first.unsubscribe()
})

test.each(['refetch', 'forward fetch'])(
'a %s after a failed first page starts from the initialPageParam the query was started with',
async (request) => {
const { api, store, requestedParams, getPageParams } = makeInfiniteApi({
failOnHit: 1,
})

const first = store.dispatch(
api.endpoints.pages.initiate('x', { initialPageParam: 5 }),
)
await first
expect(requestedParams).toEqual([5])

if (request === 'refetch') {
await first.refetch()
} else {
await store.dispatch(
api.endpoints.pages.initiate('x', { direction: 'forward' }),
)
}

expect(requestedParams).toEqual([5, 5])
expect(getPageParams()).toEqual([5])

first.unsubscribe()
},
)

test('hasNextPage is computed when selecting, and memoized while the cache entry is unchanged', async () => {
const { api, store, getNextPageParam } = makeInfiniteApi()

Expand Down
Loading