|
| 1 | +--- |
| 2 | +title: infiniteQueryOptions |
| 3 | +--- |
| 4 | + |
| 5 | +# {{ $frontmatter.title }} |
| 6 | + |
| 7 | +The `infiniteQueryOptions` method allows you to construct type-safe [Infinite Query Options](https://tanstack.com/query/latest/docs/framework/react/guides/infinite-queries). |
| 8 | + |
| 9 | +`infiniteQueryOptions` can be used together with `@tanstack/react-query` APIs that take infinite query options, such as |
| 10 | +[useInfiniteQuery](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery), |
| 11 | +[usePrefetchInfiniteQuery](https://tanstack.com/query/latest/docs/framework/react/reference/usePrefetchInfiniteQuery), |
| 12 | +[QueryClient.fetchInfiniteQuery](https://tanstack.com/query/latest/docs/reference/QueryClient#queryclientfetchinfinitequery) |
| 13 | +among many others. |
| 14 | + |
| 15 | +If you would like to use an infinite query API that is not explicitly supported by `openapi-react-query`, this is the way to go. |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +[useInfiniteQuery example](use-infinite-query#example) rewritten using `infiniteQueryOptions`. |
| 20 | + |
| 21 | +::: code-group |
| 22 | + |
| 23 | +```tsx [src/app.tsx] |
| 24 | +import { useInfiniteQuery } from '@tanstack/react-query'; |
| 25 | +import { $api } from './api'; |
| 26 | + |
| 27 | +const PostList = () => { |
| 28 | + const { data, fetchNextPage, hasNextPage, isFetching } = useInfiniteQuery( |
| 29 | + $api.infiniteQueryOptions( |
| 30 | + 'get', |
| 31 | + '/posts', |
| 32 | + { |
| 33 | + params: { |
| 34 | + query: { |
| 35 | + limit: 10, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + getNextPageParam: lastPage => lastPage.nextPage, |
| 41 | + initialPageParam: 0, |
| 42 | + } |
| 43 | + ) |
| 44 | + ); |
| 45 | + |
| 46 | + return ( |
| 47 | + <div> |
| 48 | + {data?.pages.map((page, i) => ( |
| 49 | + <div key={i}> |
| 50 | + {page.items.map(post => ( |
| 51 | + <div key={post.id}>{post.title}</div> |
| 52 | + ))} |
| 53 | + </div> |
| 54 | + ))} |
| 55 | + {hasNextPage && ( |
| 56 | + <button onClick={() => fetchNextPage()} disabled={isFetching}> |
| 57 | + {isFetching ? 'Loading...' : 'Load More'} |
| 58 | + </button> |
| 59 | + )} |
| 60 | + </div> |
| 61 | + ); |
| 62 | +}; |
| 63 | +``` |
| 64 | + |
| 65 | +```ts [src/api.ts] |
| 66 | +import createFetchClient from 'openapi-fetch'; |
| 67 | +import createClient from 'openapi-react-query'; |
| 68 | +import type { paths } from './my-openapi-3-schema'; // generated by openapi-typescript |
| 69 | + |
| 70 | +const fetchClient = createFetchClient<paths>({ |
| 71 | + baseUrl: 'https://myapi.dev/v1/', |
| 72 | +}); |
| 73 | +export const $api = createClient(fetchClient); |
| 74 | +``` |
| 75 | + |
| 76 | +::: |
| 77 | + |
| 78 | +::: info Good to Know |
| 79 | + |
| 80 | +[useInfiniteQuery](use-infinite-query) uses `infiniteQueryOptions` under the hood. |
| 81 | + |
| 82 | +::: |
| 83 | + |
| 84 | +Usage with `usePrefetchInfiniteQuery`. |
| 85 | + |
| 86 | +::: code-group |
| 87 | + |
| 88 | +```tsx [src/post-list.tsx] |
| 89 | +import { usePrefetchInfiniteQuery } from '@tanstack/react-query'; |
| 90 | +import { $api } from './api'; |
| 91 | + |
| 92 | +export const PostList = () => { |
| 93 | + // Prefetch infinite query |
| 94 | + usePrefetchInfiniteQuery( |
| 95 | + $api.infiniteQueryOptions( |
| 96 | + 'get', |
| 97 | + '/posts', |
| 98 | + { |
| 99 | + params: { |
| 100 | + query: { |
| 101 | + limit: 10, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + getNextPageParam: lastPage => lastPage.nextPage, |
| 107 | + initialPageParam: 0, |
| 108 | + pages: 3, // Prefetch first 3 pages |
| 109 | + } |
| 110 | + ) |
| 111 | + ); |
| 112 | + |
| 113 | + // ... rest of the component |
| 114 | +}; |
| 115 | +``` |
| 116 | + |
| 117 | +```ts [src/api.ts] |
| 118 | +import createFetchClient from 'openapi-fetch'; |
| 119 | +import createClient from 'openapi-react-query'; |
| 120 | +import type { paths } from './my-openapi-3-schema'; // generated by openapi-typescript |
| 121 | + |
| 122 | +const fetchClient = createFetchClient<paths>({ |
| 123 | + baseUrl: 'https://myapi.dev/v1/', |
| 124 | +}); |
| 125 | +export const $api = createClient(fetchClient); |
| 126 | +``` |
| 127 | + |
| 128 | +::: |
| 129 | + |
| 130 | +## Api |
| 131 | + |
| 132 | +```tsx |
| 133 | +const infiniteQueryOptions = $api.infiniteQueryOptions(method, path, options, infiniteQueryOptions); |
| 134 | +``` |
| 135 | + |
| 136 | +**Arguments** |
| 137 | + |
| 138 | +- `method` **(required)** |
| 139 | + - The HTTP method to use for the request. |
| 140 | + - The method is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. |
| 141 | +- `path` **(required)** |
| 142 | + - The pathname to use for the request. |
| 143 | + - Must be an available path for the given method in your schema. |
| 144 | + - The pathname is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. |
| 145 | +- `options` |
| 146 | + - The fetch options to use for the request. |
| 147 | + - Only required if the OpenApi schema requires parameters. |
| 148 | + - The options `params` are used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. |
| 149 | +- `infiniteQueryOptions` **(required)** |
| 150 | + - `pageParamName` The query param name used for pagination, `"cursor"` by default. |
| 151 | + - `initialPageParam` **(required)**: The initial page param for the first page. |
| 152 | + - `getNextPageParam`: Function to calculate the next page param. |
| 153 | + - `getPreviousPageParam`: Function to calculate the previous page param. |
| 154 | + - Additional infinite query options can be passed through. |
| 155 | + |
| 156 | +**Returns** |
| 157 | + |
| 158 | +- [Infinite Query Options](https://tanstack.com/query/latest/docs/framework/react/guides/infinite-queries) |
| 159 | + - Fully typed thus `data` and `error` will be correctly deduced. |
| 160 | + - `queryKey` is `[method, path, params]`. |
| 161 | + - `queryFn` is set to a fetcher function that handles pagination. |
0 commit comments