Skip to content

Commit de7430a

Browse files
committed
fixup! feat: add category for createAPIClient and rename related markdown files
1 parent a436986 commit de7430a

3 files changed

Lines changed: 45 additions & 82 deletions

File tree

website/versioned_docs/version-2.14.0/codegen/CLI/cli.mdx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,6 @@ It is possible to use multiple plugins at the same time. For example, `--plugin
184184
- `all` - include all available callbacks (useQuery, useMutation, setQueryData, etc.)
185185
- `none` - don't include any callbacks by default (they must be specified when calling the function)
186186
- `useQuery,useMutation,...` - list of specific callbacks to include
187-
- `context:<ContextName>` - Generate a React Context that provides `queryClient`, `requestFn`, and `baseUrl` to the API client.
188-
When the API client is created without options, it reads these values from the Context at render time.
189-
This enables creating the client **outside of React components**, making hooks static and compatible with React Compiler.
190-
See [Context-based API Client](/codegen/context-api-client.mdx) for detailed documentation.
191187
- **Examples:**
192188
- `--create-api-client-fn createAPIClient services:all callbacks:all` - creates the `createAPIClient()` function that
193189
includes all services and all callbacks by default (_default_ behavior)
@@ -196,9 +192,6 @@ It is possible to use multiple plugins at the same time. For example, `--plugin
196192
building a **minimal bundle size** client where you explicitly control which services and callbacks to include.
197193
- `--create-api-client-fn createCustomClient filename:create-custom-api services:none callbacks:setQueryData,getQueryData` - creates
198194
the `createCustomClient()` function in the `create-custom-api.ts` file with no default services and only `setQueryData()` and `getQueryData()` callbacks
199-
- `--create-api-client-fn createAPIClient context:APIClientContext callbacks:useQuery,useMutation` - creates
200-
the `createAPIClient()` function with a generated `APIClientContext` React Context. When called without options,
201-
the client reads `queryClient`, `requestFn`, and `baseUrl` from the Context
202195

203196
::::tip
204197
When generating multiple API client functions with different parameters, it might be more convenient to use a configuration file

website/versioned_docs/version-2.14.0/codegen/CLI/redocly-config.mdx

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -184,48 +184,3 @@ apis:
184184
- getQueryKey
185185
- getInfiniteQueryKey
186186
```
187-
188-
#### Using Context for React Compiler Compatibility
189-
190-
The `context` option generates a React Context that provides `queryClient`, `requestFn`, and `baseUrl` to the API client.
191-
This allows you to create the client **outside of React components**, making hooks static and compatible with React Compiler.
192-
193-
```yaml
194-
apis:
195-
main:
196-
root: ./openapi.json
197-
x-openapi-qraft:
198-
plugin:
199-
tanstack-query-react: true
200-
openapi-typescript: true
201-
output-dir: src/api
202-
create-api-client-fn:
203-
# React API client with Context support
204-
createAPIClient:
205-
context: APIClientContext # Generate APIClientContext React Context
206-
callbacks:
207-
- useQuery
208-
- useMutation
209-
- setQueryData
210-
- getQueryData
211-
- getQueryKey
212-
- getInfiniteQueryKey
213-
```
214-
215-
With this configuration, you can create the API client outside of components:
216-
217-
```tsx
218-
import { createAPIClient, APIClientContext } from './api';
219-
220-
// Create outside component - hooks are static and React Compiler compatible
221-
const api = createAPIClient();
222-
223-
function MyComponent() {
224-
// Hooks read queryClient, requestFn, baseUrl from APIClientContext
225-
const { data } = api.pets.getPets.useQuery();
226-
return <div>{data?.length} pets</div>;
227-
}
228-
```
229-
230-
For detailed documentation on using Context-based API clients, see the
231-
[Context-based API Client](/codegen/context-api-client.mdx) documentation.

website/versioned_docs/version-2.14.0/codegen/create-api-client-function.mdx

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,30 +148,51 @@ the necessary `requestFn`, `baseUrl` and `queryClient` for React Hooks.
148148
```
149149
</TabItem>
150150
<TabItem value="multiple-api-versions" label={<span style={{verticalAlign: 'middle'}}>Multiple API versions and <em>React <code>Context</code></em></span>}>
151-
This example demonstrates how to use multiple API clients with React Context. With the `context:` option
152-
in CLI, you can generate an API client that retrieves `queryClient`, `requestFn`, and `baseUrl` from
153-
React Context at render time.
151+
In this example, we create multiple API clients that manage two different API versions, each with its own `QueryClient`.
152+
This approach mounts and unmounts `QueryClient` instances during the lifecycle of the application.
154153

