Skip to content

Commit 851a4e8

Browse files
committed
feat: enhance documentation for context-based API client patterns and usage examples
1 parent 4046196 commit 851a4e8

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

website/docs/codegen/cli/redocly-config.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,18 @@ apis:
233233
234234
# React API client with specific callbacks only
235235
createReactAPIClient:
236+
context: APIClientContext # Read baseUrl/requestFn/queryClient from React Context
236237
services: none # Don't include any services directly
237238
callbacks: # Include only specific callbacks
238239
- setQueryData
239240
- getQueryData
240241
- getQueryKey
241242
- getInfiniteQueryKey
242243
```
244+
245+
::::info Context-based createAPIClient
246+
`context:<ContextName>` is a must-have for React Compiler compatibility because it keeps generated hooks static
247+
(API client can be created outside React components).
248+
Need full setup and runtime patterns? See
249+
[Context-based API Client](../openapi/create-api-client-function/context-api-client.mdx).
250+
::::

website/docs/codegen/openapi/create-api-client-function/context-api-client.mdx

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,103 @@ function PetUpdateForm({ petId }: { petId: number }) {
293293
}
294294
```
295295

296-
### Pattern 4: Multiple API Versions
296+
### Pattern 4: Minimal Context Client with Runtime Callbacks
297+
298+
Use this pattern when you generate a fully minimal context client and pass
299+
only the services/operations and callbacks you need at runtime.
300+
Keep hook callbacks (like `useMutation`) on a static client outside components,
301+
but create a utility client inside a component for methods like `setQueryData`
302+
that need the current Context value.
303+
304+
```bash
305+
--create-api-client-fn createMinimalContextAPIClient \
306+
filename:create-minimal-context-api-client \
307+
context:APIClientContext \
308+
services:none \
309+
callbacks:none
310+
```
311+
312+
```tsx title="src/PetEditor.tsx"
313+
import {
314+
createMinimalContextAPIClient,
315+
APIClientContext,
316+
services,
317+
} from './api';
318+
import { useMutation, setQueryData } from '@openapi-qraft/react/callbacks';
319+
import { requestFn } from '@openapi-qraft/react';
320+
import { QueryClient } from '@tanstack/react-query';
321+
import { useContext, useState, useEffect, type ReactNode } from 'react';
322+
323+
// Static hooks client (React Compiler friendly)
324+
const mutationApi = createMinimalContextAPIClient(
325+
{
326+
updatePet: services.pet.updatePet,
327+
},
328+
{ useMutation }
329+
);
330+
331+
function APIProvider({ children }: { children: ReactNode }) {
332+
const [queryClient] = useState(() => new QueryClient());
333+
334+
useEffect(() => {
335+
queryClient.mount();
336+
return () => queryClient.unmount();
337+
}, [queryClient]);
338+
339+
return (
340+
<APIClientContext.Provider
341+
value={{
342+
requestFn,
343+
queryClient,
344+
baseUrl: 'https://petstore3.swagger.io/api/v3',
345+
}}
346+
>
347+
{children}
348+
</APIClientContext.Provider>
349+
);
350+
}
351+
352+
function PetEditor({ petId }: { petId: number }) {
353+
const petParams = { path: { petId } };
354+
const apiContext = useContext(APIClientContext);
355+
356+
// Utility client that needs direct access to runtime queryClient/requestFn/baseUrl from Context
357+
const cacheApi = createMinimalContextAPIClient(
358+
{
359+
getPetById: services.pet.getPetById,
360+
},
361+
apiContext!,
362+
{ setQueryData }
363+
);
364+
365+
const { mutate, isPending } = mutationApi.updatePet.useMutation(undefined, {
366+
onMutate(variables) {
367+
cacheApi.getPetById.setQueryData(petParams, (old) => (
368+
old ? { ...old, ...variables.body } : old
369+
));
370+
},
371+
});
372+
373+
return (
374+
<button
375+
disabled={isPending}
376+
onClick={() => mutate({ body: { id: petId, name: 'Renamed', photoUrls: [] } })}
377+
>
378+
Rename Pet
379+
</button>
380+
);
381+
}
382+
383+
export default function App() {
384+
return (
385+
<APIProvider>
386+
<PetEditor petId={1} />
387+
</APIProvider>
388+
);
389+
}
390+
```
391+
392+
### Pattern 5: Multiple API Versions
297393

298394
When working with multiple API versions, each can have its own Context:
299395

0 commit comments

Comments
 (0)