@@ -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
298394When working with multiple API versions, each can have its own Context:
299395
0 commit comments