useInfiniteQuery(...)
The Hook enables you to perform asynchronous data fetching operations with support for infinite scrolling scenarios. See the TanStack useInfiniteQuery(...) ๐ด documentation for more details.
const query = qraft.<service>.<operation>.useInfiniteQuery(
parameters,
infiniteQueryOptions,
queryClient,
)
Argumentsโ
-
parameters: { path, query, header } | QueryKey | {}
- Required and strictly-typed โจ parameters for the query
parameters
will be used to generate theQueryKey
- Instead of an object with
{path, query, header}
, you can pass aQueryKey
as an array which is also strictly-typed โจ - If operation does not require parameters, you must pass an empty object
{}
for strictness
-
infiniteQueryOptions?: UseInfiniteQueryOptions
- Optional, represents the options of the useInfiniteQuery(...) ๐ด Hook
queryOptions.queryFn
could be provided to override the defaultqueryFn
used by Qraftis not allowed, as it will be generated fromqueryOptions.queryKey
parameters
- Optional, represents the options of the useInfiniteQuery(...) ๐ด Hook
-
queryClient?: QueryClient
- Optional QueryClient ๐ด to be used
- If not provided
QraftContext.queryClient
will be used if available- useQueryClient() ๐ด result will be used as a fallback
Returnsโ
UseInfiniteQueryResult
object, see the TanStack useInfiniteQuery(...) ๐ด documentation for more details.
Examplesโ
- Offset-based ๐ฆ
- PaginationToken-based ๐ฆ
src/PostList.tsx
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI
const qraft = createAPIClient();
function PostList() {
/**
* `qraft.posts.getPosts.useInfiniteQuery(...)` launches
* the initial query to fetch the first page of posts:
* ###
* GET /posts?limit=10&page=1
*/
const infiniteQuery = qraft.posts.getPosts.useInfiniteQuery(
{ query: { limit: 10 } },
{
getNextPageParam: (lastPage, allPages, lastPageParams) => {
if (lastPage.length < 10) return; // if less than 10 items, there are no more pages
return {
query: {
page: Number(lastPageParams.query?.page) + 1,
},
};
},
initialPageParam: {
query: {
page: 1, // will be used in initial request
},
},
}
);
return (
<div>
{infiniteQuery.data?.pages.map((page, pageIndex) => (
<ul key={pageIndex}>
{page.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
))}
<button onClick={() => {
// โฌ๏ธ Initiates a GET request for the next page of posts: /posts?limit=10&page=2
infiniteQuery.fetchNextPage()
}}>
Load More
</button>
</div>
);
}
src/DataExportList.tsx
import { createAPIClient } from './api'; // generated by OpenAPI Qraft CLI
const qraft = createAPIClient();
function DataExportList({ initial_pagination_token }: { initial_pagination_token?: string }) {
/**
* `qraft.posts.getPosts.useInfiniteQuery(...)` launches
* the initial query to fetch the first page of posts:
* ###
* GET /data_exports?pagination_token=<INITIAL_PAGINATION_TOKEN>
* x-monite-version: 2023-09-01
* x-monite-entity-id: 3e3e-3e3e-3e3e
**/
const infiniteQuery = qraft.dataExports.getDataExports.useInfiniteQuery(
{
header: {
'x-monite-version': '2023-09-01',
'x-monite-entity-id': '3e3e-3e3e-3e3e',
},
},
{
initialPageParam: {
query: { pagination_token: initial_pagination_token }, // will be used in initial request
},
getNextPageParam: (lastPage, allPages, lastPageParam) => ({
query: { pagination_token: lastPage.next_pagination_token },
}),
getPreviousPageParam: (firstPage, allPages, firstPageParam) => ({
query: { pagination_token: firstPage.prev_pagination_token },
}),
}
);
return (
<div>
<button
disabled={!infiniteQuery.hasPreviousPage}
onClick={() => {
/**
* โฌ๏ธ Initiates a GET request:
* GET /data_exports?pagination_token=<PREV_PAGINATION_TOKEN>
* x-monite-version: 2023-09-01
* x-monite-entity-id: 3e3e-3e3e-3e3e
*/
infiniteQuery.fetchPreviousPage()
}}
>
Load Previous
</button>
{infiniteQuery.data?.pages.map((page, pageIndex) => (
<ul key={pageIndex}>
{page.map((dataExport) => (
<li key={dataExport.id}>{dataExport.name}</li>
))}
</ul>
))}
<button
disabled={!infiniteQuery.hasNextPage}
onClick={() => {
/**
* โฌ๏ธ Initiates a GET request:
* GET /data_exports?pagination_token=<NEXT_PAGINATION_TOKEN>
* x-monite-version: 2023-09-01
* x-monite-entity-id: 3e3e-3e3e-3e3e
*/
infiniteQuery.fetchNextPage()
}}
>
Load More
</button>
</div>
);
}