-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue-query): add support for infiniteQueryOptions (#7257)
* feat(vue-query): ✨ add support for infiniteQueryOptions * test(vue-query): ✅ add infiniteQueryOptions types tests * test(vue-query): ✅ add test for infiniteQueryOptions * test: ✅ add more infiniteQueryOptions test * fix: ✅ fix failing test
- Loading branch information
1 parent
4e1a04b
commit c73eb30
Showing
5 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
packages/vue-query/src/__tests__/infiniteQueryOptions.types.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import { QueryClient } from '@tanstack/query-core' | ||
import { reactive } from 'vue-demi' | ||
import { infiniteQueryOptions } from '../infiniteQueryOptions' | ||
import { useInfiniteQuery } from '../useInfiniteQuery' | ||
import { type Equal, type Expect, doNotExecute } from './test-utils' | ||
import type { InfiniteData, dataTagSymbol } from '@tanstack/query-core' | ||
|
||
describe('infiniteQueryOptions', () => { | ||
it('should not allow excess properties', () => { | ||
doNotExecute(() => | ||
infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('data'), | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
// @ts-expect-error this is a good error, because stallTime does not exist! | ||
stallTime: 1000, | ||
}), | ||
) | ||
}) | ||
it('should infer types for callbacks', () => { | ||
doNotExecute(() => | ||
infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('data'), | ||
staleTime: 1000, | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
select: (data) => { | ||
const result: Expect< | ||
Equal<InfiniteData<string, number>, typeof data> | ||
> = true | ||
|
||
return result | ||
}, | ||
}), | ||
) | ||
}) | ||
it('should work when passed to useInfiniteQuery', () => { | ||
doNotExecute(() => { | ||
const options = infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('string'), | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
}) | ||
|
||
const { data } = reactive(useInfiniteQuery(options)) | ||
|
||
const result: Expect< | ||
Equal<typeof data, InfiniteData<string, unknown> | undefined> | ||
> = true | ||
|
||
return result | ||
}) | ||
}) | ||
it('should tag the queryKey with the result type of the QueryFn', () => { | ||
doNotExecute(() => { | ||
const { queryKey } = infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('string'), | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
}) | ||
|
||
const result: Expect< | ||
Equal<(typeof queryKey)[typeof dataTagSymbol], InfiniteData<string>> | ||
> = true | ||
|
||
return result | ||
}) | ||
}) | ||
it('should tag the queryKey even if no promise is returned', () => { | ||
doNotExecute(() => { | ||
const { queryKey } = infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => 'string', | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
}) | ||
|
||
const result: Expect< | ||
Equal<(typeof queryKey)[typeof dataTagSymbol], InfiniteData<string>> | ||
> = true | ||
|
||
return result | ||
}) | ||
}) | ||
it('should tag the queryKey with the result type of the QueryFn if select is used', () => { | ||
doNotExecute(() => { | ||
const { queryKey } = infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('string'), | ||
select: (data) => data.pages, | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
}) | ||
|
||
const result: Expect< | ||
Equal<(typeof queryKey)[typeof dataTagSymbol], InfiniteData<string>> | ||
> = true | ||
|
||
return result | ||
}) | ||
}) | ||
it('should return the proper type when passed to getQueryData', () => { | ||
doNotExecute(() => { | ||
const { queryKey } = infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('string'), | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
}) | ||
|
||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData(queryKey) | ||
|
||
const result: Expect< | ||
Equal<typeof data, InfiniteData<string, unknown> | undefined> | ||
> = true | ||
|
||
return result | ||
}) | ||
}) | ||
it('should properly type when passed to setQueryData', () => { | ||
doNotExecute(() => { | ||
const { queryKey } = infiniteQueryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => Promise.resolve('string'), | ||
getNextPageParam: () => 1, | ||
initialPageParam: 1, | ||
}) | ||
|
||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, (prev) => { | ||
expectTypeOf(prev).toEqualTypeOf< | ||
InfiniteData<string, unknown> | undefined | ||
>() | ||
return prev | ||
}) | ||
|
||
const result: Expect< | ||
Equal<typeof data, InfiniteData<string, unknown> | undefined> | ||
> = true | ||
|
||
return result | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { DataTag } from '@tanstack/query-core' | ||
import type { InfiniteData } from '@tanstack/query-core' | ||
import type { UseInfiniteQueryOptions } from './useInfiniteQuery' | ||
import type { DefaultError, QueryKey } from '@tanstack/query-core' | ||
|
||
export type UndefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError = DefaultError, | ||
TData = InfiniteData<TQueryFnData>, | ||
TQueryKey extends QueryKey = QueryKey, | ||
TPageParam = unknown, | ||
> = UseInfiniteQueryOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryFnData, | ||
TQueryKey, | ||
TPageParam | ||
> & { | ||
initialData?: undefined | ||
} | ||
|
||
type NonUndefinedGuard<T> = T extends undefined ? never : T | ||
|
||
export type DefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError = DefaultError, | ||
TData = InfiniteData<TQueryFnData>, | ||
TQueryKey extends QueryKey = QueryKey, | ||
TPageParam = unknown, | ||
> = UseInfiniteQueryOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryFnData, | ||
TQueryKey, | ||
TPageParam | ||
> & { | ||
initialData: | ||
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>> | ||
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>) | ||
} | ||
|
||
export function infiniteQueryOptions< | ||
TQueryFnData, | ||
TError = DefaultError, | ||
TData = InfiniteData<TQueryFnData>, | ||
TQueryKey extends QueryKey = QueryKey, | ||
TPageParam = unknown, | ||
>( | ||
options: UndefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryKey, | ||
TPageParam | ||
>, | ||
): UndefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryKey, | ||
TPageParam | ||
> & { | ||
queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>> | ||
} | ||
|
||
export function infiniteQueryOptions< | ||
TQueryFnData, | ||
TError = DefaultError, | ||
TData = InfiniteData<TQueryFnData>, | ||
TQueryKey extends QueryKey = QueryKey, | ||
TPageParam = unknown, | ||
>( | ||
options: DefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryKey, | ||
TPageParam | ||
>, | ||
): DefinedInitialDataInfiniteOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryKey, | ||
TPageParam | ||
> & { | ||
queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>> | ||
} | ||
|
||
export function infiniteQueryOptions(options: unknown) { | ||
return options | ||
} |