@@ -13,11 +13,18 @@ import {
13
13
14
14
import { buildQueryOpts } from './build-query-opts' ;
15
15
16
+ /**
17
+ * Applies Omit over a union, while preserving its union-ness.
18
+ */
19
+ type DistributiveOmit < T , K extends keyof any > = T extends any
20
+ ? Omit < T , K >
21
+ : never ;
22
+
16
23
/**
17
24
* Represents the return value of the `useQuery` hook when `query` is expected to return
18
25
* a single row.
19
26
*/
20
- export type UseQuerySingleReturn < Result > = Omit <
27
+ export type UseQuerySingleReturn < Result > = DistributiveOmit <
21
28
UseReactQueryResult < PostgrestSingleResponse < Result > [ 'data' ] , PostgrestError > ,
22
29
'refetch'
23
30
> &
@@ -31,7 +38,7 @@ export type UseQuerySingleReturn<Result> = Omit<
31
38
* Represents the return value of the `useQuery` hook when `query` is expected to return
32
39
* either a single row or an empty response.
33
40
*/
34
- export type UseQueryMaybeSingleReturn < Result > = Omit <
41
+ export type UseQueryMaybeSingleReturn < Result > = DistributiveOmit <
35
42
UseReactQueryResult <
36
43
PostgrestMaybeSingleResponse < Result > [ 'data' ] ,
37
44
PostgrestError
@@ -48,7 +55,7 @@ export type UseQueryMaybeSingleReturn<Result> = Omit<
48
55
* Represents the return value of the `useQuery` hook when `query` is expected to return
49
56
* one or more rows.
50
57
*/
51
- export type UseQueryReturn < Result > = Omit <
58
+ export type UseQueryReturn < Result > = DistributiveOmit <
52
59
UseReactQueryResult < PostgrestResponse < Result > [ 'data' ] , PostgrestError > ,
53
60
'refetch'
54
61
> &
@@ -62,7 +69,7 @@ export type UseQueryReturn<Result> = Omit<
62
69
* Represents the return value of the `useQuery` hook when the type of the query response
63
70
* is not known.
64
71
*/
65
- export type UseQueryAnyReturn < Result > = Omit <
72
+ export type UseQueryAnyReturn < Result > = DistributiveOmit <
66
73
UseReactQueryResult < AnyPostgrestResponse < Result > [ 'data' ] , PostgrestError > ,
67
74
'refetch'
68
75
> &
@@ -131,12 +138,24 @@ function useQuery<Result>(
131
138
'queryKey' | 'queryFn'
132
139
> ,
133
140
) : UseQueryAnyReturn < Result > {
134
- const { data, ...rest } = useReactQuery <
135
- AnyPostgrestResponse < Result > ,
136
- PostgrestError
137
- > ( buildQueryOpts < Result > ( query , config ) ) ;
141
+ const result = useReactQuery < AnyPostgrestResponse < Result > , PostgrestError > (
142
+ buildQueryOpts < Result > ( query , config ) ,
143
+ ) ;
144
+
145
+ // isPending and isLoadingError are the only cases in which no data is present
146
+ if ( result . isPending || result . isLoadingError ) {
147
+ return {
148
+ ...result ,
149
+ data : undefined ,
150
+ count : null ,
151
+ } ;
152
+ }
138
153
139
- return { data : data ?. data , count : data ?. count ?? null , ...rest } ;
154
+ return {
155
+ ...result ,
156
+ data : result . data ?. data ,
157
+ count : result . data ?. count ,
158
+ } ;
140
159
}
141
160
142
161
export { useQuery } ;
0 commit comments