11import { Instance , SnapshotIn } from 'mobx-state-tree' ;
22import { 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' ;
49import { Context } from './QueryClientProvider' ;
510import { QueryClient } from './QueryClient' ;
611import { EmptyPagination , EmptyRequest , QueryObserver } from './MstQueryHandler' ;
@@ -16,7 +21,12 @@ function mergeWithDefaultOptions(key: string, options: any, queryClient: QueryCl
1621type QueryOptions < T extends Instance < QueryReturnType > > = {
1722 request ?: SnapshotIn < T [ 'variables' ] [ 'request' ] > ;
1823 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 ) ;
2030 staleTime ?: number ;
2131 enabled ?: boolean ;
2232 initialData ?: any ;
@@ -26,7 +36,7 @@ type QueryOptions<T extends Instance<QueryReturnType>> = {
2636
2737export function useQuery < T extends Instance < QueryReturnType > > (
2838 query : T ,
29- options : QueryOptions < T > = { }
39+ options : QueryOptions < T > = { } ,
3040) {
3141 const [ observer , setObserver ] = useState ( ( ) => new QueryObserver ( query , true ) ) ;
3242
@@ -36,7 +46,9 @@ export function useQuery<T extends Instance<QueryReturnType>>(
3646 ( options as any ) . request = options . request ?? EmptyRequest ;
3747
3848 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+ ) ;
4052 }
4153
4254 useEffect ( ( ) => {
@@ -54,7 +66,7 @@ export function useQuery<T extends Instance<QueryReturnType>>(
5466 } , [ options ] ) ;
5567
5668 return {
57- data : query . data as typeof query [ 'data' ] ,
69+ data : query . data as ( typeof query ) [ 'data' ] ,
5870 dataUpdatedAt : query . __MstQueryHandler . cachedAt ?. getTime ( ) ,
5971 error : query . error ,
6072 isFetched : query . isFetched ,
@@ -71,7 +83,15 @@ type InfiniteQueryOptions<T extends Instance<InfiniteQueryReturnType>> = {
7183 request ?: SnapshotIn < T [ 'variables' ] [ 'request' ] > ;
7284 pagination ?: SnapshotIn < T [ 'variables' ] [ 'pagination' ] > ;
7385 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 ) ;
7595 staleTime ?: number ;
7696 enabled ?: boolean ;
7797 initialData ?: any ;
@@ -81,7 +101,7 @@ type InfiniteQueryOptions<T extends Instance<InfiniteQueryReturnType>> = {
81101
82102export function useInfiniteQuery < T extends Instance < InfiniteQueryReturnType > > (
83103 query : T ,
84- options : InfiniteQueryOptions < T > = { }
104+ options : InfiniteQueryOptions < T > = { } ,
85105) {
86106 const [ observer , setObserver ] = useState ( ( ) => new QueryObserver ( query , true ) ) ;
87107
@@ -90,9 +110,11 @@ export function useInfiniteQuery<T extends Instance<InfiniteQueryReturnType>>(
90110
91111 ( options as any ) . request = options . request ?? EmptyRequest ;
92112 ( options as any ) . pagination = options . pagination ?? EmptyPagination ;
93-
113+
94114 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+ ) ;
96118 }
97119
98120 useEffect ( ( ) => {
@@ -110,7 +132,7 @@ export function useInfiniteQuery<T extends Instance<InfiniteQueryReturnType>>(
110132 } , [ options ] ) ;
111133
112134 return {
113- data : query . data as typeof query [ 'data' ] ,
135+ data : query . data as ( typeof query ) [ 'data' ] ,
114136 dataUpdatedAt : query . __MstQueryHandler . cachedAt ?. getTime ( ) ,
115137 error : query . error ,
116138 isFetched : query . isFetched ,
@@ -131,7 +153,7 @@ type MutationOptions<T extends Instance<MutationReturnType>> = {
131153
132154export function useMutation < T extends Instance < MutationReturnType > > (
133155 mutation : T ,
134- options : MutationOptions < T > = { }
156+ options : MutationOptions < T > = { } ,
135157) {
136158 const [ observer , setObserver ] = useState ( ( ) => new QueryObserver ( mutation , false ) ) ;
137159
@@ -149,7 +171,7 @@ export function useMutation<T extends Instance<MutationReturnType>>(
149171 } , [ options ] ) ;
150172
151173 const result = {
152- data : mutation . data as typeof mutation [ 'data' ] ,
174+ data : mutation . data as ( typeof mutation ) [ 'data' ] ,
153175 error : mutation . error ,
154176 isLoading : mutation . isLoading ,
155177 mutation,
@@ -162,7 +184,7 @@ export function useMutation<T extends Instance<MutationReturnType>>(
162184 } ) => {
163185 const result = mutation . mutate ( { ...params , ...options } as any ) ;
164186 return result as Promise < { data : T [ 'data' ] ; error : any ; result : TResult } > ;
165- }
187+ } ,
166188 ) ;
167189
168190 return [ mutate , result ] as [ typeof mutate , typeof result ] ;
@@ -181,7 +203,7 @@ type UseVolatileQueryOptions<T extends Instance<QueryReturnType>> = QueryOptions
181203} ;
182204
183205export function useVolatileQuery (
184- options : UseVolatileQueryOptions < Instance < typeof VolatileQuery > > = { }
206+ options : UseVolatileQueryOptions < Instance < typeof VolatileQuery > > = { } ,
185207) {
186208 const queryClient = useContext ( Context ) ! as QueryClient < any > ;
187209 const query = useRefQuery ( VolatileQuery , queryClient ) ;
0 commit comments