1
1
import { Instance , SnapshotIn } from 'mobx-state-tree' ;
2
2
import { useContext , useEffect , useRef , useState } from 'react' ;
3
- import { VolatileQuery , MutationReturnType , QueryReturnType , InfiniteQueryReturnType } from './create' ;
3
+ import {
4
+ VolatileQuery ,
5
+ MutationReturnType ,
6
+ QueryReturnType ,
7
+ InfiniteQueryReturnType ,
8
+ } from './create' ;
4
9
import { Context } from './QueryClientProvider' ;
5
10
import { QueryClient } from './QueryClient' ;
6
11
import { EmptyPagination , EmptyRequest , QueryObserver } from './MstQueryHandler' ;
@@ -16,7 +21,12 @@ function mergeWithDefaultOptions(key: string, options: any, queryClient: QueryCl
16
21
type QueryOptions < T extends Instance < QueryReturnType > > = {
17
22
request ?: SnapshotIn < T [ 'variables' ] [ 'request' ] > ;
18
23
refetchOnMount ?: 'always' | 'never' | 'if-stale' ;
19
- refetchOnChanged ?: 'all' | 'request' | 'pagination' | 'none' ;
24
+ refetchOnChanged ?:
25
+ | 'all'
26
+ | 'request'
27
+ | 'pagination'
28
+ | 'none'
29
+ | ( ( options : { prevRequest : Exclude < T [ 'variables' ] [ 'request' ] , undefined > } ) => boolean ) ;
20
30
staleTime ?: number ;
21
31
enabled ?: boolean ;
22
32
initialData ?: any ;
@@ -26,7 +36,7 @@ type QueryOptions<T extends Instance<QueryReturnType>> = {
26
36
27
37
export function useQuery < T extends Instance < QueryReturnType > > (
28
38
query : T ,
29
- options : QueryOptions < T > = { }
39
+ options : QueryOptions < T > = { } ,
30
40
) {
31
41
const [ observer , setObserver ] = useState ( ( ) => new QueryObserver ( query , true ) ) ;
32
42
@@ -36,7 +46,9 @@ export function useQuery<T extends Instance<QueryReturnType>>(
36
46
( options as any ) . request = options . request ?? EmptyRequest ;
37
47
38
48
if ( ( query as any ) . isInfinite ) {
39
- throw new Error ( 'useQuery should be used with a query that does not have pagination. Use useInfiniteQuery instead.' ) ;
49
+ throw new Error (
50
+ 'useQuery should be used with a query that does not have pagination. Use useInfiniteQuery instead.' ,
51
+ ) ;
40
52
}
41
53
42
54
useEffect ( ( ) => {
@@ -54,7 +66,7 @@ export function useQuery<T extends Instance<QueryReturnType>>(
54
66
} , [ options ] ) ;
55
67
56
68
return {
57
- data : query . data as typeof query [ 'data' ] ,
69
+ data : query . data as ( typeof query ) [ 'data' ] ,
58
70
dataUpdatedAt : query . __MstQueryHandler . cachedAt ?. getTime ( ) ,
59
71
error : query . error ,
60
72
isFetched : query . isFetched ,
@@ -71,7 +83,15 @@ type InfiniteQueryOptions<T extends Instance<InfiniteQueryReturnType>> = {
71
83
request ?: SnapshotIn < T [ 'variables' ] [ 'request' ] > ;
72
84
pagination ?: SnapshotIn < T [ 'variables' ] [ 'pagination' ] > ;
73
85
refetchOnMount ?: 'always' | 'never' | 'if-stale' ;
74
- refetchOnChanged ?: 'all' | 'request' | 'pagination' | 'none' ;
86
+ refetchOnChanged ?:
87
+ | 'all'
88
+ | 'request'
89
+ | 'pagination'
90
+ | 'none'
91
+ | ( ( options : {
92
+ prevRequest : Exclude < T [ 'variables' ] [ 'request' ] , undefined > ;
93
+ prevPagination : Exclude < T [ 'variables' ] [ 'pagination' ] , undefined > ;
94
+ } ) => boolean ) ;
75
95
staleTime ?: number ;
76
96
enabled ?: boolean ;
77
97
initialData ?: any ;
@@ -81,7 +101,7 @@ type InfiniteQueryOptions<T extends Instance<InfiniteQueryReturnType>> = {
81
101
82
102
export function useInfiniteQuery < T extends Instance < InfiniteQueryReturnType > > (
83
103
query : T ,
84
- options : InfiniteQueryOptions < T > = { }
104
+ options : InfiniteQueryOptions < T > = { } ,
85
105
) {
86
106
const [ observer , setObserver ] = useState ( ( ) => new QueryObserver ( query , true ) ) ;
87
107
@@ -90,9 +110,11 @@ export function useInfiniteQuery<T extends Instance<InfiniteQueryReturnType>>(
90
110
91
111
( options as any ) . request = options . request ?? EmptyRequest ;
92
112
( options as any ) . pagination = options . pagination ?? EmptyPagination ;
93
-
113
+
94
114
if ( ! ( query as any ) . isInfinite ) {
95
- throw new Error ( 'useInfiniteQuery should be used with a query that has pagination. Use useQuery instead.' ) ;
115
+ throw new Error (
116
+ 'useInfiniteQuery should be used with a query that has pagination. Use useQuery instead.' ,
117
+ ) ;
96
118
}
97
119
98
120
useEffect ( ( ) => {
@@ -110,7 +132,7 @@ export function useInfiniteQuery<T extends Instance<InfiniteQueryReturnType>>(
110
132
} , [ options ] ) ;
111
133
112
134
return {
113
- data : query . data as typeof query [ 'data' ] ,
135
+ data : query . data as ( typeof query ) [ 'data' ] ,
114
136
dataUpdatedAt : query . __MstQueryHandler . cachedAt ?. getTime ( ) ,
115
137
error : query . error ,
116
138
isFetched : query . isFetched ,
@@ -131,7 +153,7 @@ type MutationOptions<T extends Instance<MutationReturnType>> = {
131
153
132
154
export function useMutation < T extends Instance < MutationReturnType > > (
133
155
mutation : T ,
134
- options : MutationOptions < T > = { }
156
+ options : MutationOptions < T > = { } ,
135
157
) {
136
158
const [ observer , setObserver ] = useState ( ( ) => new QueryObserver ( mutation , false ) ) ;
137
159
@@ -149,7 +171,7 @@ export function useMutation<T extends Instance<MutationReturnType>>(
149
171
} , [ options ] ) ;
150
172
151
173
const result = {
152
- data : mutation . data as typeof mutation [ 'data' ] ,
174
+ data : mutation . data as ( typeof mutation ) [ 'data' ] ,
153
175
error : mutation . error ,
154
176
isLoading : mutation . isLoading ,
155
177
mutation,
@@ -162,7 +184,7 @@ export function useMutation<T extends Instance<MutationReturnType>>(
162
184
} ) => {
163
185
const result = mutation . mutate ( { ...params , ...options } as any ) ;
164
186
return result as Promise < { data : T [ 'data' ] ; error : any ; result : TResult } > ;
165
- }
187
+ } ,
166
188
) ;
167
189
168
190
return [ mutate , result ] as [ typeof mutate , typeof result ] ;
@@ -181,7 +203,7 @@ type UseVolatileQueryOptions<T extends Instance<QueryReturnType>> = QueryOptions
181
203
} ;
182
204
183
205
export function useVolatileQuery (
184
- options : UseVolatileQueryOptions < Instance < typeof VolatileQuery > > = { }
206
+ options : UseVolatileQueryOptions < Instance < typeof VolatileQuery > > = { } ,
185
207
) {
186
208
const queryClient = useContext ( Context ) ! as QueryClient < any > ;
187
209
const query = useRefQuery ( VolatileQuery , queryClient ) ;
0 commit comments