Skip to content

feat(vue-query): support invoke in vue runWithContext() #5703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/vue-query/src/__tests__/useQueryClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ describe('useQueryClient', () => {

expect(queryClient).toStrictEqual(queryClientMock)
expect(injectSpy).toHaveBeenCalledTimes(1)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT, null)
})

test('should throw an error when queryClient does not exist in the context', () => {
injectSpy.mockReturnValueOnce(undefined)

expect(useQueryClient).toThrowError()
expect(injectSpy).toHaveBeenCalledTimes(1)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT)
expect(injectSpy).toHaveBeenCalledWith(VUE_QUERY_CLIENT, null)
})

test('should throw an error when used outside of setup function', () => {
Expand All @@ -44,6 +44,6 @@ describe('useQueryClient', () => {

useQueryClient(queryClientKey)

expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter)
expect(injectSpy).toHaveBeenCalledWith(expectedKeyParameter, null)
})
})
9 changes: 9 additions & 0 deletions packages/vue-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
computed,
getCurrentScope,
onScopeDispose,
reactive,
readonly,
Expand Down Expand Up @@ -58,6 +59,14 @@ export function useBaseQuery<
| UseQueryOptionsGeneric<TQueryFnData, TError, TData, TQueryKey> = {},
arg3: UseQueryOptionsGeneric<TQueryFnData, TError, TData, TQueryKey> = {},
): UseQueryReturnType<TData, TError> {
if (process.env.NODE_ENV === 'development') {
if (!getCurrentScope()) {
console.warn(
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
)
}
}

const options = computed(() => parseQueryArgs(arg1, arg2, arg3))

const queryClient =
Expand Down
17 changes: 16 additions & 1 deletion packages/vue-query/src/useIsFetching.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { computed, onScopeDispose, ref, unref, watch } from 'vue-demi'
import {
computed,
getCurrentScope,
onScopeDispose,
ref,
unref,
watch,
} from 'vue-demi'
import { useQueryClient } from './useQueryClient'
import { cloneDeepUnref, isQueryKey } from './utils'
import type { Ref } from 'vue-demi'
Expand All @@ -17,6 +24,14 @@ export function useIsFetching(
arg1?: MaybeRef<QueryKey> | QueryFilters,
arg2?: Omit<QueryFilters, 'queryKey'>,
): Ref<number> {
if (process.env.NODE_ENV === 'development') {
if (!getCurrentScope()) {
console.warn(
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
)
}
}

const filters = computed(() => parseFilterArgs(arg1, arg2))
const queryClient =
filters.value.queryClient ?? useQueryClient(filters.value.queryClientKey)
Expand Down
17 changes: 16 additions & 1 deletion packages/vue-query/src/useIsMutating.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { computed, onScopeDispose, ref, unref, watch } from 'vue-demi'
import {
computed,
getCurrentScope,
onScopeDispose,
ref,
unref,
watch,
} from 'vue-demi'
import { useQueryClient } from './useQueryClient'
import { cloneDeepUnref, isQueryKey } from './utils'
import type { Ref } from 'vue-demi'
Expand All @@ -17,6 +24,14 @@ export function useIsMutating(
arg1?: MaybeRef<MutationKey> | MutationFilters,
arg2?: Omit<MutationFilters, 'mutationKey'>,
): Ref<number> {
if (process.env.NODE_ENV === 'development') {
if (!getCurrentScope()) {
console.warn(
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
)
}
}

const filters = computed(() => parseFilterArgs(arg1, arg2))
const queryClient =
filters.value.queryClient ?? useQueryClient(filters.value.queryClientKey)
Expand Down
9 changes: 9 additions & 0 deletions packages/vue-query/src/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
computed,
getCurrentScope,
onScopeDispose,
reactive,
readonly,
Expand Down Expand Up @@ -148,6 +149,14 @@ export function useMutation<
VueMutationObserverOptions<TData, TError, TVariables, TContext>
>,
): UseMutationReturnType<TData, TError, TVariables, TContext> {
if (process.env.NODE_ENV === 'development') {
if (!getCurrentScope()) {
console.warn(
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
)
}
}

const options = computed(() => {
return parseMutationArgs(arg1, arg2, arg3)
})
Expand Down
17 changes: 16 additions & 1 deletion packages/vue-query/src/useQueries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { QueriesObserver } from '@tanstack/query-core'
import { computed, onScopeDispose, reactive, readonly, watch } from 'vue-demi'
import {
computed,
getCurrentScope,
onScopeDispose,
reactive,
readonly,
watch,
} from 'vue-demi'
import { useQueryClient } from './useQueryClient'
import { cloneDeepUnref } from './utils'
import type { Ref } from 'vue-demi'
Expand Down Expand Up @@ -133,6 +140,14 @@ export function useQueries<T extends any[]>({
queries: Ref<UseQueriesOptionsArg<T>> | UseQueriesOptionsArg<T>
queryClient?: QueryClient
}): Readonly<UseQueriesResults<T>> {
if (process.env.NODE_ENV === 'development') {
if (!getCurrentScope()) {
console.warn(
'vue-query composables like "uesQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
)
}
}

const unreffedQueries = computed(
() => cloneDeepUnref(queries) as UseQueriesOptionsArg<T>,
)
Expand Down
16 changes: 9 additions & 7 deletions packages/vue-query/src/useQueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { getClientKey } from './utils'
import type { QueryClient } from './queryClient'

export function useQueryClient(id = ''): QueryClient {
const vm = getCurrentInstance()?.proxy

if (!vm) {
throw new Error('vue-query hooks can only be used inside setup() function.')
}

const key = getClientKey(id)
const queryClient = inject<QueryClient>(key)
const queryClient = inject<QueryClient | null>(key, null)

if (!queryClient) {
const vm = getCurrentInstance()?.proxy

if (!vm) {
throw new Error(
'vue-query hooks can only be used inside setup() function.',
)
}

throw new Error(
"No 'queryClient' found in Vue context, use 'VueQueryPlugin' to properly initialize the library.",
)
Expand Down