2
2
3
3
import { useCallback , useContext , useEffect , useMemo , useState } from "react" ;
4
4
import { WunderGraphContext } from "./provider" ;
5
- import { RequestOptions , MutateRequestOptions , SubscriptionRequestOptions , Response } from "./client " ;
5
+ import { RequestOptions , MutateRequestOptions , SubscriptionRequestOptions , Response } from "@wundergraph/sdk " ;
6
6
import {
7
7
AddMessageInput ,
8
8
AllUsersInput ,
9
9
ChangeUserNameInput ,
10
10
DeleteAllMessagesByUserEmailInput ,
11
11
UpdateUserInput ,
12
12
AllUsersResponse ,
13
+ CountriesResponse ,
14
+ GermanyResponse ,
13
15
MessagesResponse ,
14
16
MockQueryResponse ,
17
+ QueryResponse ,
18
+ UsResponse ,
15
19
UserInfoResponse ,
16
20
} from "./models" ;
17
21
@@ -47,8 +51,17 @@ const Query = <R extends {}, I extends {}>(
47
51
const [ shouldFetch , setShouldFetch ] = useState < boolean > ( options === undefined || options . initialState === undefined ) ;
48
52
const refetch = useCallback ( ( options ?: RequestOptions < I , R > ) => {
49
53
if ( options !== undefined ) {
50
- _setOptions ( options ) ;
54
+ _setOptions ( {
55
+ ...options ,
56
+ lazy : false ,
57
+ } ) ;
58
+ } else if ( _options && _options . lazy === true ) {
59
+ _setOptions ( {
60
+ ..._options ,
61
+ lazy : false ,
62
+ } ) ;
51
63
}
64
+ setResponse ( { status : "loading" } ) ;
52
65
setShouldFetch ( true ) ;
53
66
} , [ ] ) ;
54
67
useEffect ( ( ) => {
@@ -62,6 +75,8 @@ const Query = <R extends {}, I extends {}>(
62
75
status : "ok" ,
63
76
body : options . initialState ,
64
77
}
78
+ : _options && _options . lazy === true
79
+ ? { status : "lazy" }
65
80
: { status : "loading" }
66
81
) ;
67
82
useEffect ( ( ) => {
@@ -75,6 +90,9 @@ const Query = <R extends {}, I extends {}>(
75
90
if ( ! shouldFetch ) {
76
91
return ;
77
92
}
93
+ if ( _options && _options . lazy === true ) {
94
+ return ;
95
+ }
78
96
const abortController = new AbortController ( ) ;
79
97
if ( response . status === "ok" ) {
80
98
setResponse ( { status : "ok" , refetching : true , body : response . body } ) ;
@@ -136,6 +154,7 @@ const Mutation = <R extends {}, I extends {}>(
136
154
abortSignal :
137
155
options !== undefined && options . abortSignal !== undefined ? options . abortSignal : _options ?. abortSignal ,
138
156
} ;
157
+ setResponse ( { status : "loading" } ) ;
139
158
const result = await promiseFactory ( combinedOptions ) ;
140
159
setResponse ( result ) ;
141
160
if ( result . status === "ok" && combinedOptions . refetchMountedQueriesOnSuccess === true ) {
@@ -204,11 +223,28 @@ const Subscription = <R, I>(
204
223
} ;
205
224
} ;
206
225
226
+ export const useLoadingComplete = ( ...responses : Response < any > [ ] ) => {
227
+ const [ loading , setLoading ] = useState ( true ) ;
228
+ useEffect ( ( ) => {
229
+ const isLoading = responses . some ( ( r ) => r . status === "loading" ) ;
230
+ if ( isLoading !== loading ) setLoading ( isLoading ) ;
231
+ } , responses ) ;
232
+ return loading ;
233
+ } ;
234
+
207
235
export const useQuery = {
208
236
AllUsers : ( options : RequestOptions < AllUsersInput , AllUsersResponse > ) => {
209
237
const { client } = useWunderGraph ( ) ;
210
238
return Query ( client . query . AllUsers , { requiresAuthentication : false } , options ) ;
211
239
} ,
240
+ Countries : ( options ?: RequestOptions < never , CountriesResponse > ) => {
241
+ const { client } = useWunderGraph ( ) ;
242
+ return Query ( client . query . Countries , { requiresAuthentication : false } , options ) ;
243
+ } ,
244
+ Germany : ( options ?: RequestOptions < never , GermanyResponse > ) => {
245
+ const { client } = useWunderGraph ( ) ;
246
+ return Query ( client . query . Germany , { requiresAuthentication : false } , options ) ;
247
+ } ,
212
248
Messages : ( options ?: RequestOptions < never , MessagesResponse > ) => {
213
249
const { client } = useWunderGraph ( ) ;
214
250
return Query ( client . query . Messages , { requiresAuthentication : false } , options ) ;
@@ -217,6 +253,14 @@ export const useQuery = {
217
253
const { client } = useWunderGraph ( ) ;
218
254
return Query ( client . query . MockQuery , { requiresAuthentication : false } , options ) ;
219
255
} ,
256
+ Query : ( options ?: RequestOptions < never , QueryResponse > ) => {
257
+ const { client } = useWunderGraph ( ) ;
258
+ return Query ( client . query . Query , { requiresAuthentication : false } , options ) ;
259
+ } ,
260
+ Us : ( options ?: RequestOptions < never , UsResponse > ) => {
261
+ const { client } = useWunderGraph ( ) ;
262
+ return Query ( client . query . Us , { requiresAuthentication : false } , options ) ;
263
+ } ,
220
264
UserInfo : ( options ?: RequestOptions < never , UserInfoResponse > ) => {
221
265
const { client } = useWunderGraph ( ) ;
222
266
return Query ( client . query . UserInfo , { requiresAuthentication : true } , options ) ;
@@ -247,6 +291,14 @@ export const useLiveQuery = {
247
291
const { client } = useWunderGraph ( ) ;
248
292
return Subscription ( client . liveQuery . AllUsers , { requiresAuthentication : false } , options ) ;
249
293
} ,
294
+ Countries : ( options ?: SubscriptionRequestOptions ) => {
295
+ const { client } = useWunderGraph ( ) ;
296
+ return Subscription ( client . liveQuery . Countries , { requiresAuthentication : false } , options ) ;
297
+ } ,
298
+ Germany : ( options ?: SubscriptionRequestOptions ) => {
299
+ const { client } = useWunderGraph ( ) ;
300
+ return Subscription ( client . liveQuery . Germany , { requiresAuthentication : false } , options ) ;
301
+ } ,
250
302
Messages : ( options ?: SubscriptionRequestOptions ) => {
251
303
const { client } = useWunderGraph ( ) ;
252
304
return Subscription ( client . liveQuery . Messages , { requiresAuthentication : false } , options ) ;
@@ -255,6 +307,14 @@ export const useLiveQuery = {
255
307
const { client } = useWunderGraph ( ) ;
256
308
return Subscription ( client . liveQuery . MockQuery , { requiresAuthentication : false } , options ) ;
257
309
} ,
310
+ Query : ( options ?: SubscriptionRequestOptions ) => {
311
+ const { client } = useWunderGraph ( ) ;
312
+ return Subscription ( client . liveQuery . Query , { requiresAuthentication : false } , options ) ;
313
+ } ,
314
+ Us : ( options ?: SubscriptionRequestOptions ) => {
315
+ const { client } = useWunderGraph ( ) ;
316
+ return Subscription ( client . liveQuery . Us , { requiresAuthentication : false } , options ) ;
317
+ } ,
258
318
UserInfo : ( options ?: SubscriptionRequestOptions ) => {
259
319
const { client } = useWunderGraph ( ) ;
260
320
return Subscription ( client . liveQuery . UserInfo , { requiresAuthentication : true } , options ) ;
0 commit comments