155-
:::tip React Compiler Compatibility
156-
Creating the API client **outside of the component** with the `context:` option makes hooks static,
157-
allowing React Compiler to optimize them. If you create the client inside `useMemo`, hooks become
158-
dynamic and cannot be optimized by React Compiler.
159-
:::
154+
This setup is particularly useful when you need to work with different API versions simultaneously, or when you're in the process
155+
of migrating from one API version to another.
160156

161157
```tsx title="src/MultipleAPIClientsApp.tsx"
162-
import { createAPIClient as createAPIClientV1, APIClientContextV1 } from './api-v1'; // generated with context:APIClientContextV1
163-
import { createAPIClient as createAPIClientV2, APIClientContextV2 } from './api-v2'; // generated with context:APIClientContextV2
164-
import { requestFn } from '@openapi-qraft/react';
165-
import { useEffect, useState, type ReactNode } from "react";
158+
import { createAPIClient as createAPIClientV1Basic } from './api-v1'; // generated by OpenAPI Qraft CLI (1️⃣)
159+
import { createAPIClient as createAPIClientV2Basic } from './api-v2'; // generated by OpenAPI Qraft CLI (2️⃣)
160+
import { requestFn, type CreateAPIBasicClientOptions, type CreateAPIQueryClientOptions } from '@openapi-qraft/react';
161+
import { createContext, useContext, useMemo, useEffect, type ReactNode} from "react";
166162
import { QueryClient } from '@tanstack/react-query'
167163

168-
// ⬇︎ Create API clients OUTSIDE of the component - hooks are static and React Compiler compatible
169-
const apiV1 = createAPIClientV1(); // Uses APIClientContextV1 for options
170-
const apiV2 = createAPIClientV2(); // Uses APIClientContextV2 for options
164+
// Hypothetical legacy API Client
165+
function createAPIClientV1(options: CreateAPIQueryClientOptions) {
166+
return createAPIClientV1Basic(options);
167+
}
168+
169+
// New version API Client
170+
function createAPIClientV2(options: CreateAPIQueryClientOptions) {
171+
return createAPIClientV2Basic(options);
172+
}
173+
174+
const MultipleAPIClientsContext = createContext<{
175+
apiV1: ReturnType<typeof createAPIClientV1>; // 1️⃣
176+
apiV2: ReturnType<typeof createAPIClientV2>; // 2️⃣
177+
}>(null!);
171178

172179
export default function MultipleAPIClientsApp({ children }: { children: ReactNode }) {
173-
const [queryClient1] = useState(() => new QueryClient());
174-
const [queryClient2] = useState(() => new QueryClient());
180+
const queryClient1 = useMemo(() => new QueryClient(), []);
181+
const queryClient2 = useMemo(() => new QueryClient(), []);
182+
183+
// Hypothetical legacy API Client without Query Client feature
184+
const apiV1 = useMemo(() => createAPIClientV1({
185+
requestFn,
186+
queryClient: queryClient1,
187+
baseUrl: 'https://api.sandbox.monite.com/v1',
188+
}), [queryClient1]);
189+
190+
// New version API Client with the modern features
191+
const apiV2 = useMemo(() => createAPIClientV2({
192+
requestFn,
193+
queryClient: queryClient2,
194+
baseUrl: 'https://api.sandbox.monite.com/v2',
195+
}), [queryClient2]);
175196

176197
useEffect(() => {
177198
queryClient1.mount();
@@ -183,24 +204,18 @@ the necessary `requestFn`, `baseUrl` and `queryClient` for React Hooks.
183204
}, [queryClient1, queryClient2]);
184205

185206
return (
186-
<APIClientContextV1.Provider value={{ requestFn, queryClient: queryClient1, baseUrl: 'https://api.example.com/v1' }}>
187-
<APIClientContextV2.Provider value={{ requestFn, queryClient: queryClient2, baseUrl: 'https://api.example.com/v2' }}>
188-
{children}
189-
</APIClientContextV2.Provider>
190-
</APIClientContextV1.Provider>
207+
<MultipleAPIClientsContext.Provider value={{ apiV1, apiV2 }}>
208+
{children}
209+
</MultipleAPIClientsContext.Provider>
191210
);
192211
}
193212

194-
function YourComponent() {
195-
// ⬇︎ Use hooks directly - they read options from Context
196-
const { data: v1Data } = apiV1.pets.getPets.useQuery();
197-
const { data: v2Data } = apiV2.pets.getPets.useQuery();
213+
function YourComponents() {
214+
const { apiV1, apiV2 } = useContext(MultipleAPIClientsContext);
198215

199-
return <div>...</div>;
216+
// Use `apiV1` and `apiV2` as needed
217+
// ...
200218
}
201219
```
202-
203-
For more details on configuring and using the Context-based API client, see the
204-
[Context-based API Client](/codegen/context-api-client.mdx) documentation.
205220
</TabItem>
206221
</Tabs>

0 commit comments

Comments
 (0)