-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
feat(openapi-react-query): support for useInfiniteQuery() #1881
Open
jungwoo3490
wants to merge
5
commits into
openapi-ts:main
Choose a base branch
from
jungwoo3490:feat/useInfiniteQuery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+339
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d7495b
feat(react-query): add useInfiniteQuery
jungwoo3490 5b61eea
feat(react-query): add unit test for useInfiniteQuery
jungwoo3490 ba441ca
fix(react-query): revert unnecessary import order change
jungwoo3490 dabc800
docs(react-query): add useInfiniteQuery docs
jungwoo3490 688bc2f
fix(react-query): fix lint ci error
jungwoo3490 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
title: useInfiniteQuery | ||
--- | ||
|
||
# {{ $frontmatter.title }} | ||
|
||
The `useInfiniteQuery` method allows you to use the original [useInfiniteQuery](https://tanstack.com/query/latest/docs/framework/react/guides/infinite-queries) | ||
|
||
- The result is the same as the original function. | ||
- The `queryKey` is `[method, path, params]`. | ||
- `data` and `error` are fully typed. | ||
- You can pass infinite query options as fourth parameter. | ||
|
||
::: tip | ||
You can find more information about `useInfiniteQuery` on the [@tanstack/react-query documentation](https://tanstack.com/query/latest/docs/framework/react/guides/infinite-queries). | ||
::: | ||
|
||
## Example | ||
|
||
::: code-group | ||
|
||
```tsx [src/app.tsx] | ||
import { $api } from "./api"; | ||
const PostList = () => { | ||
const { data, fetchNextPage, hasNextPage, isFetching } = | ||
$api.useInfiniteQuery( | ||
"get", | ||
"/posts", | ||
{ | ||
params: { | ||
query: { | ||
limit: 10, | ||
}, | ||
}, | ||
}, | ||
{ | ||
getNextPageParam: (lastPage) => lastPage.nextPage, | ||
initialPageParam: 0, | ||
} | ||
); | ||
|
||
return ( | ||
<div> | ||
{data?.pages.map((page, i) => ( | ||
<div key={i}> | ||
{page.items.map((post) => ( | ||
<div key={post.id}>{post.title}</div> | ||
))} | ||
</div> | ||
))} | ||
{hasNextPage && ( | ||
<button onClick={() => fetchNextPage()} disabled={isFetching}> | ||
{isFetching ? "Loading..." : "Load More"} | ||
</button> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export const App = () => { | ||
return ( | ||
<ErrorBoundary fallbackRender={({ error }) => `Error: ${error.message}`}> | ||
<MyComponent /> | ||
</ErrorBoundary> | ||
); | ||
}; | ||
``` | ||
|
||
```ts [src/api.ts] | ||
import createFetchClient from "openapi-fetch"; | ||
import createClient from "openapi-react-query"; | ||
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript | ||
|
||
const fetchClient = createFetchClient<paths>({ | ||
baseUrl: "https://myapi.dev/v1/", | ||
}); | ||
export const $api = createClient(fetchClient); | ||
``` | ||
|
||
::: | ||
|
||
## Api | ||
|
||
```tsx | ||
const query = $api.useInfiniteQuery( | ||
method, | ||
path, | ||
options, | ||
infiniteQueryOptions, | ||
queryClient | ||
); | ||
``` | ||
|
||
**Arguments** | ||
|
||
- `method` **(required)** | ||
- The HTTP method to use for the request. | ||
- The method is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `path` **(required)** | ||
- The pathname to use for the request. | ||
- Must be an available path for the given method in your schema. | ||
- The pathname is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `options` | ||
- The fetch options to use for the request. | ||
- Only required if the OpenApi schema requires parameters. | ||
- The options `params` are used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. | ||
- `infiniteQueryOptions` | ||
- The original `useInfiniteQuery` options. | ||
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) | ||
- `queryClient` | ||
- The original `queryClient` option. | ||
- [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to add
getPreviousPageParam
here too or do we get it by default viabaseOptions
?