Skip to content

Fix infinite query type portability issues #4881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2025
Merged
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
25 changes: 25 additions & 0 deletions examples/type-portability/bundler/src/app/services/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export const postsApi = apiSlice.injectEndpoints({
getErrorProne: build.query<{ success: boolean }, void>({
query: () => 'error-prone',
}),
getInfinitePosts: build.infiniteQuery<PostsResponse, void, number>({
queryFn: ({ pageParam = 0 }) => ({
data: [],
}),
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (lastPage) =>
lastPage.length === 0 ? undefined : lastPage[lastPage.length - 1].id,
},
}),
}),
})

Expand All @@ -87,6 +97,7 @@ export const {
useLazyGetErrorProneQuery,
useLazyGetPostQuery,
useLazyGetPostsQuery,
useGetInfinitePostsInfiniteQuery,
endpoints,
enhanceEndpoints,
injectEndpoints,
Expand All @@ -106,6 +117,7 @@ export const {
getPosts,
login,
updatePost,
getInfinitePosts,
} = endpoints

export const {
Expand Down Expand Up @@ -182,6 +194,19 @@ export const {
useMutation: _____useMutation,
} = updatePost

export const {
Types: ______Types,
initiate: ______initiate,
matchFulfilled: ______matchFulfilled,
matchPending: ______matchPending,
matchRejected: ______matchRejected,
name: ______name,
select: ______select,
useInfiniteQueryState: ______useQueryState,
useInfiniteQuery: ______useQuery,
useInfiniteQuerySubscription: ______useQuerySubscription,
} = getInfinitePosts

export const {
internal_getRTKQSubscriptions,
middlewareRegistered,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useGetErrorProneQuery,
useGetPostsQuery,
useLoginMutation,
useGetInfinitePostsInfiniteQuery,
} from '../../app/services/posts'
import { logout, selectIsAuthenticated } from '../auth/authSlice'
import { PostDetail } from './PostDetail'
Expand Down Expand Up @@ -70,6 +71,7 @@ export const PostListItem = ({

export const PostList = () => {
const { data: posts, isLoading } = useGetPostsQuery()
useGetInfinitePostsInfiniteQuery()
const navigate = useNavigate()

if (isLoading) {
Expand Down
27 changes: 27 additions & 0 deletions examples/type-portability/nodenext-cjs/src/app/services/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ namespace postsModule {
getErrorProne: build.query<{ success: boolean }, void>({
query: () => 'error-prone',
}),
getInfinitePosts: build.infiniteQuery<PostsResponse, void, number>({
queryFn: ({ pageParam = 0 }) => ({
data: [],
}),
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (lastPage) =>
lastPage.length === 0
? undefined
: lastPage[lastPage.length - 1].id,
},
}),
}),
})

Expand All @@ -90,6 +102,7 @@ namespace postsModule {
useLazyGetErrorProneQuery,
useLazyGetPostQuery,
useLazyGetPostsQuery,
useGetInfinitePostsInfiniteQuery,
endpoints,
enhanceEndpoints,
injectEndpoints,
Expand All @@ -109,6 +122,7 @@ namespace postsModule {
getPosts,
login,
updatePost,
getInfinitePosts,
} = endpoints

export const {
Expand Down Expand Up @@ -185,6 +199,19 @@ namespace postsModule {
useMutation: _____useMutation,
} = updatePost

export const {
Types: ______Types,
initiate: ______initiate,
matchFulfilled: ______matchFulfilled,
matchPending: ______matchPending,
matchRejected: ______matchRejected,
name: ______name,
select: ______select,
useInfiniteQueryState: ______useQueryState,
useInfiniteQuery: ______useQuery,
useInfiniteQuerySubscription: ______useQuerySubscription,
} = getInfinitePosts

export const {
internal_getRTKQSubscriptions,
middlewareRegistered,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import useAddPostMutation = postsModule.useAddPostMutation
import useGetErrorProneQuery = postsModule.useGetErrorProneQuery
import useGetPostsQuery = postsModule.useGetPostsQuery
import useLoginMutation = postsModule.useLoginMutation
import useGetInfinitePostsInfiniteQuery = postsModule.useGetInfinitePostsInfiniteQuery
import logout = authSliceModule.logout
import selectIsAuthenticated = authSliceModule.selectIsAuthenticated

Expand Down Expand Up @@ -81,6 +82,7 @@ const PostListItem = ({

const PostList = () => {
const { data: posts, isLoading } = useGetPostsQuery()
useGetInfinitePostsInfiniteQuery()
const navigate = useNavigate()

if (isLoading) {
Expand Down
25 changes: 25 additions & 0 deletions examples/type-portability/nodenext-esm/src/app/services/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ export const postsApi = apiSlice.injectEndpoints({
getErrorProne: build.query<{ success: boolean }, void>({
query: () => 'error-prone',
}),
getInfinitePosts: build.infiniteQuery<PostsResponse, void, number>({
queryFn: ({ pageParam = 0 }) => ({
data: [],
}),
infiniteQueryOptions: {
initialPageParam: 0,
getNextPageParam: (lastPage) =>
lastPage.length === 0 ? undefined : lastPage[lastPage.length - 1].id,
},
}),
}),
})

Expand All @@ -87,6 +97,7 @@ export const {
useLazyGetErrorProneQuery,
useLazyGetPostQuery,
useLazyGetPostsQuery,
useGetInfinitePostsInfiniteQuery,
endpoints,
enhanceEndpoints,
injectEndpoints,
Expand All @@ -106,6 +117,7 @@ export const {
getPosts,
login,
updatePost,
getInfinitePosts,
} = endpoints

export const {
Expand Down Expand Up @@ -182,6 +194,19 @@ export const {
useMutation: _____useMutation,
} = updatePost

export const {
Types: ______Types,
initiate: ______initiate,
matchFulfilled: ______matchFulfilled,
matchPending: ______matchPending,
matchRejected: ______matchRejected,
name: ______name,
select: ______select,
useInfiniteQueryState: ______useQueryState,
useInfiniteQuery: ______useQuery,
useInfiniteQuerySubscription: ______useQuerySubscription,
} = getInfinitePosts

export const {
internal_getRTKQSubscriptions,
middlewareRegistered,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Post } from '../../app/services/posts.js'
import {
useAddPostMutation,
useGetErrorProneQuery,
useGetInfinitePostsInfiniteQuery,
useGetPostsQuery,
useLoginMutation,
} from '../../app/services/posts.js'
Expand Down Expand Up @@ -70,6 +71,7 @@ export const PostListItem = ({

export const PostList = () => {
const { data: posts, isLoading } = useGetPostsQuery()
useGetInfinitePostsInfiniteQuery()
const navigate = useNavigate()

if (isLoading) {
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export type InfiniteQueryConfigOptions<DataType, PageParam> = {
maxPages?: number
}

export interface InfiniteData<DataType, PageParam> {
export type InfiniteData<DataType, PageParam> = {
pages: Array<DataType>
pageParams: Array<PageParam>
}
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/endpointDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,14 @@ export type QueryDefinition<
> = BaseEndpointDefinition<QueryArg, BaseQuery, ResultType> &
QueryExtraOptions<TagTypes, ResultType, QueryArg, BaseQuery, ReducerPath>

export interface InfiniteQueryTypes<
export type InfiniteQueryTypes<
QueryArg,
PageParam,
BaseQuery extends BaseQueryFn,
TagTypes extends string,
ResultType,
ReducerPath extends string = string,
> extends BaseEndpointTypes<QueryArg, BaseQuery, ResultType> {
> = BaseEndpointTypes<QueryArg, BaseQuery, ResultType> & {
/**
* The endpoint definition type. To be used with some internal generic types.
* @example
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/query/react/buildHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,9 @@ export type TypedLazyInfiniteQueryTrigger<
>
>

interface UseInfiniteQuerySubscriptionOptions<
export type UseInfiniteQuerySubscriptionOptions<
D extends InfiniteQueryDefinition<any, any, any, any, any>,
> extends SubscriptionOptions {
> = SubscriptionOptions & {
/**
* Prevents a query from automatically running.
*
Expand Down