Skip to content

Commit adafd87

Browse files
committed
fix: enhance useQuery implementation with current scope checks
1 parent 19bb0db commit adafd87

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

packages/vue-apollo-composable/src/useQuery.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import type { EventHookOn } from '@vueuse/core'
1616
import type { Subscription } from 'rxjs'
1717
import type { RenameKey } from './util/types.ts'
1818
import { NetworkStatus } from '@apollo/client'
19-
import { computed, onScopeDispose, ref, shallowRef, toRef, toValue } from '@vue/reactivity'
20-
import { nextTick, onServerPrefetch, watch } from '@vue/runtime-core'
19+
import { computed, getCurrentScope, onScopeDispose, ref, shallowRef, toRef, toValue } from '@vue/reactivity'
20+
import { getCurrentInstance, nextTick, onServerPrefetch, watch } from '@vue/runtime-core'
2121
import { createEventHook, useDebounceFn, useThrottleFn } from '@vueuse/core'
2222
import { equal } from '@wry/equality'
2323
import { useApolloClient } from './useApolloClient.ts'
@@ -1003,6 +1003,9 @@ export function useQueryImpl<
10031003
// #endregion
10041004

10051005
// #region Core State
1006+
const currentScope = getCurrentScope()
1007+
const currentInstance = getCurrentInstance()
1008+
10061009
const observableQuery = shallowRef<ObservableQuery<TData, TVariables>>()
10071010
const subscription = shallowRef<Subscription>()
10081011

@@ -1020,7 +1023,8 @@ export function useQueryImpl<
10201023
const networkStatus = computed(() => currentState.value.networkStatus)
10211024
const error = computed(() => currentState.value.error)
10221025

1023-
trackQuery(loading)
1026+
if (currentScope)
1027+
trackQuery(loading)
10241028
// #endregion
10251029

10261030
// #region Events
@@ -1137,10 +1141,15 @@ export function useQueryImpl<
11371141
// #endregion
11381142

11391143
// #region Cleanup
1140-
onScopeDispose(() => {
1141-
subscription.value?.unsubscribe()
1142-
observableQuery.value?.stop()
1143-
})
1144+
if (currentScope) {
1145+
onScopeDispose(() => {
1146+
subscription.value?.unsubscribe()
1147+
observableQuery.value?.stop()
1148+
})
1149+
}
1150+
else {
1151+
console.warn('[Vue apollo] useQuery() is called outside of an active effect scope and the query will not be automatically stopped.')
1152+
}
11441153
// #endregion
11451154

11461155
// #region Public API
@@ -1258,16 +1267,18 @@ export function useQueryImpl<
12581267
}).then(onfulfilled, onrejected)
12591268
}
12601269

1261-
onServerPrefetch(() => {
1262-
if (
1263-
(vueApolloQueryOptions.value.prefetch ?? true)
1264-
&& isEnabled.value
1265-
) {
1266-
return { then }
1267-
}
1270+
if (currentInstance) {
1271+
onServerPrefetch(() => {
1272+
if (
1273+
(vueApolloQueryOptions.value.prefetch ?? true)
1274+
&& isEnabled.value
1275+
) {
1276+
return { then }
1277+
}
12681278

1269-
return Promise.resolve()
1270-
})
1279+
return Promise.resolve()
1280+
})
1281+
}
12711282

12721283
return {
12731284
then,

0 commit comments

Comments
 (0)