Skip to content

Commit c4068d6

Browse files
committed
chore: add snapshot files
1 parent bace2e0 commit c4068d6

File tree

8 files changed

+1284
-0
lines changed

8 files changed

+1284
-0
lines changed

src/snapshot/v1/hooks/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<!--
2+
This code was generated by @basketry/react-query@{{version}}
3+
4+
Changes to this file may cause incorrect behavior and will be lost if
5+
the code is regenerated.
6+
7+
To make changes to the contents of this file:
8+
1. Edit source/path.ext
9+
2. Run the Basketry CLI
10+
11+
About Basketry: https://basketry.io
12+
About @basketry/react-query: https://basketry.io/docs/components/@basketry/react-query
13+
-->
14+
15+
# React Query Hooks
16+
17+
This directory contains the generated React Query hooks that provide access to the BasketryExample v1 API.
18+
19+
For more information about React Query, [read the official docs](https://tanstack.com/query/latest/docs/framework/react/overview).
20+
21+
## Setup
22+
23+
Wrap your application in the `BasketryExampleProvider` exported from the `context` module. This provides implementations of the interfaces that empower the query and mutation hooks.
24+
25+
```tsx
26+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
27+
import { BasketryExampleProvider } from './v1/hooks/context';
28+
29+
export const App = () => {
30+
const queryClient = new QueryClient();
31+
32+
return (
33+
<BasketryExampleProvider root="/v1">
34+
<QueryClientProvider client={queryClient}>
35+
<div>Your app goes here</div>
36+
</QueryClientProvider>
37+
</BasketryExampleProvider>
38+
);
39+
};
40+
```
41+
42+
Note that the `BasketryExampleProvider` _DOES NOT_ automatically service as a Tanstack `QueryClientProvider`. You will also need to wrap your component tree in a `QueryClientProvider`. It doesn't matter which order you wrap the components, but both are required.
43+
44+
## Queries
45+
46+
See: [Tanstack Query docs for Queries](https://tanstack.com/query/latest/docs/framework/react/guides/queries)
47+
48+
Each query hook is the equivalent of the general `useQuery` hook with the method-specific `queryFn`, `select`, and `queryKey` properties provided.
49+
50+
```tsx
51+
import { useGizmos } from './v1/hooks/gizmos';
52+
53+
export const Example = () => {
54+
const { data, isLoading } = useGizmos({
55+
/* params */
56+
});
57+
58+
// Use `isLoading` value to display a loading indicator
59+
if (isLoading) return <div>Loading ...</div>;
60+
61+
// Use `data` value to display the response
62+
return (
63+
<div>
64+
<h1>Here is your data:</h1>
65+
<pre>{JSON.stringify(data, null, 2)}</pre>
66+
</div>
67+
);
68+
};
69+
```
70+
71+
### Suspense
72+
73+
See: [Tanstack Query docs for Suspense](https://tanstack.com/query/latest/docs/framework/react/guides/suspense)
74+
75+
React Query can also be used with React's Suspense for Data Fetching API's. Each generated query hook has a Suspense variant that can be used in place of the standard hook.
76+
77+
```tsx
78+
import { useSuspenseGizmos } from './v1/hooks/gizmos';
79+
80+
export const ExampleContainer = () => (
81+
// Use suspense to display a loading indicator
82+
<React.Suspense fallback={<div>Loading...</div>}>
83+
<Example />
84+
</React.Suspense>
85+
);
86+
87+
export const Example = () => {
88+
const { data } = useSuspenseGizmos({
89+
/* params */
90+
});
91+
92+
// Use `data` value to display the response
93+
return (
94+
<div>
95+
<h1>Here is your data:</h1>
96+
<pre>{JSON.stringify(data, null, 2)}</pre>
97+
</div>
98+
);
99+
};
100+
```
101+
102+
### QueryClient Overrides
103+
104+
Both the standard and suspense hooks can be called with optional client overrides. These options are only applied to the specific query and do not affect the global QueryClient.
105+
106+
```tsx
107+
const { data } = useGizmos(
108+
{
109+
/* params */
110+
},
111+
{ retry: 5, retryDelay: 1000 },
112+
);
113+
114+
const { data } = useSuspenseGizmos(
115+
{
116+
/* params */
117+
},
118+
{ retry: 5, retryDelay: 1000 },
119+
);
120+
```
121+
122+
## Mutations
123+
124+
See: [Tanstack Query docs for Mutations](https://tanstack.com/query/latest/docs/framework/react/guides/mutations)
125+
126+
```tsx
127+
import { useUpdateGizmo } from './v1/hooks/gizmos';
128+
129+
export const Example = () => {
130+
const { mutate } = useUpdateGizmo({
131+
onSuccess: (data, variables) => {
132+
console.log('called with variables', variables);
133+
console.log('returned data', data);
134+
},
135+
onError: console.error,
136+
});
137+
138+
const handleClick = useCallback(() => {
139+
mutate({
140+
/* params */
141+
});
142+
}, [mutate]);
143+
144+
return (
145+
<div>
146+
<button onClick={handleClick}>Update gizmo</button>
147+
</div>
148+
);
149+
};
150+
```
151+
152+
## Error Handling
153+
154+
React Query returns an `error` property from the query and mutation hooks. This value is non-null when an error has been raised.
155+
156+
The generated hooks return an error of type `QueryError<T>` where `T` is the type of error returned from the API method. This error type is a discriminated union of either a handled or unhandled error.
157+
158+
Handled errors will be of type `T` and are generally things like validation errors returned in a structurd format from the API. Unhandled errors are of type `unknown` generally represent exceptions thrown during the execution of the API or the processing of the response.
159+
160+
## Services
161+
162+
The generated hooks make use of the generated HTTP Client service implementations. While hooks provide a React-idiomatic mechanism for interacting with your API, the raw service implmentations provide more precise, fine-gained control.
163+
164+
Using the generated React Query hooks will be sufficient for most use cases; however, the services can be access from within the `BasketryExampleProvider` tree by using the hooks exported from the `context` module.
165+
166+
```tsx
167+
import { useCallback } from 'react';
168+
import { useGizmoService } from './v1/hooks/context';
169+
170+
export const Example = () => {
171+
const gizmoService = useGizmoService();
172+
173+
const handleClick = useCallback(() => {
174+
// Do something directly with the gizmo service
175+
}, [gizmoService]);
176+
177+
return (
178+
<div>
179+
<button onClick={handleClick}>Custom action</button>
180+
</div>
181+
);
182+
};
183+
```
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* This code was generated by @basketry/react-query@{{version}}
3+
*
4+
* Changes to this file may cause incorrect behavior and will be lost if
5+
* the code is regenerated.
6+
*
7+
* To make changes to the contents of this file:
8+
* 1. Edit source/path.ext
9+
* 2. Run the Basketry CLI
10+
*
11+
* About Basketry: https://basketry.io
12+
* About @basketry/react-query: https://basketry.io/docs/components/@basketry/react-query
13+
*/
14+
15+
import {
16+
mutationOptions,
17+
queryOptions,
18+
type UndefinedInitialDataOptions,
19+
useMutation,
20+
type UseMutationOptions,
21+
useQuery,
22+
useQueryClient,
23+
useSuspenseQuery,
24+
} from '@tanstack/react-query';
25+
import type { Error } from '../types';
26+
import {
27+
getAuthPermutationService,
28+
useAuthPermutationService,
29+
} from './context';
30+
import { guard, type QueryError } from './runtime';
31+
32+
// Query and mutation options exports for React Query v5
33+
34+
export const allAuthSchemesQueryOptions = () => {
35+
const authPermutationService = getAuthPermutationService();
36+
return queryOptions({
37+
queryKey: ['authPermutation', 'allAuthSchemes', {}],
38+
queryFn: async () => {
39+
const res = await guard(authPermutationService.allAuthSchemes());
40+
if (res.errors.length) {
41+
const handled: QueryError<Error[]> = {
42+
kind: 'handled',
43+
payload: res.errors,
44+
};
45+
throw handled;
46+
}
47+
return res;
48+
},
49+
select: (data) => data.data,
50+
});
51+
};
52+
53+
export const comboAuthSchemesMutationOptions = () => {
54+
const authPermutationService = getAuthPermutationService();
55+
return mutationOptions({
56+
mutationFn: async () => {
57+
const res = await guard(authPermutationService.comboAuthSchemes());
58+
if (res.errors.length) {
59+
const handled: QueryError<Error[]> = {
60+
kind: 'handled',
61+
payload: res.errors,
62+
};
63+
throw handled;
64+
}
65+
return res.data;
66+
},
67+
});
68+
};
69+
70+
// Legacy hooks - deprecated, use query/mutation options exports instead
71+
72+
/** @deprecated Use allAuthSchemesQueryOptions with useQuery instead */
73+
export function useAllAuthSchemes(
74+
options?: Omit<
75+
UndefinedInitialDataOptions<void, QueryError<Error[]>, void>,
76+
'queryKey' | 'queryFn' | 'select'
77+
>,
78+
) {
79+
const defaultOptions = allAuthSchemesQueryOptions();
80+
return useQuery({ ...defaultOptions, ...options });
81+
}
82+
83+
/** @deprecated Use allAuthSchemesQueryOptions with useSuspenseQuery instead */
84+
export function useAllAuthSchemes(
85+
options?: Omit<
86+
UndefinedInitialDataOptions<void, QueryError<Error[]>, void>,
87+
'queryKey' | 'queryFn' | 'select'
88+
>,
89+
) {
90+
const defaultOptions = allAuthSchemesQueryOptions();
91+
return useSuspenseQuery({ ...defaultOptions, ...options });
92+
}
93+
94+
/** @deprecated Use comboAuthSchemesMutationOptions with useMutation instead */
95+
export function useComboAuthSchemes(
96+
options?: Omit<UseMutationOptions<void, QueryError<Error[]>>, 'mutationFn'>,
97+
) {
98+
const queryClient = useQueryClient();
99+
const authPermutationService = useAuthPermutationService();
100+
return useMutation({
101+
mutationFn: async () => {
102+
const res = await guard(authPermutationService.comboAuthSchemes());
103+
if (res.errors.length) {
104+
const handled: QueryError<Error[]> = {
105+
kind: 'handled',
106+
payload: res.errors,
107+
};
108+
throw handled;
109+
}
110+
queryClient.invalidateQueries({ queryKey: ['authPermutation'] });
111+
return res.data;
112+
},
113+
...options,
114+
});
115+
}

0 commit comments

Comments
 (0)