Skip to content

Commit cabf324

Browse files
author
liaoxuan
committed
feat: add more utils
1 parent 369f827 commit cabf324

10 files changed

Lines changed: 685 additions & 36 deletions

README-zh_CN.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@
3636
- [安装](#installation)
3737
- [例子](#examples)
3838
- 使用
39-
- [createQuery](#createQuery)
40-
- [createInfiniteQuery](#createInfiniteQuery)
41-
- [createMutation](#createMutation)
39+
- [createQuery](#createquery)
40+
- [createInfiniteQuery](#createinfinitequery)
41+
- [createMutation](#createmutation)
42+
- [createImmutableQuery](#createimmutablequery)
43+
- [createSuspenseQuery](#createsuspensequery)
44+
- [createSuspenseInfiniteQuery](#createsuspenseinfinitequery)
4245
- [类型推导](#类型推导)
4346
- [注意事项](#注意事项)
4447
- [问题](#issues)
@@ -282,6 +285,68 @@ Returns
282285
- `setData: (updater: Updater<TData>, options?: SetDataOptions) => TData | undefined`
283286
- 它的参数与 `queryClient.setQueryData` 类似,但不需要传入 `queryKey`
284287

288+
## createImmutableQuery
289+
290+
如果资源是不可变的,即使我们再怎么重新请求也永远不会发生任何改变,那么我们可以禁用它的所有的自动重新请求。
291+
ReactQueryKit 提供了一个辅助函数 `createImmutableQuery` 来标记资源为不可变的:
292+
293+
```ts
294+
import { createImmutableQuery } from 'react-query-kit'
295+
296+
createImmutableQuery({
297+
...options,
298+
})
299+
300+
// 相当于
301+
createQuery({
302+
...options,
303+
refetchInterval: false,
304+
refetchOnMount: false,
305+
refetchOnReconnect: false,
306+
refetchOnWindowFocus: false,
307+
staleTime: Infinity,
308+
gcTime: Infinity,
309+
})
310+
```
311+
312+
## createSuspenseQuery
313+
314+
这与在查询配置中将 suspense 选项设置为 true 具有相同的效果,但在 TypeScript 中效果更好,因为 data 是有定义的(因为错误和加载状态由 Suspense 和 ErrorBoundaries 处理)。
315+
316+
```ts
317+
import { createSuspenseQuery } from 'react-query-kit'
318+
319+
createSuspenseQuery({
320+
...options,
321+
})
322+
323+
// 相当于
324+
createQuery({
325+
...options,
326+
enabled: true,
327+
suspense: true,
328+
throwOnError: true,
329+
})
330+
```
331+
332+
## createSuspenseInfiniteQuery
333+
334+
```ts
335+
import { createSuspenseInfiniteQuery } from 'react-query-kit'
336+
337+
createSuspenseInfiniteQuery({
338+
...options,
339+
})
340+
341+
// 相当于
342+
createInfiniteQuery({
343+
...options,
344+
enabled: true,
345+
suspense: true,
346+
throwOnError: true,
347+
})
348+
```
349+
285350
## createMutation
286351

287352
### Usage

README.md

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ English | [简体中文](./README-zh_CN.md)
3636
- [Installation](#installation)
3737
- [Examples](#examples)
3838
- Usage
39-
- [createQuery](#createQuery)
40-
- [createInfiniteQuery](#createInfiniteQuery)
41-
- [createMutation](#createMutation)
42-
- [Type inference](#Type-inference)
43-
- [Caution](#Caution)
39+
- [createQuery](#createquery)
40+
- [createInfiniteQuery](#createinfinitequery)
41+
- [createMutation](#createmutation)
42+
- [createImmutableQuery](#createimmutablequery)
43+
- [createSuspenseQuery](#createsuspensequery)
44+
- [createSuspenseInfiniteQuery](#createsuspenseinfinitequery)
45+
- [createMutation](#createmutation)
46+
- [Type inference](#type-inference)
47+
- [Caution](#caution)
4448
- [Issues](#issues)
4549
- [🐛 Bugs](#-bugs)
4650
- [💡 Feature Requests](#-feature-requests)
@@ -295,6 +299,68 @@ Returns
295299
- `setData: (updater: Updater<InfiniteData<TFnData>>, options?: SetDataOptions) => TData | undefined`
296300
- it's args similar with `queryClient.setQueryData` but without `queryKey`
297301

302+
## createImmutableQuery
303+
304+
If the resource is immutable, no matter how many times we request it again, it will never change. In this case, we can disable all automatic re-requests for it.
305+
ReactQueryKit provides a helper function `createImmutableQuery` to mark a resource as immutable.
306+
307+
```ts
308+
import { createImmutableQuery } from 'react-query-kit'
309+
310+
createImmutableQuery({
311+
...options,
312+
})
313+
314+
// equals to
315+
createQuery({
316+
...options,
317+
refetchInterval: false,
318+
refetchOnMount: false,
319+
refetchOnReconnect: false,
320+
refetchOnWindowFocus: false,
321+
staleTime: Infinity,
322+
gcTime: Infinity,
323+
})
324+
```
325+
326+
## createSuspenseQuery
327+
328+
This has the same effect as setting the `suspense` option to `true` in the query config, but it works better in TypeScript, because `data` is guaranteed to be defined (as errors and loading states are handled by Suspense- and ErrorBoundaries).
329+
330+
```ts
331+
import { createSuspenseQuery } from 'react-query-kit'
332+
333+
createSuspenseQuery({
334+
...options,
335+
})
336+
337+
// equals to
338+
createQuery({
339+
...options,
340+
enabled: true,
341+
suspense: true,
342+
throwOnError: true,
343+
})
344+
```
345+
346+
## createSuspenseInfiniteQuery
347+
348+
```ts
349+
import { createSuspenseInfiniteQuery } from 'react-query-kit'
350+
351+
createSuspenseInfiniteQuery({
352+
...options,
353+
})
354+
355+
// equals to
356+
createInfiniteQuery({
357+
...options,
358+
enabled: true,
359+
suspense: true,
360+
throwOnError: true,
361+
})
362+
```
363+
298364
## createMutation
299365

300366
### Usage

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@rollup/plugin-commonjs": "^22.0.2",
2828
"@rollup/plugin-node-resolve": "^13.2.1",
2929
"@rollup/plugin-replace": "^4.0.0",
30-
"@tanstack/react-query": "^5.0.0-alpha.19",
30+
"@tanstack/react-query": "^5.0.0-beta.9",
3131
"@testing-library/jest-dom": "^5.16.5",
3232
"@testing-library/react": "^14.0.0",
3333
"@types/jest": "^28.1.6",

src/createBaseQuery.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@ type QueryBaseHookOptions = Omit<
1919
AdditionalQueryHookOptions<any, any> & { context?: any }
2020

2121
export function createBaseQuery(
22-
options: any,
22+
initialOptions: any,
2323
useRQHook: (options: any, queryClient?: any) => any,
24-
queryClient?: any
24+
queryClient?: any,
25+
overrideOptions?: QueryBaseHookOptions
2526
): any {
2627
const {
2728
primaryKey,
2829
queryFn,
2930
queryKeyHashFn,
3031
useDefaultOptions,
3132
...defaultOptions
32-
} = options as CreateQueryOptions
33+
} = initialOptions as CreateQueryOptions
3334

3435
const getPrimaryKey = () => primaryKey
3536

@@ -41,6 +42,7 @@ export function createBaseQuery(
4142
...defaultOptions,
4243
...useDefaultOptions?.(),
4344
...options,
45+
...overrideOptions,
4446
} as QueryBaseHookOptions
4547

4648
const queryKey = getKey(variables)
@@ -50,18 +52,21 @@ export function createBaseQuery(
5052
mergedOptions.context ? { context: mergedOptions.context } : queryClient
5153
)
5254

53-
const queryOptions = {
54-
...mergedOptions,
55-
enabled:
56-
typeof enabled === 'function'
57-
? enabled(client.getQueryData(queryKey), variables)
58-
: enabled,
59-
queryKeyHashFn,
60-
queryFn,
61-
queryKey,
62-
}
55+
const result = useRQHook(
56+
{
57+
...mergedOptions,
58+
enabled:
59+
typeof enabled === 'function'
60+
? enabled(client.getQueryData(queryKey), variables)
61+
: enabled,
62+
queryKeyHashFn,
63+
queryFn,
64+
queryKey,
65+
},
66+
client
67+
)
6368

64-
return Object.assign(useRQHook(queryOptions, client), {
69+
return Object.assign(result, {
6570
queryKey,
6671
variables,
6772
setData: (updater: Updater<any, any>, setDataOptions?: SetDataOptions) =>

src/createImmutableQuery.tsx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import type { QueryClient, UseQueryOptions } from '@tanstack/react-query'
2+
import { useQuery } from '@tanstack/react-query'
3+
import { createBaseQuery } from './createBaseQuery'
4+
import type {
5+
AdditionalCreateOptions,
6+
CompatibleWithV4,
7+
ImmutableQueryHook,
8+
ImmutableQueryHookOptions,
9+
inferQueryKey,
10+
} from './types'
11+
12+
export interface CreateImmutableQueryOptions<
13+
TFnData,
14+
TVariables = any,
15+
Error = unknown
16+
> extends Omit<
17+
UseQueryOptions<TFnData, Error, TFnData, inferQueryKey<TVariables>>,
18+
| 'queryKey'
19+
| 'queryFn'
20+
| 'enabled'
21+
| 'select'
22+
| 'refetchInterval'
23+
| 'refetchIntervalInBackground'
24+
| 'refetchOnMount'
25+
| 'refetchOnReconnect'
26+
| 'refetchOnWindowFocus'
27+
| 'staleTime'
28+
| 'gcTime'
29+
| 'cacheTime'
30+
>,
31+
AdditionalCreateOptions<TFnData, TVariables> {}
32+
33+
export function createImmutableQuery<
34+
TFnData,
35+
TVariables = any,
36+
Error = unknown
37+
>(
38+
options: CreateImmutableQueryOptions<TFnData, TVariables, Error> & {
39+
useDefaultOptions: () => Omit<
40+
ImmutableQueryHookOptions<TFnData, Error, TFnData, TVariables>,
41+
'select'
42+
> & { variables: TVariables }
43+
},
44+
queryClient?: CompatibleWithV4<QueryClient, void>
45+
): ImmutableQueryHook<TFnData, TVariables, Error, TVariables | void>
46+
47+
export function createImmutableQuery<
48+
TFnData,
49+
TVariables = any,
50+
Error = unknown
51+
>(
52+
options: CreateImmutableQueryOptions<TFnData, TVariables, Error> & {
53+
useDefaultOptions: () => Omit<
54+
ImmutableQueryHookOptions<TFnData, Error, TFnData, TVariables>,
55+
'select' | 'variables'
56+
>
57+
},
58+
queryClient?: CompatibleWithV4<QueryClient, void>
59+
): ImmutableQueryHook<TFnData, TVariables, Error, TVariables>
60+
61+
export function createImmutableQuery<
62+
TFnData,
63+
TVariables = any,
64+
Error = unknown
65+
>(
66+
options: CreateImmutableQueryOptions<TFnData, TVariables, Error>,
67+
queryClient?: CompatibleWithV4<QueryClient, void>
68+
): ImmutableQueryHook<TFnData, TVariables, Error, TVariables>
69+
70+
export function createImmutableQuery(options: any, queryClient?: any) {
71+
return createBaseQuery(options, useQuery, queryClient, {
72+
refetchInterval: false,
73+
refetchOnMount: false,
74+
refetchOnReconnect: false,
75+
refetchOnWindowFocus: false,
76+
staleTime: Infinity,
77+
gcTime: Infinity,
78+
// compatible with ReactQuery v4
79+
// @ts-ignore
80+
cacheTime: Infinity,
81+
})
82+
}

0 commit comments

Comments
 (0)