Skip to content

Commit 50cec8b

Browse files
posvaDamianOsipiuk
authored andcommitted
feat(vue-query): support injectable contexts
This feature requires Vue 3.3.0, which has been out for a while now. It allows using vue-query APIs in places where it is valid to use them but currently throws an error. - `hasInjectionContext()`: vuejs/core#8111 - `app.runWithContext()`: vuejs/core#7451
1 parent 787efb5 commit 50cec8b

File tree

8 files changed

+538
-154
lines changed

8 files changed

+538
-154
lines changed

examples/vue/basic/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@tanstack/vue-query": "^5.0.0-beta.0",
12-
"vue": "^3.2.47"
12+
"vue": "^3.3.0"
1313
},
1414
"devDependencies": {
1515
"@vitejs/plugin-vue": "^4.2.3",

examples/vue/dependent-queries/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@tanstack/vue-query": "^5.0.0-beta.0",
12-
"vue": "^3.2.47"
12+
"vue": "^3.3.0"
1313
},
1414
"devDependencies": {
1515
"@vitejs/plugin-vue": "^4.2.3",

examples/vue/persister/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"@tanstack/query-persist-client-core": "^5.0.0-beta.0",
1212
"@tanstack/query-sync-storage-persister": "^5.0.0-beta.0",
1313
"@tanstack/vue-query": "^5.0.0-beta.0",
14-
"vue": "^3.2.47"
14+
"vue": "^3.3.0"
1515
},
1616
"devDependencies": {
1717
"@vitejs/plugin-vue": "^4.2.3",

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"solid-js": "^1.7.8",
8484
"stream-to-array": "^2.3.0",
8585
"tsup": "^7.1.0",
86+
"ts-node": "^10.7.0",
8687
"type-fest": "^3.13.0",
8788
"typescript": "^5.0.4",
8889
"vite": "^4.4.4",

packages/vue-query/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"@tanstack/match-sorter-utils": "^8.8.4",
6262
"@tanstack/query-core": "workspace:*",
6363
"@vue/devtools-api": "^6.5.0",
64-
"vue-demi": "^0.13.11"
64+
"vue-demi": "^0.14.5"
6565
},
6666
"devDependencies": {
6767
"@vue/composition-api": "1.7.1",
@@ -71,7 +71,7 @@
7171
},
7272
"peerDependencies": {
7373
"@vue/composition-api": "^1.1.2",
74-
"vue": "^2.5.0 || ^3.0.0"
74+
"vue": "^2.5.0 || ^3.3.0"
7575
},
7676
"peerDependenciesMeta": {
7777
"@vue/composition-api": {

packages/vue-query/src/__tests__/useQueryClient.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { getCurrentInstance, inject } from 'vue-demi'
1+
import { hasInjectionContext, inject } from 'vue-demi'
22
import { vi } from 'vitest'
33
import { useQueryClient } from '../useQueryClient'
44
import { VUE_QUERY_CLIENT } from '../utils'
55
import type { Mock } from 'vitest'
66

77
describe('useQueryClient', () => {
88
const injectSpy = inject as Mock
9-
const getCurrentInstanceSpy = getCurrentInstance as Mock
9+
const hasInjectionContextSpy = hasInjectionContext as Mock
1010

1111
beforeEach(() => {
1212
vi.restoreAllMocks()
@@ -32,10 +32,10 @@ describe('useQueryClient', () => {
3232
})
3333

3434
test('should throw an error when used outside of setup function', () => {
35-
getCurrentInstanceSpy.mockReturnValueOnce(undefined)
35+
hasInjectionContextSpy.mockReturnValueOnce(false)
3636

3737
expect(useQueryClient).toThrowError()
38-
expect(getCurrentInstanceSpy).toHaveBeenCalledTimes(1)
38+
expect(hasInjectionContextSpy).toHaveBeenCalledTimes(1)
3939
})
4040

4141
test('should call inject with a custom key as a suffix', () => {

packages/vue-query/src/useQueryClient.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { getCurrentInstance, inject } from 'vue-demi'
1+
import { getCurrentScope, hasInjectionContext, inject } from 'vue-demi'
22

33
import { getClientKey } from './utils'
44
import type { QueryClient } from './queryClient'
55

66
export function useQueryClient(id = ''): QueryClient {
7-
const vm = getCurrentInstance()?.proxy
8-
9-
if (!vm) {
7+
if (
8+
// ensures that `inject()` can be used
9+
!hasInjectionContext() ||
10+
// ensures `ref()`, `onScopeDispose()` and other APIs can be used
11+
!getCurrentScope()
12+
) {
1013
throw new Error('vue-query hooks can only be used inside setup() function.')
1114
}
1215

0 commit comments

Comments
 (0)