Skip to content

Commit 08b863f

Browse files
authored
Merge pull request #33 from guendev/chore/remove-unnecessary-logic
fix(core): improve reactive variable handling in useQuery and useSubscription
2 parents fe3854c + 2bf90d0 commit 08b863f

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

.changeset/clean-roses-kiss.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@vue3-apollo/core": patch
3+
"@vue3-apollo/nuxt": patch
4+
---
5+
6+
fix(core): improve reactive variable handling in `useQuery` and `useSubscription`
7+
- Replaced `syncRef` with `computed` for `reactiveVariables`.
8+
- Simplified variable updates to improve reactivity and reduce overhead.

packages/core/src/composables/useQuery.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import type {
77
TypedDocumentNode
88
} from '@apollo/client/core'
99
import type { DocumentNode } from 'graphql'
10-
import type { MaybeRefOrGetter, Ref } from 'vue'
10+
import type { MaybeRefOrGetter } from 'vue'
1111

12-
import { createEventHook, syncRef, useDebounceFn, useThrottleFn } from '@vueuse/core'
13-
import { computed, getCurrentScope, isReadonly, isRef, onScopeDispose, onServerPrefetch, ref, shallowRef, toValue, watch } from 'vue'
12+
import { createEventHook, useDebounceFn, useThrottleFn } from '@vueuse/core'
13+
import { computed, getCurrentScope, onScopeDispose, onServerPrefetch, ref, shallowRef, toValue, watch } from 'vue'
1414

1515
import type { HookContext, UseBaseOption } from '../utils/type'
1616

@@ -164,13 +164,7 @@ export function useQuery<TData = unknown, TVariables extends OperationVariables
164164
type: 'queries'
165165
})
166166

167-
const reactiveVariables = ref(toValue(variables))
168-
if (isRef(variables)) {
169-
syncRef(variables as Ref, reactiveVariables, {
170-
direction: isReadonly(variables) ? 'ltr' : 'both'
171-
})
172-
}
173-
167+
const reactiveVariables = computed(() => toValue(variables) ?? {} as TVariables)
174168
const reactiveDocument = computed(() => toValue(document))
175169

176170
const getQueryOptions = () => {
@@ -192,7 +186,7 @@ export function useQuery<TData = unknown, TVariables extends OperationVariables
192186
const queryResult = await client.query<TData, TVariables>({
193187
...getQueryOptions(),
194188
query: reactiveDocument.value,
195-
variables: toValue(reactiveVariables)
189+
variables: reactiveVariables.value
196190
})
197191

198192
// Set initial data from server
@@ -274,7 +268,7 @@ export function useQuery<TData = unknown, TVariables extends OperationVariables
274268
try {
275269
const cachedData = client.readQuery<TData, TVariables>({
276270
query: reactiveDocument.value,
277-
variables: toValue(reactiveVariables)
271+
variables: reactiveVariables.value
278272
})
279273

280274
// If we have cached data from SSR, set the result and loading state immediately
@@ -289,10 +283,10 @@ export function useQuery<TData = unknown, TVariables extends OperationVariables
289283
}
290284

291285
query.value = client.watchQuery<TData, TVariables>({
286+
...getQueryOptions(),
292287
notifyOnNetworkStatusChange: options?.notifyOnNetworkStatusChange ?? options?.keepPreviousResult,
293288
query: reactiveDocument.value,
294-
variables: toValue(reactiveVariables),
295-
...getQueryOptions()
289+
variables: reactiveVariables.value
296290
})
297291

298292
startObserver()
@@ -325,8 +319,8 @@ export function useQuery<TData = unknown, TVariables extends OperationVariables
325319
? useThrottleFn(updateVariables, options.throttle)
326320
: updateVariables
327321

328-
watch(reactiveVariables, (newVariables) => {
329-
optimizedUpdateVariables(newVariables)
322+
watch(reactiveVariables, (updatedVariables) => {
323+
optimizedUpdateVariables(updatedVariables)
330324
}, {
331325
deep: true,
332326
flush: 'post'

packages/core/src/composables/useSubscription.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import type {
77
TypedDocumentNode
88
} from '@apollo/client/core'
99
import type { DocumentNode } from 'graphql'
10-
import type { MaybeRefOrGetter, Ref } from 'vue'
10+
import type { MaybeRefOrGetter } from 'vue'
1111

12-
import { createEventHook, syncRef } from '@vueuse/core'
13-
import { computed, getCurrentScope, isReadonly, isRef, onScopeDispose, ref, shallowRef, toRef, toValue, watch } from 'vue'
12+
import { createEventHook } from '@vueuse/core'
13+
import { computed, getCurrentScope, onScopeDispose, ref, shallowRef, toRef, toValue, watch } from 'vue'
1414

1515
import type { HookContext, UseBaseOption } from '../utils/type'
1616

@@ -63,13 +63,7 @@ export function useSubscription<
6363
const subscriptionData = createEventHook<[TData, HookContext]>()
6464
const subscriptionError = createEventHook<[ErrorLike, HookContext]>()
6565

66-
const reactiveVariables = ref(toValue(variables))
67-
if (isRef(variables)) {
68-
syncRef(variables as Ref, reactiveVariables, {
69-
direction: isReadonly(variables) ? 'ltr' : 'both'
70-
})
71-
}
72-
66+
const reactiveVariables = computed(() => toValue(variables) ?? {} as TVariables)
7367
const reactiveDocument = computed(() => toValue(document))
7468

7569
useApolloTracking({
@@ -130,7 +124,7 @@ export function useSubscription<
130124

131125
subscription.value = client.subscribe<TData, TVariables>({
132126
query: reactiveDocument.value,
133-
variables: toValue(reactiveVariables),
127+
variables: reactiveVariables.value,
134128
...options
135129
})
136130

0 commit comments

Comments
 (0)