1
1
import {
2
2
ref ,
3
3
Ref ,
4
- unref ,
5
4
computed ,
6
5
watch ,
7
6
onServerPrefetch ,
8
7
getCurrentScope ,
9
8
onScopeDispose ,
10
9
nextTick ,
11
10
shallowRef ,
11
+ MaybeRefOrGetter ,
12
+ toRef ,
12
13
} from 'vue-demi'
13
14
import { DocumentNode } from 'graphql'
14
15
import type {
@@ -26,9 +27,6 @@ import type {
26
27
} from '@apollo/client/core/index.js'
27
28
import { throttle , debounce } from 'throttle-debounce'
28
29
import { useApolloClient } from './useApolloClient'
29
- import { ReactiveFunction } from './util/ReactiveFunction'
30
- import { paramToRef } from './util/paramToRef'
31
- import { paramToReactive } from './util/paramToReactive'
32
30
import { useEventHook } from './util/useEventHook'
33
31
import { trackQuery } from './util/loadingTracking'
34
32
import { resultErrorsToApolloError , toApolloError } from './util/toApolloError'
@@ -53,9 +51,9 @@ interface SubscribeToMoreItem {
53
51
}
54
52
55
53
// Parameters
56
- 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 >
57
- export type VariablesParameter < TVariables > = TVariables | Ref < TVariables > | ReactiveFunction < TVariables >
58
- export type OptionsParameter < TResult , TVariables extends OperationVariables > = UseQueryOptions < TResult , TVariables > | Ref < UseQueryOptions < TResult , TVariables > > | ReactiveFunction < UseQueryOptions < TResult , TVariables > >
54
+ export type DocumentParameter < TResult , TVariables > = DocumentNode | TypedDocumentNode < TResult , TVariables > | null | undefined
55
+ export type VariablesParameter < TVariables > = TVariables
56
+ export type OptionsParameter < TResult , TVariables extends OperationVariables > = UseQueryOptions < TResult , TVariables >
59
57
60
58
export interface OnResultContext {
61
59
client : ApolloClient < any >
@@ -77,11 +75,11 @@ export interface UseQueryReturn<TResult, TVariables extends OperationVariables>
77
75
forceDisabled : Ref < boolean >
78
76
document : Ref < DocumentNode | null | undefined >
79
77
variables : Ref < TVariables | undefined >
80
- options : UseQueryOptions < TResult , TVariables > | Ref < UseQueryOptions < TResult , TVariables > >
78
+ options : Ref < UseQueryOptions < TResult , TVariables > >
81
79
query : Ref < ObservableQuery < TResult , TVariables > | null | undefined >
82
80
refetch : ( variables ?: TVariables ) => Promise < ApolloQueryResult < TResult > > | undefined
83
81
fetchMore : ( options : FetchMoreQueryOptions < TVariables , TResult > & FetchMoreOptions < TResult , TVariables > ) => Promise < ApolloQueryResult < TResult > > | undefined
84
- subscribeToMore : < TSubscriptionVariables = OperationVariables , TSubscriptionData = TResult > ( options : SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > | Ref < SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > > | ReactiveFunction < SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > > ) => void
82
+ subscribeToMore : < TSubscriptionVariables = OperationVariables , TSubscriptionData = TResult > ( options : MaybeRefOrGetter < SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > > ) => void
85
83
onResult : ( fn : ( param : ApolloQueryResult < TResult > , context : OnResultContext ) => void ) => {
86
84
off : ( ) => void
87
85
}
@@ -94,49 +92,49 @@ export interface UseQueryReturn<TResult, TVariables extends OperationVariables>
94
92
* Use a query that does not require variables or options.
95
93
* */
96
94
export function useQuery < TResult = any > (
97
- document : DocumentParameter < TResult , undefined >
95
+ document : MaybeRefOrGetter < DocumentParameter < TResult , undefined > >
98
96
) : UseQueryReturn < TResult , Record < string , never > >
99
97
100
98
/**
101
99
* Use a query that has optional variables but not options
102
100
*/
103
101
export function useQuery < TResult = any , TVariables extends OperationVariables = OperationVariables > (
104
- document : DocumentParameter < TResult , TVariables >
102
+ document : MaybeRefOrGetter < DocumentParameter < TResult , TVariables > >
105
103
) : UseQueryReturn < TResult , TVariables >
106
104
107
105
/**
108
106
* Use a query that has required variables but not options
109
107
*/
110
108
export function useQuery < TResult = any , TVariables extends OperationVariables = OperationVariables > (
111
- document : DocumentParameter < TResult , TVariables > ,
112
- variables : VariablesParameter < TVariables >
109
+ document : MaybeRefOrGetter < DocumentParameter < TResult , TVariables > > ,
110
+ variables : MaybeRefOrGetter < VariablesParameter < TVariables > >
113
111
) : UseQueryReturn < TResult , TVariables >
114
112
115
113
/**
116
114
* Use a query that requires options but not variables.
117
115
*/
118
116
export function useQuery < TResult = any > (
119
- document : DocumentParameter < TResult , undefined > ,
117
+ document : MaybeRefOrGetter < DocumentParameter < TResult , undefined > > ,
120
118
variables : undefined | null ,
121
- options : OptionsParameter < TResult , Record < string , never > > ,
119
+ options : MaybeRefOrGetter < OptionsParameter < TResult , Record < string , never > > > ,
122
120
) : UseQueryReturn < TResult , Record < string , never > >
123
121
124
122
/**
125
123
* Use a query that requires variables and options.
126
124
*/
127
125
export function useQuery < TResult = any , TVariables extends OperationVariables = OperationVariables > (
128
- document : DocumentParameter < TResult , TVariables > ,
129
- variables : VariablesParameter < TVariables > ,
130
- options : OptionsParameter < TResult , TVariables > ,
126
+ document : MaybeRefOrGetter < DocumentParameter < TResult , TVariables > > ,
127
+ variables : MaybeRefOrGetter < VariablesParameter < TVariables > > ,
128
+ options : MaybeRefOrGetter < OptionsParameter < TResult , TVariables > > ,
131
129
) : UseQueryReturn < TResult , TVariables >
132
130
133
131
export function useQuery <
134
132
TResult ,
135
133
TVariables extends OperationVariables
136
134
> (
137
- document : DocumentParameter < TResult , TVariables > ,
138
- variables ?: VariablesParameter < TVariables > ,
139
- options ?: OptionsParameter < TResult , TVariables > ,
135
+ document : MaybeRefOrGetter < DocumentParameter < TResult , TVariables > > ,
136
+ variables ?: MaybeRefOrGetter < VariablesParameter < TVariables > > ,
137
+ options ?: MaybeRefOrGetter < OptionsParameter < TResult , TVariables > > ,
140
138
) : UseQueryReturn < TResult , TVariables > {
141
139
return useQueryImpl < TResult , TVariables > ( document , variables , options )
142
140
}
@@ -145,18 +143,18 @@ export function useQueryImpl<
145
143
TResult ,
146
144
TVariables extends OperationVariables
147
145
> (
148
- document : DocumentParameter < TResult , TVariables > ,
149
- variables ?: VariablesParameter < TVariables > ,
150
- options : OptionsParameter < TResult , TVariables > = { } ,
146
+ document : MaybeRefOrGetter < DocumentParameter < TResult , TVariables > > ,
147
+ variables ?: MaybeRefOrGetter < VariablesParameter < TVariables > > ,
148
+ options : MaybeRefOrGetter < OptionsParameter < TResult , TVariables > > = { } ,
151
149
lazy = false ,
152
150
) : UseQueryReturn < TResult , TVariables > {
153
151
const currentScope = getCurrentScope ( )
154
152
155
153
const currentOptions = ref < UseQueryOptions < TResult , TVariables > > ( )
156
154
157
- const documentRef = paramToRef ( document )
158
- const variablesRef = paramToRef ( variables )
159
- const optionsRef = paramToReactive ( options )
155
+ const documentRef = toRef ( document )
156
+ const variablesRef = toRef ( variables ) as Ref < TVariables | undefined >
157
+ const optionsRef = toRef ( options )
160
158
161
159
// Result
162
160
/**
@@ -252,7 +250,7 @@ export function useQueryImpl<
252
250
if ( isServer ) {
253
251
applyDocument ( documentRef . value )
254
252
applyVariables ( variablesRef . value )
255
- applyOptions ( unref ( optionsRef ) )
253
+ applyOptions ( optionsRef . value )
256
254
}
257
255
258
256
started = true
@@ -469,7 +467,7 @@ export function useQueryImpl<
469
467
const isEnabled = computed ( ( ) => enabledOption . value && ! forceDisabled . value && ! ! documentRef . value )
470
468
471
469
// Applying options first (in case it disables the query)
472
- watch ( ( ) => unref ( optionsRef ) , applyOptions , {
470
+ watch ( optionsRef , applyOptions , {
473
471
deep : true ,
474
472
immediate : true ,
475
473
} )
@@ -557,12 +555,10 @@ export function useQueryImpl<
557
555
TSubscriptionVariables = OperationVariables ,
558
556
TSubscriptionData = TResult
559
557
> (
560
- options : SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > |
561
- Ref < SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > > |
562
- ReactiveFunction < SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > > ,
558
+ options : MaybeRefOrGetter < SubscribeToMoreOptions < TResult , TSubscriptionVariables , TSubscriptionData > > ,
563
559
) {
564
560
if ( isServer ) return
565
- const optionsRef = paramToRef ( options )
561
+ const optionsRef = toRef ( options )
566
562
watch ( optionsRef , ( value , oldValue , onCleanup ) => {
567
563
const index = subscribeToMoreItems . findIndex ( item => item . options === oldValue )
568
564
if ( index !== - 1 ) {
0 commit comments