Skip to content

Commit 0a7fd6d

Browse files
authored
feat!: Inject query options in context (#31)
* Invert the options spreading, so we can override from the context * Pass the useQuery options to the context for consolidation * Fix context template
1 parent dcc87e4 commit 0a7fd6d

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ describe("generateReactQueryComponents", () => {
104104
/**
105105
* Get all the pets
106106
*/
107-
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
108-
...queryOptions,
109-
...options
107+
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
108+
...options,
109+
...queryOptions
110110
}); };
111111
"
112112
`);
@@ -216,9 +216,9 @@ describe("generateReactQueryComponents", () => {
216216
/**
217217
* Get all the pets
218218
*/
219-
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
220-
...queryOptions,
221-
...options
219+
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
220+
...options,
221+
...queryOptions
222222
}); };
223223
"
224224
`);
@@ -332,9 +332,9 @@ describe("generateReactQueryComponents", () => {
332332
/**
333333
* Get all the pets
334334
*/
335-
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
336-
...queryOptions,
337-
...options
335+
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
336+
...options,
337+
...queryOptions
338338
}); };
339339
"
340340
`);
@@ -419,9 +419,9 @@ describe("generateReactQueryComponents", () => {
419419
/**
420420
* Get all the pets
421421
*/
422-
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
423-
...queryOptions,
424-
...options
422+
export const useListPets = (queryKey: reactQuery.QueryKey, variables: ListPetsVariables, options?: Omit<reactQuery.UseQueryOptions<ListPetsResponse, undefined, ListPetsResponse>, \\"queryKey\\" | \\"queryFn\\">) => { const { fetcherOptions, queryOptions, queryKeyFn } = usePetstoreContext(options); return reactQuery.useQuery<ListPetsResponse, undefined, ListPetsResponse>(queryKeyFn(queryKey), () => fetchListPets({ ...fetcherOptions, ...variables }), {
423+
...options,
424+
...queryOptions
425425
}); };
426426
"
427427
`);

plugins/typescript/src/generators/generateReactQueryComponents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ const createQueryHook = ({
438438
f.createCallExpression(
439439
f.createIdentifier(contextHookName),
440440
undefined,
441-
[]
441+
[f.createIdentifier("options")]
442442
)
443443
),
444444
],
@@ -485,10 +485,10 @@ const createQueryHook = ({
485485
f.createObjectLiteralExpression(
486486
[
487487
f.createSpreadAssignment(
488-
f.createIdentifier("queryOptions")
488+
f.createIdentifier("options")
489489
),
490490
f.createSpreadAssignment(
491-
f.createIdentifier("options")
491+
f.createIdentifier("queryOptions")
492492
),
493493
],
494494
true

plugins/typescript/src/templates/context.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { pascal } from "case";
22

33
export const getContext = (prefix: string) =>
4-
`import type { QueryKey } from "react-query";
4+
`import type { QueryKey, UseQueryOptions } from "react-query";
55
66
export type ${pascal(prefix)}Context = {
77
fetcherOptions: {
@@ -29,8 +29,17 @@ export const getContext = (prefix: string) =>
2929
3030
/**
3131
* Context injected into every react-query hook wrappers
32+
*
33+
* @param queryOptions options from the useQuery wrapper
3234
*/
33-
export const use${pascal(prefix)}Context = (): ${pascal(prefix)}Context => {
35+
export function use${pascal(prefix)}Context<
36+
TQueryFnData = unknown,
37+
TError = unknown,
38+
TData = TQueryFnData,
39+
TQueryKey extends QueryKey = QueryKey
40+
>(
41+
queryOptions?: Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryKey' | 'queryFn'>
42+
): ${pascal(prefix)}Context {
3443
return {
3544
fetcherOptions: {},
3645
queryOptions: {},

0 commit comments

Comments
 (0)