Skip to content

Commit b2cfc2c

Browse files
committed
feat(preact-query): use new methods for usePrefetchQuery plus tests
1 parent 4006613 commit b2cfc2c

9 files changed

Lines changed: 76 additions & 63 deletions

.changeset/cute-cloths-switch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/preact-query': minor
3+
---
4+
5+
update usePrefetchQuery to use new methods

packages/preact-query/src/__tests__/infiniteQueryOptions.test-d.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ describe('infiniteQueryOptions', () => {
6464

6565
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
6666
})
67+
it('should work when passed to infiniteQuery', async () => {
68+
const options = infiniteQueryOptions({
69+
queryKey: ['key'],
70+
queryFn: () => Promise.resolve('string'),
71+
getNextPageParam: () => 1,
72+
initialPageParam: 1,
73+
})
74+
75+
const data = await new QueryClient().infiniteQuery(options)
76+
77+
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
78+
})
79+
it('should work when passed to infiniteQuery with select', async () => {
80+
const options = infiniteQueryOptions({
81+
queryKey: ['key'],
82+
queryFn: () => Promise.resolve('string'),
83+
getNextPageParam: () => 1,
84+
initialPageParam: 1,
85+
select: (data) => data.pages,
86+
})
87+
88+
const data = await new QueryClient().infiniteQuery(options)
89+
90+
expectTypeOf(data).toEqualTypeOf<Array<string>>()
91+
})
6792
it('should work when passed to fetchInfiniteQuery', async () => {
6893
const options = infiniteQueryOptions({
6994
queryKey: queryKey(),

packages/preact-query/src/__tests__/queryOptions.test-d.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,25 @@ describe('queryOptions', () => {
5757
const { data } = useSuspenseQuery(options)
5858
expectTypeOf(data).toEqualTypeOf<number>()
5959
})
60+
it('should work when passed to query', async () => {
61+
const options = queryOptions({
62+
queryKey: ['key'],
63+
queryFn: () => Promise.resolve(5),
64+
})
6065

66+
const data = await new QueryClient().query(options)
67+
expectTypeOf(data).toEqualTypeOf<number>()
68+
})
69+
it('should work when passed to query with select', async () => {
70+
const options = queryOptions({
71+
queryKey: ['key'],
72+
queryFn: () => Promise.resolve(5),
73+
select: (data) => data.toString(),
74+
})
75+
76+
const data = await new QueryClient().query(options)
77+
expectTypeOf(data).toEqualTypeOf<string>()
78+
})
6179
it('should work when passed to fetchQuery', async () => {
6280
const options = queryOptions({
6381
queryKey: queryKey(),

packages/preact-query/src/__tests__/useInfiniteQuery.test-d.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ describe('pageParam', () => {
3939
})
4040
})
4141

42+
it('initialPageParam should define type of param passed to queryFunctionContext for infiniteQuery', () => {
43+
const queryClient = new QueryClient()
44+
queryClient.infiniteQuery({
45+
queryKey: ['key'],
46+
queryFn: ({ pageParam }) => {
47+
expectTypeOf(pageParam).toEqualTypeOf<number>()
48+
return Promise.resolve(pageParam)
49+
},
50+
initialPageParam: 1,
51+
})
52+
})
53+
4254
it('initialPageParam should define type of param passed to queryFunctionContext for prefetchInfiniteQuery', () => {
4355
const queryClient = new QueryClient()
4456
queryClient.prefetchInfiniteQuery({

packages/preact-query/src/__tests__/usePrefetchInfiniteQuery.test-d.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('usePrefetchInfiniteQuery', () => {
2525
)
2626
})
2727

28-
it('should not allow refetchInterval, enabled or throwOnError options', () => {
28+
it('should not allow refetchInterval or throwOnError options', () => {
2929
assertType(
3030
usePrefetchInfiniteQuery({
3131
queryKey: queryKey(),
@@ -37,17 +37,6 @@ describe('usePrefetchInfiniteQuery', () => {
3737
}),
3838
)
3939

40-
assertType(
41-
usePrefetchInfiniteQuery({
42-
queryKey: queryKey(),
43-
queryFn: () => Promise.resolve(5),
44-
initialPageParam: 1,
45-
getNextPageParam: () => 1,
46-
// @ts-expect-error TS2353
47-
enabled: true,
48-
}),
49-
)
50-
5140
assertType(
5241
usePrefetchInfiniteQuery({
5342
queryKey: queryKey(),
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { queryKey } from '@tanstack/query-test-utils'
22
import { assertType, describe, expectTypeOf, it } from 'vitest'
33

4-
import { skipToken, usePrefetchQuery } from '..'
4+
import { usePrefetchQuery } from '..'
55

66
describe('usePrefetchQuery', () => {
77
it('should return nothing', () => {
@@ -13,7 +13,7 @@ describe('usePrefetchQuery', () => {
1313
expectTypeOf(result).toEqualTypeOf<void>()
1414
})
1515

16-
it('should not allow refetchInterval, enabled or throwOnError options', () => {
16+
it('should not allow refetchInterval or throwOnError options', () => {
1717
assertType(
1818
usePrefetchQuery({
1919
queryKey: queryKey(),
@@ -23,15 +23,6 @@ describe('usePrefetchQuery', () => {
2323
}),
2424
)
2525

26-
assertType(
27-
usePrefetchQuery({
28-
queryKey: queryKey(),
29-
queryFn: () => Promise.resolve(5),
30-
// @ts-expect-error TS2345
31-
enabled: true,
32-
}),
33-
)
34-
3526
assertType(
3627
usePrefetchQuery({
3728
queryKey: queryKey(),
@@ -41,21 +32,4 @@ describe('usePrefetchQuery', () => {
4132
}),
4233
)
4334
})
44-
45-
it('should not allow skipToken in queryFn', () => {
46-
assertType(
47-
usePrefetchQuery({
48-
queryKey: queryKey(),
49-
// @ts-expect-error
50-
queryFn: skipToken,
51-
}),
52-
)
53-
assertType(
54-
usePrefetchQuery({
55-
queryKey: queryKey(),
56-
// @ts-expect-error
57-
queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
58-
}),
59-
)
60-
})
6135
})

packages/preact-query/src/types.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type {
44
DefinedInfiniteQueryObserverResult,
55
DefinedQueryObserverResult,
66
DistributiveOmit,
7-
FetchQueryOptions,
87
InfiniteQueryObserverOptions,
98
InfiniteQueryObserverResult,
109
MutateFunction,
@@ -45,21 +44,6 @@ export interface UseBaseQueryOptions<
4544
subscribed?: boolean
4645
}
4746

48-
export interface UsePrefetchQueryOptions<
49-
TQueryFnData = unknown,
50-
TError = DefaultError,
51-
TData = TQueryFnData,
52-
TQueryKey extends QueryKey = QueryKey,
53-
> extends OmitKeyof<
54-
FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
55-
'queryFn'
56-
> {
57-
queryFn?: Exclude<
58-
FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],
59-
SkipToken
60-
>
61-
}
62-
6347
export type AnyUseQueryOptions = UseQueryOptions<any, any, any, any>
6448
export interface UseQueryOptions<
6549
TQueryFnData = unknown,

packages/preact-query/src/usePrefetchInfiniteQuery.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { noop } from '@tanstack/query-core'
12
import type {
23
DefaultError,
3-
FetchInfiniteQueryOptions,
44
QueryClient,
55
QueryKey,
6+
InfiniteQueryExecuteOptions,
67
} from '@tanstack/query-core'
78

89
import { useQueryClient } from './QueryClientProvider'
@@ -14,7 +15,7 @@ export function usePrefetchInfiniteQuery<
1415
TQueryKey extends QueryKey = QueryKey,
1516
TPageParam = unknown,
1617
>(
17-
options: FetchInfiniteQueryOptions<
18+
options: InfiniteQueryExecuteOptions<
1819
TQueryFnData,
1920
TError,
2021
TData,
@@ -26,6 +27,6 @@ export function usePrefetchInfiniteQuery<
2627
const client = useQueryClient(queryClient)
2728

2829
if (!client.getQueryState(options.queryKey)) {
29-
client.prefetchInfiniteQuery(options)
30+
void client.infiniteQuery(options).then(noop).catch(noop)
3031
}
3132
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
1+
import { noop } from '@tanstack/query-core'
2+
import type {
3+
DefaultError,
4+
QueryClient,
5+
QueryKey,
6+
QueryExecuteOptions,
7+
} from '@tanstack/query-core'
28

39
import { useQueryClient } from './QueryClientProvider'
4-
import type { UsePrefetchQueryOptions } from './types'
510

611
export function usePrefetchQuery<
712
TQueryFnData = unknown,
813
TError = DefaultError,
914
TData = TQueryFnData,
1015
TQueryKey extends QueryKey = QueryKey,
1116
>(
12-
options: UsePrefetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
17+
options: QueryExecuteOptions<TQueryFnData, TError, TData, TQueryKey>,
1318
queryClient?: QueryClient,
1419
) {
1520
const client = useQueryClient(queryClient)
1621

1722
if (!client.getQueryState(options.queryKey)) {
18-
client.prefetchQuery(options)
23+
void client.fetchQuery(options).then(noop).catch(noop)
1924
}
2025
}

0 commit comments

Comments
 (0)