Skip to content

Commit 01574bc

Browse files
authored
feat: select support added for react-query components (#83)
* select support added for react-query * typo fix * fix: missing TSelectData on UseQueryOptions added * infer fetcher response type and support for blob * fix: use TData instead of TSelectData and infer queryResult * merge error fixed
1 parent 3a15e0c commit 01574bc

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

plugins/typescript/src/generators/generateReactQueryComponents.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("generateReactQueryComponents", () => {
109109
/**
110110
* Get all the pets
111111
*/
112-
export const useListPets = (variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, ListPetsResponse>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
112+
export const useListPets = <TData = ListPetsResponse>(variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, TData>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
113113
...options,
114114
...queryOptions
115115
}); };
@@ -231,7 +231,7 @@ describe("generateReactQueryComponents", () => {
231231
/**
232232
* Get all the pets
233233
*/
234-
export const useListPets = (variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, ListPetsResponse>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
234+
export const useListPets = <TData = ListPetsResponse>(variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, TData>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
235235
...options,
236236
...queryOptions
237237
}); };
@@ -357,7 +357,7 @@ describe("generateReactQueryComponents", () => {
357357
/**
358358
* Get all the pets
359359
*/
360-
export const useListPets = (variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, ListPetsResponse>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
360+
export const useListPets = <TData = ListPetsResponse>(variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, TData>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
361361
...options,
362362
...queryOptions
363363
}); };
@@ -454,7 +454,7 @@ describe("generateReactQueryComponents", () => {
454454
/**
455455
* Get all the pets
456456
*/
457-
export const useListPets = (variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, ListPetsResponse>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
457+
export const useListPets = <TData = ListPetsResponse>(variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, TData>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
458458
...options,
459459
...queryOptions
460460
}); };
@@ -1162,7 +1162,7 @@ describe("generateReactQueryComponents", () => {
11621162
/**
11631163
* Get all the pets
11641164
*/
1165-
export const useListPets = (variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, ListPetsResponse>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
1165+
export const useListPets = <TData = ListPetsResponse>(variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, ListPetsError, TData>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, ListPetsError, TData>(queryKeyFn({ path: \\"/pets\\", operationId: \\"listPets\\", variables }), () => fetchListPets({ ...fetcherOptions, ...variables }), {
11661166
...options,
11671167
...queryOptions
11681168
}); };

plugins/typescript/src/generators/generateReactQueryComponents.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ const createQueryHook = ({
462462
undefined,
463463
f.createArrowFunction(
464464
undefined,
465-
undefined,
465+
[f.createTypeParameterDeclaration("TData", undefined, dataType)],
466466
[
467467
f.createParameterDeclaration(
468468
undefined,
@@ -527,7 +527,14 @@ const createQueryHook = ({
527527
f.createIdentifier("reactQuery"),
528528
f.createIdentifier("useQuery")
529529
),
530-
[dataType, errorType, dataType],
530+
[
531+
dataType,
532+
errorType,
533+
f.createTypeReferenceNode(
534+
f.createIdentifier("TData"),
535+
[]
536+
),
537+
],
531538
[
532539
f.createCallExpression(
533540
f.createIdentifier("queryKeyFn"),
@@ -608,7 +615,11 @@ const createUseQueryOptionsType = (
608615
f.createIdentifier("reactQuery"),
609616
f.createIdentifier("UseQueryOptions")
610617
),
611-
[dataType, errorType, dataType]
618+
[
619+
dataType,
620+
errorType,
621+
f.createTypeReferenceNode(f.createIdentifier("TData"), []),
622+
]
612623
),
613624
f.createUnionTypeNode([
614625
f.createLiteralTypeNode(f.createStringLiteral("queryKey")),

0 commit comments

Comments
 (0)