Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-query): add usePrefetchQuery, usePrefetchInfiniteQuery #1252

Merged
merged 4 commits into from
Sep 4, 2024
Merged
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
6 changes: 6 additions & 0 deletions .changeset/shy-wolves-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@suspensive/react-query-4": minor
"@suspensive/react-query-5": minor
---

feat(react-query): add usePrefetchQuery, usePrefetchInfiniteQuery
2 changes: 2 additions & 0 deletions packages/react-query-4/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export { useSuspenseQueries } from './useSuspenseQueries'
export type { SuspenseQueriesOptions, SuspenseQueriesResults } from './useSuspenseQueries'
export { useSuspenseInfiniteQuery } from './useSuspenseInfiniteQuery'
export type { UseSuspenseInfiniteQueryOptions, UseSuspenseInfiniteQueryResult } from './useSuspenseInfiniteQuery'
export { usePrefetchQuery } from './usePrefetchQuery'
export { usePrefetchInfiniteQuery } from './usePrefetchInfiniteQuery'

export { SuspenseQuery } from './SuspenseQuery'
export { SuspenseQueries } from './SuspenseQueries'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type InfiniteData, type UseInfiniteQueryResult, useInfiniteQuery, useQu
import { describe, expectTypeOf, it } from 'vitest'
import { infiniteQueryOptions } from './infiniteQueryOptions'
import { SuspenseInfiniteQuery } from './SuspenseInfiniteQuery'
import { usePrefetchInfiniteQuery } from './usePrefetchInfiniteQuery'
import { type UseSuspenseInfiniteQueryResult, useSuspenseInfiniteQuery } from './useSuspenseInfiniteQuery'

const infiniteQuery = {
Expand Down Expand Up @@ -77,7 +78,12 @@ describe('infiniteQueryOptions', () => {
</SuspenseInfiniteQuery>
))()
})

it('should be used with usePrefetchInfiniteQuery', () => {
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
expectTypeOf(usePrefetchInfiniteQuery(infiniteQuery.options1())).toMatchTypeOf<void>()
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
expectTypeOf(usePrefetchInfiniteQuery({ ...infiniteQuery.options1() })).toMatchTypeOf<void>()
})
it('should be used with useQueryClient', async () => {
const queryClient = useQueryClient()

Expand Down
9 changes: 7 additions & 2 deletions packages/react-query-4/src/queryOptions.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { type UseQueryResult, useQueries, useQuery, useQueryClient } from '@tans
import { describe, expectTypeOf, it } from 'vitest'
import { queryOptions } from './queryOptions'
import { SuspenseQuery } from './SuspenseQuery'
import { usePrefetchQuery } from './usePrefetchQuery'
import { useSuspenseQueries } from './useSuspenseQueries'
import { type UseSuspenseQueryResult, useSuspenseQuery } from './useSuspenseQuery'

Expand Down Expand Up @@ -56,7 +57,6 @@ describe('queryOptions', () => {
</SuspenseQuery>
))()
})

it('should be used with useQueries', () => {
const [query1, query2, query3] = useQueries({
queries: [
Expand Down Expand Up @@ -102,7 +102,12 @@ describe('queryOptions', () => {
expectTypeOf(query3).toEqualTypeOf<UseSuspenseQueryResult<string>>()
expectTypeOf(query3.data).toEqualTypeOf<string>()
})

it('should be used with usePrefetchQuery', () => {
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
expectTypeOf(usePrefetchQuery(query.options1())).toMatchTypeOf<void>()
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
expectTypeOf(usePrefetchQuery({ ...query.options1() })).toMatchTypeOf<void>()
})
it('should be used with useQueryClient', async () => {
const queryClient = useQueryClient()

Expand Down
14 changes: 14 additions & 0 deletions packages/react-query-4/src/usePrefetchInfiniteQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type FetchInfiniteQueryOptions, type QueryKey, useQueryClient } from '@tanstack/react-query'

export function usePrefetchInfiniteQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(options: FetchInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey>) {
const queryClient = useQueryClient()

if (typeof options.queryKey !== 'undefined' && !queryClient.getQueryState(options.queryKey)) {
queryClient.prefetchInfiniteQuery(options)
}
}
14 changes: 14 additions & 0 deletions packages/react-query-4/src/usePrefetchQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type FetchQueryOptions, type QueryKey, useQueryClient } from '@tanstack/react-query'

export function usePrefetchQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>) {
const queryClient = useQueryClient()

if (typeof options.queryKey !== 'undefined' && !queryClient.getQueryState(options.queryKey)) {
queryClient.prefetchQuery(options)
}
}
2 changes: 2 additions & 0 deletions packages/react-query-5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export { useSuspenseQueries } from './useSuspenseQueries'
export type { SuspenseQueriesOptions, SuspenseQueriesResults } from './useSuspenseQueries'
export { useSuspenseInfiniteQuery } from './useSuspenseInfiniteQuery'
export type { UseSuspenseInfiniteQueryOptions, UseSuspenseInfiniteQueryResult } from './useSuspenseInfiniteQuery'
export { usePrefetchQuery } from './usePrefetchQuery'
export { usePrefetchInfiniteQuery } from './usePrefetchInfiniteQuery'

export { SuspenseQuery } from './SuspenseQuery'
export { SuspenseQueries } from './SuspenseQueries'
Expand Down
12 changes: 12 additions & 0 deletions packages/react-query-5/src/usePrefetchInfiniteQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { usePrefetchInfiniteQuery as original_usePrefetchInfiniteQuery } from '@tanstack/react-query'

/**
* This feature is officially supported in \@tanstack/react-query@5, You can proceed with the migration.
* @deprecated Use `usePrefetchInfiniteQuery` from \@tanstack/react-query@5
* @example
* ```diff
* - import { usePrefetchInfiniteQuery } from '@suspensive/react-query'
* + import { usePrefetchInfiniteQuery } from '@tanstack/react-query'
* ```
*/
export const usePrefetchInfiniteQuery = original_usePrefetchInfiniteQuery
12 changes: 12 additions & 0 deletions packages/react-query-5/src/usePrefetchQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { usePrefetchQuery as original_usePrefetchQuery } from '@tanstack/react-query'

/**
* This feature is officially supported in \@tanstack/react-query@5, You can proceed with the migration.
* @deprecated Use `usePrefetchQuery` from \@tanstack/react-query@5
* @example
* ```diff
* - import { usePrefetchQuery } from '@suspensive/react-query'
* + import { usePrefetchQuery } from '@tanstack/react-query'
* ```
*/
export const usePrefetchQuery = original_usePrefetchQuery
2 changes: 2 additions & 0 deletions packages/react-query/src/scripts/utils/package.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const version4APIs = [
'useSuspenseQuery',
'useSuspenseQueries',
'useSuspenseInfiniteQuery',
'usePrefetchQuery',
'usePrefetchInfiniteQuery',
'queryOptions',
'infiniteQueryOptions',
'SuspenseQuery',
Expand Down
2 changes: 2 additions & 0 deletions packages/react-query/src/scripts/utils/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export function getTanStackReactQueryAPIs(majorVersionOfTanStackQuery: string):
'useSuspenseQuery',
'useSuspenseQueries',
'useSuspenseInfiniteQuery',
'usePrefetchQuery',
'usePrefetchInfiniteQuery',
'queryOptions',
'infiniteQueryOptions',
]
Expand Down
Loading