-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
useQuery.ts
661 lines (574 loc) Β· 19.4 KB
/
useQuery.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import {
ref,
Ref,
unref,
computed,
watch,
onServerPrefetch,
getCurrentScope,
getCurrentInstance,
onScopeDispose,
nextTick,
shallowRef,
} from 'vue-demi'
import { DocumentNode } from 'graphql'
import type {
OperationVariables,
WatchQueryOptions,
ObservableQuery,
ApolloQueryResult,
SubscribeToMoreOptions,
FetchMoreQueryOptions,
FetchMoreOptions,
ObservableSubscription,
TypedDocumentNode,
ApolloError,
ApolloClient,
} from '@apollo/client/core/index.js'
import { throttle, debounce } from 'throttle-debounce'
import { useApolloClient } from './useApolloClient'
import { ReactiveFunction } from './util/ReactiveFunction'
import { paramToRef } from './util/paramToRef'
import { paramToReactive } from './util/paramToReactive'
import { useEventHook } from './util/useEventHook'
import { trackQuery } from './util/loadingTracking'
import { resultErrorsToApolloError, toApolloError } from './util/toApolloError'
import { isServer } from './util/env'
export interface UseQueryOptions<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TResult = any,
TVariables extends OperationVariables = OperationVariables
> extends Omit<WatchQueryOptions<TVariables>, 'query' | 'variables'> {
clientId?: string
enabled?: boolean | Ref<boolean>
throttle?: number
debounce?: number
prefetch?: boolean
keepPreviousResult?: boolean
}
interface SubscribeToMoreItem {
options: any
unsubscribeFns: (() => void)[]
}
// Parameters
export type DocumentParameter<TResult, TVariables> = DocumentNode | Ref<DocumentNode | null | undefined> | ReactiveFunction<DocumentNode | null | undefined> | TypedDocumentNode<TResult, TVariables> | Ref<TypedDocumentNode<TResult, TVariables> | null | undefined> | ReactiveFunction<TypedDocumentNode<TResult, TVariables> | null | undefined>
export type VariablesParameter<TVariables> = TVariables | Ref<TVariables> | ReactiveFunction<TVariables>
export type OptionsParameter<TResult, TVariables extends OperationVariables> = UseQueryOptions<TResult, TVariables> | Ref<UseQueryOptions<TResult, TVariables>> | ReactiveFunction<UseQueryOptions<TResult, TVariables>>
export interface OnResultContext {
client: ApolloClient<any>
}
export interface OnErrorContext {
client: ApolloClient<any>
}
// Return
export interface UseQueryReturn<TResult, TVariables extends OperationVariables> {
result: Ref<TResult | undefined>
loading: Ref<boolean>
networkStatus: Ref<number | undefined>
error: Ref<ApolloError | null>
start: () => void
stop: () => void
restart: () => void
forceDisabled: Ref<boolean>
document: Ref<DocumentNode | null | undefined>
variables: Ref<TVariables | undefined>
options: UseQueryOptions<TResult, TVariables> | Ref<UseQueryOptions<TResult, TVariables>>
query: Ref<ObservableQuery<TResult, TVariables> | null | undefined>
refetch: (variables?: TVariables) => Promise<ApolloQueryResult<TResult>> | undefined
fetchMore: (options: FetchMoreQueryOptions<TVariables, TResult> & FetchMoreOptions<TResult, TVariables>) => Promise<ApolloQueryResult<TResult>> | undefined
updateQuery: (mapFn: (previousQueryResult: TResult, options: Pick<WatchQueryOptions<TVariables, TResult>, 'variables'>) => TResult) => void
subscribeToMore: <TSubscriptionVariables = OperationVariables, TSubscriptionData = TResult>(options: SubscribeToMoreOptions<TResult, TSubscriptionVariables, TSubscriptionData> | Ref<SubscribeToMoreOptions<TResult, TSubscriptionVariables, TSubscriptionData>> | ReactiveFunction<SubscribeToMoreOptions<TResult, TSubscriptionVariables, TSubscriptionData>>) => void
onResult: (fn: (param: ApolloQueryResult<TResult>, context: OnResultContext) => void) => {
off: () => void
}
onError: (fn: (param: ApolloError, context: OnErrorContext) => void) => {
off: () => void
}
}
/**
* Use a query that does not require variables or options.
* */
export function useQuery<TResult = any> (
document: DocumentParameter<TResult, undefined>
): UseQueryReturn<TResult, Record<string, never>>
/**
* Use a query that has optional variables but not options
*/
export function useQuery<TResult = any, TVariables extends OperationVariables = OperationVariables> (
document: DocumentParameter<TResult, TVariables>
): UseQueryReturn<TResult, TVariables>
/**
* Use a query that has required variables but not options
*/
export function useQuery<TResult = any, TVariables extends OperationVariables = OperationVariables> (
document: DocumentParameter<TResult, TVariables>,
variables: VariablesParameter<TVariables>
): UseQueryReturn<TResult, TVariables>
/**
* Use a query that requires options but not variables.
*/
export function useQuery<TResult = any> (
document: DocumentParameter<TResult, undefined>,
variables: undefined | null,
options: OptionsParameter<TResult, Record<string, never>>,
): UseQueryReturn<TResult, Record<string, never>>
/**
* Use a query that requires variables and options.
*/
export function useQuery<TResult = any, TVariables extends OperationVariables = OperationVariables> (
document: DocumentParameter<TResult, TVariables>,
variables: VariablesParameter<TVariables>,
options: OptionsParameter<TResult, TVariables>,
): UseQueryReturn<TResult, TVariables>
export function useQuery<
TResult,
TVariables extends OperationVariables
> (
document: DocumentParameter<TResult, TVariables>,
variables?: VariablesParameter<TVariables>,
options?: OptionsParameter<TResult, TVariables>,
): UseQueryReturn<TResult, TVariables> {
return useQueryImpl<TResult, TVariables>(document, variables, options)
}
export function useQueryImpl<
TResult,
TVariables extends OperationVariables
> (
document: DocumentParameter<TResult, TVariables>,
variables?: VariablesParameter<TVariables>,
options: OptionsParameter<TResult, TVariables> = {},
lazy = false,
): UseQueryReturn<TResult, TVariables> {
const currentScope = getCurrentScope()
const currentInstance = getCurrentInstance()
const currentOptions = ref<UseQueryOptions<TResult, TVariables>>()
const documentRef = paramToRef(document)
const variablesRef = paramToRef(variables)
const optionsRef = paramToReactive(options)
// Result
/**
* Result from the query
*/
const result = shallowRef<TResult | undefined>()
const resultEvent = useEventHook<[ApolloQueryResult<TResult>, OnResultContext]>()
const error = shallowRef<ApolloError | null>(null)
const errorEvent = useEventHook<[ApolloError, OnErrorContext]>()
// Loading
/**
* Indicates if a network request is pending
*/
const loading = ref(false)
currentScope && trackQuery(loading)
const networkStatus = ref<number>()
// SSR
let firstResolve: (() => void) | undefined
let firstResolveTriggered = false
let firstReject: ((apolloError: ApolloError) => void) | undefined
let firstRejectError: undefined | ApolloError
const tryFirstResolve = () => {
firstResolveTriggered = true
if (firstResolve) firstResolve()
}
const tryFirstReject = (apolloError: ApolloError) => {
firstRejectError = apolloError
if (firstReject) firstReject(apolloError)
}
const resetFirstResolveReject = () => {
firstResolve = undefined
firstReject = undefined
firstResolveTriggered = false
firstRejectError = undefined
}
currentInstance && onServerPrefetch?.(() => {
if (!isEnabled.value || (isServer && currentOptions.value?.prefetch === false)) return
return new Promise<void>((resolve, reject) => {
firstResolve = () => {
resetFirstResolveReject()
resolve()
}
firstReject = (apolloError: ApolloError) => {
resetFirstResolveReject()
reject(apolloError)
}
if (firstResolveTriggered) {
firstResolve()
} else if (firstRejectError) {
firstReject(firstRejectError)
}
}).finally(stop)
})
// Apollo Client
const { resolveClient } = useApolloClient()
function getClient () {
return resolveClient(currentOptions.value?.clientId)
}
// Query
const query: Ref<ObservableQuery<TResult, TVariables> | null | undefined> = shallowRef()
let observer: ObservableSubscription | undefined
let started = false
let ignoreNextResult = false
let firstStart = true
/**
* Starts watching the query
*/
function start () {
if (
started || !isEnabled.value ||
(isServer && currentOptions.value?.prefetch === false) ||
!currentDocument
) {
tryFirstResolve()
return
}
// On server the watchers on document, variables and options are not triggered
if (isServer) {
applyDocument(documentRef.value)
applyVariables(variablesRef.value)
applyOptions(unref(optionsRef))
}
started = true
error.value = null
loading.value = true
const client = getClient()
query.value = client.watchQuery<TResult, TVariables>({
query: currentDocument,
variables: currentVariables ?? {} as TVariables,
...currentOptions.value,
...(isServer && currentOptions.value?.fetchPolicy !== 'no-cache')
? {
fetchPolicy: 'network-only',
}
: {},
})
startQuerySubscription()
// Make the cache data available to the component immediately
// This prevents SSR hydration mismatches
if (!isServer && (firstStart || !currentOptions.value?.keepPreviousResult) && (currentOptions.value?.fetchPolicy !== 'no-cache' || currentOptions.value.notifyOnNetworkStatusChange)) {
const currentResult = query.value.getCurrentResult(false)
if (!currentResult.loading || currentResult.partial || currentOptions.value?.notifyOnNetworkStatusChange) {
onNextResult(currentResult)
ignoreNextResult = !currentResult.loading
} else if (currentResult.error) {
onError(currentResult.error)
ignoreNextResult = true
}
}
if (!isServer) {
for (const item of subscribeToMoreItems) {
addSubscribeToMore(item)
}
}
firstStart = false
}
function startQuerySubscription () {
if (observer && !observer.closed) return
if (!query.value) return
// Create subscription
ignoreNextResult = false
observer = query.value.subscribe({
next: onNextResult,
error: onError,
})
}
function getErrorPolicy () {
const client = resolveClient(currentOptions.value?.clientId)
return currentOptions.value?.errorPolicy || client.defaultOptions?.watchQuery?.errorPolicy
}
function onNextResult (queryResult: ApolloQueryResult<TResult>) {
if (ignoreNextResult) {
ignoreNextResult = false
return
}
// Remove any previous error that may still be present from the last fetch (so result handlers
// don't receive old errors that may not even be applicable anymore).
error.value = null
processNextResult(queryResult)
// When `errorPolicy` is `all`, `onError` will not get called and
// ApolloQueryResult.errors may be set at the same time as we get a result.
// The code is only relevant when `errorPolicy` is `all`, because for other situations it
// could hapen that next and error are called at the same time and then it will lead to multiple
// onError calls.
const errorPolicy = getErrorPolicy()
if (errorPolicy && errorPolicy === 'all' && !queryResult.error && queryResult.errors?.length) {
processError(resultErrorsToApolloError(queryResult.errors))
}
tryFirstResolve()
}
function processNextResult (queryResult: ApolloQueryResult<TResult>) {
result.value = queryResult.data && Object.keys(queryResult.data).length === 0
? queryResult.error &&
!currentOptions.value?.returnPartialData &&
currentOptions.value?.errorPolicy === 'none'
? undefined
: result.value
: queryResult.data
loading.value = queryResult.loading
networkStatus.value = queryResult.networkStatus
// Wait for handlers to be registered
nextTick(() => {
resultEvent.trigger(queryResult, {
client: getClient(),
})
})
}
function onError (queryError: unknown) {
if (ignoreNextResult) {
ignoreNextResult = false
return
}
// any error should already be an ApolloError, but we make sure
const apolloError = toApolloError(queryError)
const errorPolicy = getErrorPolicy()
if (errorPolicy && errorPolicy !== 'none') {
processNextResult((query.value as ObservableQuery<TResult, TVariables>).getCurrentResult())
}
processError(apolloError)
tryFirstReject(apolloError)
// The observable closes the sub if an error occurs
resubscribeToQuery()
}
function processError (apolloError: ApolloError) {
error.value = apolloError
loading.value = false
networkStatus.value = 8
// Wait for handlers to be registered
nextTick(() => {
errorEvent.trigger(apolloError, {
client: getClient(),
})
})
}
function resubscribeToQuery () {
if (!query.value) return
const lastError = query.value.getLastError()
const lastResult = query.value.getLastResult()
query.value.resetLastResults()
startQuerySubscription()
Object.assign(query.value, { lastError, lastResult })
}
let onStopHandlers: Array<() => void> = []
/**
* Stop watching the query
*/
function stop () {
tryFirstResolve()
if (!started) return
started = false
loading.value = false
onStopHandlers.forEach(handler => handler())
onStopHandlers = []
if (query.value) {
query.value.stopPolling()
query.value = null
}
if (observer) {
observer.unsubscribe()
observer = undefined
}
}
// Restart
let restarting = false
/**
* Queue a restart of the query (on next tick) if it is already active
*/
function baseRestart () {
if (!started || restarting) return
restarting = true
// eslint-disable-next-line @typescript-eslint/no-floating-promises
nextTick(() => {
if (started) {
stop()
start()
}
restarting = false
})
}
let debouncedRestart: typeof baseRestart
let isRestartDebounceSetup = false
function updateRestartFn () {
// On server, will be called before currentOptions is initialized
// @TODO investigate
if (!currentOptions.value) {
debouncedRestart = baseRestart
} else {
if (currentOptions.value?.throttle) {
debouncedRestart = throttle(currentOptions.value.throttle, baseRestart)
} else if (currentOptions.value?.debounce) {
debouncedRestart = debounce(currentOptions.value.debounce, baseRestart)
} else {
debouncedRestart = baseRestart
}
isRestartDebounceSetup = true
}
}
function restart () {
if (!started || restarting) return
if (!isRestartDebounceSetup) updateRestartFn()
debouncedRestart()
}
// Applying document
let currentDocument: DocumentNode | null | undefined = documentRef.value
// Enabled state
const forceDisabled = ref(lazy)
const enabledOption = computed(() => !currentOptions.value || currentOptions.value.enabled == null || currentOptions.value.enabled)
const isEnabled = computed(() => enabledOption.value && !forceDisabled.value && !!documentRef.value)
// Applying options first (in case it disables the query)
watch(() => unref(optionsRef), applyOptions, {
deep: true,
immediate: true,
})
function applyOptions (value: UseQueryOptions<TResult, TVariables>) {
if (currentOptions.value && (
currentOptions.value.throttle !== value.throttle ||
currentOptions.value.debounce !== value.debounce
)) {
updateRestartFn()
}
currentOptions.value = value
restart()
}
// Applying document
watch(documentRef, applyDocument)
function applyDocument (value: DocumentNode | null | undefined) {
currentDocument = value
restart()
}
// Applying variables
let currentVariables: TVariables | undefined
let currentVariablesSerialized: string
watch(() => {
if (isEnabled.value) {
return variablesRef.value
} else {
return undefined
}
}, applyVariables, {
deep: true,
immediate: true,
})
function applyVariables (value?: TVariables) {
const serialized = JSON.stringify([value, isEnabled.value])
if (serialized !== currentVariablesSerialized) {
currentVariables = value
restart()
}
currentVariablesSerialized = serialized
}
// Refetch
function refetch (variables: TVariables | undefined = undefined) {
if (query.value) {
if (variables) {
currentVariables = variables
}
error.value = null
loading.value = true
return query.value.refetch(variables)
.then((refetchResult) => {
const currentResult = query.value?.getCurrentResult()
currentResult && processNextResult(currentResult)
return refetchResult
})
}
}
// Update Query
function updateQuery (mapFn: (previousQueryResult: TResult, options: Pick<WatchQueryOptions<TVariables, TResult>, 'variables'>) => TResult) {
if (query.value) {
query.value.updateQuery(mapFn)
}
}
// Fetch more
function fetchMore (options: FetchMoreQueryOptions<TVariables, TResult> & FetchMoreOptions<TResult, TVariables>) {
if (query.value) {
error.value = null
loading.value = true
return query.value.fetchMore(options)
.then((fetchMoreResult) => {
const currentResult = query.value?.getCurrentResult()
currentResult && processNextResult(currentResult)
return fetchMoreResult
})
}
}
// Subscribe to more
const subscribeToMoreItems: SubscribeToMoreItem[] = []
function subscribeToMore<
TSubscriptionVariables = OperationVariables,
TSubscriptionData = TResult
> (
options: SubscribeToMoreOptions<TResult, TSubscriptionVariables, TSubscriptionData> |
Ref<SubscribeToMoreOptions<TResult, TSubscriptionVariables, TSubscriptionData>> |
ReactiveFunction<SubscribeToMoreOptions<TResult, TSubscriptionVariables, TSubscriptionData>>,
) {
if (isServer) return
const optionsRef = paramToRef(options)
watch(optionsRef, (value, oldValue, onCleanup) => {
const index = subscribeToMoreItems.findIndex(item => item.options === oldValue)
if (index !== -1) {
subscribeToMoreItems.splice(index, 1)
}
const item: SubscribeToMoreItem = {
options: value,
unsubscribeFns: [],
}
subscribeToMoreItems.push(item)
addSubscribeToMore(item)
onCleanup(() => {
item.unsubscribeFns.forEach(fn => fn())
item.unsubscribeFns = []
})
}, {
immediate: true,
})
}
function addSubscribeToMore (item: SubscribeToMoreItem) {
if (!started) return
if (!query.value) {
throw new Error('Query is not defined')
}
const unsubscribe = query.value.subscribeToMore(item.options)
onStopHandlers.push(unsubscribe)
item.unsubscribeFns.push(unsubscribe)
}
// Auto start & stop
watch(isEnabled, value => {
if (value) {
nextTick(() => {
start()
})
} else {
stop()
}
})
if (isEnabled.value) {
start()
}
// Teardown
if (currentScope) {
onScopeDispose(() => {
stop()
subscribeToMoreItems.length = 0
})
} else {
console.warn('[Vue apollo] useQuery() is called outside of an active effect scope and the query will not be automatically stopped.')
}
return {
result,
loading,
networkStatus,
error,
start,
stop,
restart,
forceDisabled,
document: documentRef,
variables: variablesRef,
options: optionsRef,
query,
refetch,
fetchMore,
subscribeToMore,
updateQuery,
onResult: resultEvent.on,
onError: errorEvent.on,
}
}