|
| 1 | +--- |
| 2 | +id: angular-query |
| 3 | +title: Angular Query |
| 4 | +--- |
| 5 | + |
| 6 | +Start by providing an OpenAPI specification and an Orval config file. To use Angular Query, define the `client` in the Orval config to be `angular-query`. |
| 7 | + |
| 8 | +## Example with Angular Query |
| 9 | + |
| 10 | +```js |
| 11 | +import { defineConfig } from 'orval'; |
| 12 | + |
| 13 | +export default defineConfig({ |
| 14 | + petstore: { |
| 15 | + output: { |
| 16 | + mode: 'tags-split', |
| 17 | + target: 'src/petstore.ts', |
| 18 | + schemas: 'src/model', |
| 19 | + client: 'angular-query', |
| 20 | + mock: true, |
| 21 | + }, |
| 22 | + input: { |
| 23 | + target: './petstore.yaml', |
| 24 | + }, |
| 25 | + }, |
| 26 | +}); |
| 27 | +``` |
| 28 | + |
| 29 | +Navigate to the [Orval config reference](../reference/configuration/full-example) to see all available options. |
| 30 | + |
| 31 | +The Angular Query mode will generate an implementation file with one injectable query function per path in the OpenAPI Specification. By default, Orval uses Angular's native `HttpClient` via `inject(HttpClient)` for HTTP requests. |
| 32 | + |
| 33 | +For example, <a href="https://github.com/orval-labs/orval/blob/master/samples/angular-query/petstore.yaml" target="_blank">this Swagger specification</a> will generate the following: |
| 34 | + |
| 35 | +```ts |
| 36 | +export const showPetById = ( |
| 37 | + http: HttpClient, |
| 38 | + petId: string, |
| 39 | + options?: { signal?: AbortSignal }, |
| 40 | +): Promise<Pet> => { |
| 41 | + const request$ = http.get<Pet>(`/pets/${petId}`); |
| 42 | + if (options?.signal) { |
| 43 | + return lastValueFrom( |
| 44 | + request$.pipe(takeUntil(fromEvent(options.signal, 'abort'))), |
| 45 | + ); |
| 46 | + } |
| 47 | + return lastValueFrom(request$); |
| 48 | +}; |
| 49 | + |
| 50 | +export const getShowPetByIdQueryKey = (petId: string) => [`/pets/${petId}`]; |
| 51 | + |
| 52 | +export const getShowPetByIdQueryOptions = < |
| 53 | + TData = Awaited<ReturnType<typeof showPetById>>, |
| 54 | + TError = unknown, |
| 55 | +>( |
| 56 | + petId: string, |
| 57 | + options?: { |
| 58 | + query?: Partial< |
| 59 | + CreateQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData> |
| 60 | + >; |
| 61 | + fetch?: { signal?: AbortSignal }; |
| 62 | + }, |
| 63 | +) => { |
| 64 | + const { query: queryOptions, fetch: fetchOptions } = options ?? {}; |
| 65 | + const http = inject(HttpClient); |
| 66 | + |
| 67 | + const queryKey = queryOptions?.queryKey ?? getShowPetByIdQueryKey(petId); |
| 68 | + |
| 69 | + const queryFn: QueryFunction<Awaited<ReturnType<typeof showPetById>>> = ({ |
| 70 | + signal, |
| 71 | + }) => showPetById(http, petId, { signal, ...fetchOptions }); |
| 72 | + |
| 73 | + return { |
| 74 | + queryKey, |
| 75 | + queryFn, |
| 76 | + enabled: !!petId, |
| 77 | + ...queryOptions, |
| 78 | + } as CreateQueryOptions< |
| 79 | + Awaited<ReturnType<typeof showPetById>>, |
| 80 | + TError, |
| 81 | + TData |
| 82 | + >; |
| 83 | +}; |
| 84 | + |
| 85 | +export const injectShowPetById = < |
| 86 | + TData = Awaited<ReturnType<typeof showPetById>>, |
| 87 | + TError = unknown, |
| 88 | +>( |
| 89 | + petId: string, |
| 90 | + options?: { |
| 91 | + query?: Partial< |
| 92 | + CreateQueryOptions<Awaited<ReturnType<typeof showPetById>>, TError, TData> |
| 93 | + >; |
| 94 | + fetch?: { signal?: AbortSignal }; |
| 95 | + }, |
| 96 | +) => { |
| 97 | + const queryOptions = getShowPetByIdQueryOptions(petId, options); |
| 98 | + |
| 99 | + const query = injectQuery(() => queryOptions) as CreateQueryResult< |
| 100 | + TData, |
| 101 | + TError |
| 102 | + >; |
| 103 | + |
| 104 | + return query; |
| 105 | +}; |
| 106 | +``` |
| 107 | + |
| 108 | +## How to Use Other Queries |
| 109 | + |
| 110 | +Given the following configuration, Orval will generate query and infinite query functions with a `nextId` query parameter. It is also possible to override the config for every query with the `options` field. |
| 111 | + |
| 112 | +```js |
| 113 | +import { defineConfig } from 'orval'; |
| 114 | + |
| 115 | +export default defineConfig({ |
| 116 | + petstore: { |
| 117 | + output: { |
| 118 | + ... |
| 119 | + override: { |
| 120 | + query: { |
| 121 | + useQuery: true, |
| 122 | + useInfinite: true, |
| 123 | + useInfiniteQueryParam: 'nextId', |
| 124 | + options: { |
| 125 | + staleTime: 10000, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + ... |
| 131 | + }, |
| 132 | +}); |
| 133 | +``` |
| 134 | + |
| 135 | +If needed, it is also possible to override the `query` options for a single operation or tag: |
| 136 | + |
| 137 | +```js |
| 138 | +import { defineConfig } from 'orval'; |
| 139 | + |
| 140 | +export default defineConfig({ |
| 141 | + petstore: { |
| 142 | + output: { |
| 143 | + ... |
| 144 | + override: { |
| 145 | + operations: { |
| 146 | + listPets: { |
| 147 | + query: { |
| 148 | + ... |
| 149 | + }, |
| 150 | + } |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + ... |
| 155 | + }, |
| 156 | +}); |
| 157 | +``` |
| 158 | + |
| 159 | +Go <a href="https://github.com/orval-labs/orval/tree/master/samples/angular-query" target="_blank">here</a> for a full example |
0 commit comments