Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cute-cloths-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/preact-query': minor
---

update usePrefetchQuery to use new methods
5 changes: 5 additions & 0 deletions .changeset/true-cameras-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': minor
---

add query and infiniteQuery methods, deprecate old imperative methods
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ describe('infiniteQueryOptions', () => {

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
})
it('should work when passed to infiniteQuery', async () => {
const options = infiniteQueryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

const data = await new QueryClient().infiniteQuery(options)

expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
it('should work when passed to infiniteQuery with select', async () => {
const options = infiniteQueryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
select: (data) => data.pages,
})

const data = await new QueryClient().infiniteQuery(options)

expectTypeOf(data).toEqualTypeOf<Array<string>>()
})
it('should work when passed to fetchInfiniteQuery', async () => {
const options = infiniteQueryOptions({
queryKey: queryKey(),
Expand Down
18 changes: 18 additions & 0 deletions packages/preact-query/src/__tests__/queryOptions.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,25 @@ describe('queryOptions', () => {
const { data } = useSuspenseQuery(options)
expectTypeOf(data).toEqualTypeOf<number>()
})
it('should work when passed to query', async () => {
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

const data = await new QueryClient().query(options)
expectTypeOf(data).toEqualTypeOf<number>()
})
it('should work when passed to query with select', async () => {
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
select: (data) => data.toString(),
})

const data = await new QueryClient().query(options)
expectTypeOf(data).toEqualTypeOf<string>()
})
it('should work when passed to fetchQuery', async () => {
const options = queryOptions({
queryKey: queryKey(),
Expand Down
12 changes: 12 additions & 0 deletions packages/preact-query/src/__tests__/useInfiniteQuery.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ describe('pageParam', () => {
})
})

it('initialPageParam should define type of param passed to queryFunctionContext for infiniteQuery', () => {
const queryClient = new QueryClient()
queryClient.infiniteQuery({
queryKey: ['key'],
queryFn: ({ pageParam }) => {
expectTypeOf(pageParam).toEqualTypeOf<number>()
return Promise.resolve(pageParam)
},
initialPageParam: 1,
})
})

it('initialPageParam should define type of param passed to queryFunctionContext for prefetchInfiniteQuery', () => {
const queryClient = new QueryClient()
queryClient.prefetchInfiniteQuery({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('usePrefetchInfiniteQuery', () => {
)
})

it('should not allow refetchInterval, enabled or throwOnError options', () => {
it('should not allow refetchInterval or throwOnError options', () => {
assertType(
usePrefetchInfiniteQuery({
queryKey: queryKey(),
Expand All @@ -37,17 +37,6 @@ describe('usePrefetchInfiniteQuery', () => {
}),
)

assertType(
usePrefetchInfiniteQuery({
queryKey: queryKey(),
queryFn: () => Promise.resolve(5),
initialPageParam: 1,
getNextPageParam: () => 1,
// @ts-expect-error TS2353
enabled: true,
}),
)

assertType(
usePrefetchInfiniteQuery({
queryKey: queryKey(),
Expand Down
30 changes: 2 additions & 28 deletions packages/preact-query/src/__tests__/usePrefetchQuery.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { queryKey } from '@tanstack/query-test-utils'
import { assertType, describe, expectTypeOf, it } from 'vitest'

import { skipToken, usePrefetchQuery } from '..'
import { usePrefetchQuery } from '..'

describe('usePrefetchQuery', () => {
it('should return nothing', () => {
Expand All @@ -13,7 +13,7 @@ describe('usePrefetchQuery', () => {
expectTypeOf(result).toEqualTypeOf<void>()
})

it('should not allow refetchInterval, enabled or throwOnError options', () => {
it('should not allow refetchInterval or throwOnError options', () => {
assertType(
usePrefetchQuery({
queryKey: queryKey(),
Expand All @@ -23,15 +23,6 @@ describe('usePrefetchQuery', () => {
}),
)

assertType(
usePrefetchQuery({
queryKey: queryKey(),
queryFn: () => Promise.resolve(5),
// @ts-expect-error TS2345
enabled: true,
}),
)

assertType(
usePrefetchQuery({
queryKey: queryKey(),
Expand All @@ -41,21 +32,4 @@ describe('usePrefetchQuery', () => {
}),
)
})

it('should not allow skipToken in queryFn', () => {
assertType(
usePrefetchQuery({
queryKey: queryKey(),
// @ts-expect-error
queryFn: skipToken,
}),
)
assertType(
usePrefetchQuery({
queryKey: queryKey(),
// @ts-expect-error
queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
}),
)
})
})
16 changes: 0 additions & 16 deletions packages/preact-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
DefinedInfiniteQueryObserverResult,
DefinedQueryObserverResult,
DistributiveOmit,
FetchQueryOptions,
InfiniteQueryObserverOptions,
InfiniteQueryObserverResult,
MutateFunction,
Expand Down Expand Up @@ -45,21 +44,6 @@ export interface UseBaseQueryOptions<
subscribed?: boolean
}

export interface UsePrefetchQueryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> extends OmitKeyof<
FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
'queryFn'
> {
queryFn?: Exclude<
FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],
SkipToken
>
}

export type AnyUseQueryOptions = UseQueryOptions<any, any, any, any>
export interface UseQueryOptions<
TQueryFnData = unknown,
Expand Down
7 changes: 4 additions & 3 deletions packages/preact-query/src/usePrefetchInfiniteQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { noop } from '@tanstack/query-core'
import type {
DefaultError,
FetchInfiniteQueryOptions,
QueryClient,
QueryKey,
InfiniteQueryExecuteOptions,
} from '@tanstack/query-core'

import { useQueryClient } from './QueryClientProvider'
Expand All @@ -14,7 +15,7 @@ export function usePrefetchInfiniteQuery<
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
options: FetchInfiniteQueryOptions<
options: InfiniteQueryExecuteOptions<
TQueryFnData,
TError,
TData,
Expand All @@ -26,6 +27,6 @@ export function usePrefetchInfiniteQuery<
const client = useQueryClient(queryClient)

if (!client.getQueryState(options.queryKey)) {
client.prefetchInfiniteQuery(options)
void client.infiniteQuery(options).then(noop).catch(noop)
}
}
13 changes: 9 additions & 4 deletions packages/preact-query/src/usePrefetchQuery.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
import { noop } from '@tanstack/query-core'
import type {
DefaultError,
QueryClient,
QueryKey,
QueryExecuteOptions,
} from '@tanstack/query-core'

import { useQueryClient } from './QueryClientProvider'
import type { UsePrefetchQueryOptions } from './types'

export function usePrefetchQuery<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UsePrefetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
options: QueryExecuteOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
) {
const client = useQueryClient(queryClient)

if (!client.getQueryState(options.queryKey)) {
client.prefetchQuery(options)
void client.fetchQuery(options).then(noop).catch(noop)
}
}
Loading
Loading