|
| 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 | +``` |
0 commit comments