@@ -79,9 +79,7 @@ export interface MutationHooks<
79
79
useMutation : UseMutation < Definition >
80
80
}
81
81
82
- type IdleState < Arg > = Arg extends SkipToken
83
- ? { isSkipped : true }
84
- : { isSkipped : boolean }
82
+ type SkippedState < Skipped extends boolean > = { isSkipped : Skipped }
85
83
86
84
/**
87
85
* A React hook that automatically triggers fetches of data from an endpoint, 'subscribes' the component to the cached data, and reads the request status and cached data from the Redux store. The component will re-render as the loading status changes and the data becomes available.
@@ -98,16 +96,43 @@ type IdleState<Arg> = Arg extends SkipToken
98
96
* - Returns the latest request status and cached data from the Redux store
99
97
* - Re-renders as the request status changes and data becomes available
100
98
*/
101
- export type UseQuery < D extends QueryDefinition < any , any , any , any > > = <
102
- R extends Record < string , any > = UseQueryStateDefaultResult < D > ,
103
- Arg extends QueryArgFrom < D > | SkipToken = QueryArgFrom < D > | SkipToken
104
- > (
105
- arg : QueryArgFrom < D > | SkipToken ,
106
- options ?: UseQuerySubscriptionOptions & UseQueryStateOptions < D , R >
107
- ) => UseQueryStateResult < D , R > &
108
- ReturnType < UseQuerySubscription < D > > &
109
- Suspendable &
110
- IdleState < Arg >
99
+ export interface UseQuery < D extends QueryDefinition < any , any , any , any > > {
100
+ // arg provided
101
+ < R extends Record < string , any > = UseQueryStateDefaultResult < D > > (
102
+ arg : QueryArgFrom < D > ,
103
+ options ?: UseQuerySubscriptionOptions & UseQueryStateOptions < D , R >
104
+ ) : UseQueryStateResult < D , R > &
105
+ ReturnType < UseQuerySubscription < D > > &
106
+ Suspendable &
107
+ SkippedState < false >
108
+ // skipped query
109
+ < R extends Record < string , any > = UseQueryStateDefaultResult < D > > (
110
+ arg : SkipToken ,
111
+ options ?: UseQuerySubscriptionOptions & UseQueryStateOptions < D , R >
112
+ ) : UseQueryStateResult < D , R > &
113
+ ReturnType < UseQuerySubscription < D > > &
114
+ Suspendable &
115
+ SkippedState < true >
116
+ < R extends Record < string , any > = UseQueryStateDefaultResult < D > > (
117
+ arg : QueryArgFrom < D > | SkipToken ,
118
+ options ?: UseQuerySubscriptionOptions & UseQueryStateOptions < D , R >
119
+ ) : UseQueryStateResult < D , R > &
120
+ ReturnType < UseQuerySubscription < D > > &
121
+ Suspendable &
122
+ SkippedState < boolean >
123
+ }
124
+
125
+ /**
126
+ * @internal
127
+ */
128
+ type UseQueryParams < D extends QueryDefinition < any , any , any , any > > = Parameters <
129
+ UseQuery < D >
130
+ >
131
+
132
+ /**
133
+ * @internal
134
+ */
135
+ type AnyQueryDefinition = QueryDefinition < any , any , any , any , any >
111
136
112
137
interface UseQuerySubscriptionOptions extends SubscriptionOptions {
113
138
/**
@@ -551,7 +576,7 @@ const createSuspendablePromise = <
551
576
Definitions ,
552
577
Key
553
578
> ) : Suspendable [ 'getSuspendablePromise' ] => {
554
- const retry = ( ) => {
579
+ const fetchOnce = ( ) => {
555
580
prefetch ( args , {
556
581
force : true ,
557
582
} )
@@ -565,27 +590,19 @@ const createSuspendablePromise = <
565
590
let pendingPromise = api . util . getRunningOperationPromise ( name , args )
566
591
567
592
if ( ! pendingPromise ) {
568
- prefetch ( args , {
569
- force : true ,
570
- } )
593
+ fetchOnce ( )
571
594
572
595
pendingPromise = api . util . getRunningOperationPromise (
573
596
name as any ,
574
597
args
575
598
)
576
-
577
- if ( ! pendingPromise ) {
578
- throw new Error (
579
- `[rtk-query][react]: invalid state error, expected getRunningOperationPromise(${ name } , ${ queryStateResults . requestId } ) to be defined`
580
- )
581
- }
582
599
}
583
600
return pendingPromise
584
601
} else if ( queryStateResults . isError && ! queryStateResults . isFetching ) {
585
602
throw new SuspenseQueryError (
586
603
queryStateResults . error ,
587
604
queryStateResults . endpointName + '' ,
588
- retry
605
+ fetchOnce
589
606
)
590
607
}
591
608
}
@@ -938,7 +955,10 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
938
955
[ trigger , queryStateResults , info ]
939
956
)
940
957
} ,
941
- useQuery ( arg , options ) {
958
+ useQuery (
959
+ arg : UseQueryParams < AnyQueryDefinition > [ '0' ] ,
960
+ options : UseQueryParams < AnyQueryDefinition > [ '1' ]
961
+ ) {
942
962
const isSkipped : boolean = arg === skipToken || ! ! options ?. skip
943
963
const querySubscriptionResults = useQuerySubscription ( arg , options )
944
964
const queryStateResults = useQueryState ( arg , {
0 commit comments