100 | );
101 | }
102 |
103 | export default App;
104 | ```
105 |
106 | Invalidating queries after a mutation is important to ensure the cache is updated with the new data. This is done by calling the `queryClient.invalidateQueries` function with the query key used by the query hook.
107 |
108 | Learn more about invalidating queries [here](https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation).
109 |
110 | To ensure the query key is created the same way as the query hook, you can use the query key function exported by the generated query hooks.
111 |
112 | ```tsx
113 | import {
114 | useFindPetsByStatus,
115 | useAddPet,
116 | UseFindPetsByStatusKeyFn,
117 | } from "../openapi/queries";
118 |
119 | function App() {
120 | const [status, setStatus] = React.useState(["available"]);
121 | const { data } = useFindPetsByStatus({ query: { status } });
122 | const { mutate } = useAddPet({
123 | onSuccess: () => {
124 | queryClient.invalidateQueries({
125 | // Call the query key function to get the query key
126 | // This is important to ensure the query key is created the same way as the query hook
127 | // This insures the cache is invalidated correctly and is typed correctly
128 | queryKey: [UseFindPetsByStatusKeyFn({
129 | status
130 | })],
131 | });
132 | },
133 | });
134 |
135 | return (
136 |
137 |
Pet List
138 |
{data?.map((pet) =>
{pet.name}
)}
139 |
146 |
147 | );
148 | }
149 |
150 | export default App;
151 | ```
152 |
153 |
154 | ## Using the generated `useInfiniteQuery` hooks
155 |
156 | This feature will generate a function in infiniteQueries.ts when the name specified by the `pageParam` option exists in the query parameters and the name specified by the `nextPageParam` option exists in the response.
157 |
158 | The `initialPageParam` option can be specified to set the intial page to load, defaults to 1. The `nextPageParam` supports dot notation for nested values (i.e. `meta.next`).
159 |
160 | Example Schema:
161 |
162 | ```yml /name: page|nextPage:/
163 | paths:
164 | /paginated-pets:
165 | get:
166 | description: |
167 | Returns paginated pets from the system that the user has access to
168 | operationId: findPaginatedPets
169 | parameters:
170 | - name: page
171 | in: query
172 | description: page number
173 | required: false
174 | schema:
175 | type: integer
176 | format: int32
177 | - name: tags
178 | in: query
179 | description: tags to filter by
180 | required: false
181 | style: form
182 | schema:
183 | type: array
184 | items:
185 | type: string
186 | - name: limit
187 | in: query
188 | description: maximum number of results to return
189 | required: false
190 | schema:
191 | type: integer
192 | format: int32
193 | responses:
194 | '200':
195 | description: pet response
196 | content:
197 | application/json:
198 | schema:
199 | type: object
200 | properties:
201 | pets:
202 | type: array
203 | items:
204 | $ref: '#/components/schemas/Pet'
205 | nextPage:
206 | type: integer
207 | format: int32
208 | minimum: 1
209 | ```
210 |
211 | Usage of Generated Hooks:
212 |
213 | ```ts
214 | import { useFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
215 |
216 | const { data, fetchNextPage } = useFindPaginatedPetsInfinite({
217 | query: { tags: [], limit: 10 }
218 | });
219 | ```
220 |
--------------------------------------------------------------------------------
/docs/src/content/docs/index.mdx:
--------------------------------------------------------------------------------
1 | ---
2 | title: OpenAPI React Query Codegen
3 | description: Get started building your docs site with Starlight.
4 | template: splash
5 | hero:
6 | tagline: Code generator for creating React Query (also known as TanStack Query) hooks based on your OpenAPI schema.
7 | image:
8 | file: ../../assets/logo.webp
9 | actions:
10 | - text: Get Started
11 | link: /guides/introduction/
12 | icon: right-arrow
13 | - text: View on GitHub
14 | link: https://github.com/7nohe/openapi-react-query-codegen
15 | icon: external
16 | variant: minimal
17 | ---
18 |
19 | import { Card, CardGrid } from '@astrojs/starlight/components';
20 |
21 |
22 |
23 |
24 | Generates custom react hooks that use React(TanStack) Query's useQuery, useSuspenseQuery, useMutation and useInfiniteQuery hooks.
25 |
26 |
27 | Generates custom functions that use React Query's `ensureQueryData` and `prefetchQuery` functions to integrate into frameworks like Next.js and Remix.
28 |
29 |
30 | Generates pure TypeScript clients generated by [@hey-api/openapi-ts](https://github.com/hey-api/openapi-ts) in case you still want to do type-safe API calls without React Query.
31 |
32 |
33 |
--------------------------------------------------------------------------------
/docs/src/content/docs/license.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: License
3 | description: License of OpenAPI React Query Codegen.
4 | ---
5 |
6 | Our library uses the MIT license.
7 | However, our library heavily depends on `@hey-api/openapi-ts` which uses the FSL license.
8 | Please be aware of this when using our library.
9 | You can find more information about the license of `@hey-api/openapi-ts` [here](https://heyapi.vercel.app/license.html).
--------------------------------------------------------------------------------
/docs/src/env.d.ts:
--------------------------------------------------------------------------------
1 | ///
2 | ///
3 |
--------------------------------------------------------------------------------
/docs/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "astro/tsconfigs/strict"
3 | }
4 |
--------------------------------------------------------------------------------
/examples/nextjs-app/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 | .yarn/install-state.gz
8 |
9 | # testing
10 | /coverage
11 |
12 | # next.js
13 | /.next/
14 | /out/
15 |
16 | # production
17 | /build
18 |
19 | # misc
20 | .DS_Store
21 | *.pem
22 |
23 | # debug
24 | npm-debug.log*
25 | yarn-debug.log*
26 | yarn-error.log*
27 |
28 | # local env files
29 | .env*.local
30 |
31 | # vercel
32 | .vercel
33 |
34 | # typescript
35 | *.tsbuildinfo
36 | next-env.d.ts
37 |
--------------------------------------------------------------------------------
/examples/nextjs-app/README.md:
--------------------------------------------------------------------------------
1 | This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
2 |
3 | ## Getting Started
4 |
5 | First, run the development server:
6 |
7 | ```bash
8 | npm run dev
9 | # or
10 | yarn dev
11 | # or
12 | pnpm dev
13 | # or
14 | bun dev
15 | ```
16 |
17 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18 |
19 | You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
20 |
21 | This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
22 |
23 | ## Learn More
24 |
25 | To learn more about Next.js, take a look at the following resources:
26 |
27 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
29 |
30 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
31 |
32 | ## Deploy on Vercel
33 |
34 | The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
35 |
36 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
37 |
--------------------------------------------------------------------------------
/examples/nextjs-app/app/components/PaginatedPets.tsx:
--------------------------------------------------------------------------------
1 | "use client";
2 |
3 | import { useFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
4 | import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
5 | import React from "react";
6 |
7 | export default function PaginatedPets() {
8 | const { data, fetchNextPage } = useFindPaginatedPetsInfinite({
9 | query: {
10 | limit: 10,
11 | tags: [],
12 | },
13 | });
14 |
15 | return (
16 | <>
17 |