Skip to content

Commit e8a4dac

Browse files
feat(VueQueryPlugin): replace useQueryProvider with VueQueryPlugin (#116)
* feat: create VueQueryPlugin * docs: update Installation section * feat(plugin): vue2 plugin * feat(plugin): unmount hook * build: remove plugin, add external deps * test: vueQueryPlugin * docs: update some docs * fix: import type from core, export plugin options type * chore: update examples * feat: multiple clients support * fix: multi client support * refactor: query client key helper * docs: update custom clients page Co-authored-by: Alexey Antipov <[email protected]>
1 parent 4d43dcd commit e8a4dac

29 files changed

+589
-107
lines changed

README.md

+7-12
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,15 @@ For topics not covered in vue-query docs visit https://react-query.tanstack.com/
3939

4040
# Quick Start
4141

42-
1. Attach **Vue Query** to the root component of your Vue application
42+
1. Initialize **Vue Query** via **VueQueryPlugin**
4343

4444
```ts
45-
import { defineComponent } from "vue";
46-
import { useQueryProvider } from "vue-query";
45+
import { createApp } from "vue";
46+
import { VueQueryPlugin } from "vue-query";
4747

48-
export default defineComponent({
49-
name: "App",
50-
setup() {
51-
useQueryProvider();
52-
},
53-
});
48+
import App from "./App.vue";
49+
50+
createApp(App).use(VueQueryPlugin).mount("#app");
5451
```
5552

5653
2. Use query
@@ -75,9 +72,7 @@ For topics not covered in vue-query docs visit https://react-query.tanstack.com/
7572

7673
```ts
7774
const id = ref(1);
78-
const queryKey = ["todos", id];
79-
const queryFunction = () => getTodos(id);
8075
const enabled = ref(false);
8176

82-
const query = useQuery(queryKey, queryFunction, { enabled });
77+
const query = useQuery(["todos", id], () => getTodos(id), { enabled });
8378
```

docs/getting-started/devtools.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ When you begin your Vue Query journey, you'll want these DevTools by your side.
66

77
The DevTools are bundle split into the vue-query/devtools package. No need to install anything extra, just:
88

9-
```
9+
```ts
1010
import { VueQueryDevTools } from "vue-query/devtools";
1111
```
1212

@@ -20,7 +20,7 @@ Floating Mode will mount the DevTools as a fixed, floating element in your app a
2020

2121
Place the following code as high in your Vue app as you can. The closer it is to the root of the page, the better it will work!
2222

23-
```
23+
```vue
2424
<script lang="ts">
2525
import { defineComponent } from "vue";
2626
import { VueQueryDevTools } from "vue-query/devtools";
@@ -56,7 +56,7 @@ export default defineComponent({
5656

5757
Embedded Mode will embed the DevTools as a regular component in your application. You can style it however you'd like after that!
5858

59-
```
59+
```vue
6060
<script lang="ts">
6161
import { defineComponent } from "vue";
6262
import { VueQueryDevToolsPanel } from "vue-query/devtools";

docs/getting-started/installation.md

+24
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,27 @@ yarn add vue-query
1313
```
1414

1515
Vue Query is compatible with Vue 2.x + composition API or 3.x
16+
17+
### Vue Query Initialization
18+
19+
Before starting using Vue Query, you need to initialize it.
20+
21+
There are 2 possible ways to do it.
22+
23+
1. Using `VueQueryPlugin` (recommended)
24+
25+
```ts
26+
import { VueQueryPlugin } from "vue-query";
27+
28+
app.use(VueQueryPlugin);
29+
```
30+
31+
2. Calling `useQueryProvider` in the root component
32+
33+
```vue
34+
<script setup>
35+
import { useQueryProvider } from 'vue-query';
36+
37+
useQueryProvider();
38+
</script>
39+
```

docs/guides/custom-clients.md

+33-15
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,47 @@ Vue Query allows providing custom `QueryClient` for Vue context.
44

55
It might be handy when you need to create `QueryClient` beforehand to integrate it with other libraries that do not have access to the Vue context.
66

7-
For this reason, `useQueryProvider` accepts either QueryClient options or `QueryClient` as a first argument.
7+
For this reason, `VueQueryPlugin` accepts either `QueryClientConfig` or `QueryClient` as a plugin options.
88

9-
If You provide QueryClient options, `QueryClient` instance will be created internally and provided to Vue context.
9+
If You provide `QueryClientConfig`, `QueryClient` instance will be created internally and provided to Vue context.
1010

11-
```js
12-
useQueryProvider(queryClientOptions);
11+
```ts
12+
const vueQueryPluginOptions: VueQueryPluginOptions = {
13+
queryClientConfig: {
14+
defaultOptions: { queries: { staleTime: 3600 } },
15+
},
16+
};
17+
app.use(VueQueryPlugin, vueQueryPluginOptions);
1318
```
1419

15-
```js
16-
const myClient = new QueryClient(queryClientOptions);
17-
useQueryProvider(myClient);
20+
```ts
21+
const myClient = new QueryClient(queryClientConfig);
22+
const vueQueryPluginOptions: VueQueryPluginOptions = {
23+
queryClient: myClient,
24+
};
25+
app.use(VueQueryPlugin, vueQueryPluginOptions);
1826
```
1927

2028
### Custom context keys
2129

2230
You can also customize the key under which `QueryClient` will be accessible in Vue context. This can be usefull is you want to avoid name clashing between multiple apps on the same page.
2331

24-
It works both with default, and custom provided `QueryClient`
32+
It works both with default, and custom `QueryClient`
2533

26-
```js
27-
useQueryProvider(queryClientOptions, "foo");
34+
```ts
35+
const vueQueryPluginOptions: VueQueryPluginOptions = {
36+
queryClientKey: "Foo",
37+
};
38+
app.use(VueQueryPlugin, vueQueryPluginOptions);
2839
```
2940

30-
```js
31-
const myClient = new QueryClient(queryClientOptions);
32-
useQueryProvider(myClient, "bar");
41+
```ts
42+
const myClient = new QueryClient(queryClientConfig);
43+
const vueQueryPluginOptions: VueQueryPluginOptions = {
44+
queryClient: myClient,
45+
queryClientKey: "Foo",
46+
};
47+
app.use(VueQueryPlugin, vueQueryPluginOptions);
3348
```
3449

3550
To use the custom client key, You have to provide it as a query options
@@ -48,6 +63,9 @@ Devtools also support this by the `queryClientKeys` prop. You can provide multip
4863

4964
Internally custom key will be combined with default query key as a suffix. But user do not have to worry about it.
5065

51-
```js
52-
useQueryProvider(queryClientOptions, "foo"); // -> VUE_QUERY_CLIENT:foo
66+
```ts
67+
const vueQueryPluginOptions: VueQueryPluginOptions = {
68+
queryClientKey: "Foo",
69+
};
70+
app.use(VueQueryPlugin, vueQueryPluginOptions); // -> VUE_QUERY_CLIENT:Foo
5371
```

docs/guides/window-focus-refetching.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ If a user leaves your application and returns to stale data, **Vue Query automat
33
### Disabling globally
44

55
```ts
6-
import { defineComponent } from "vue";
7-
import { useQueryProvider } from "vue-query";
8-
9-
export default defineComponent({
10-
name: "App",
11-
setup() {
12-
useQueryProvider({
13-
defaultOptions: {
14-
queries: {
15-
refetchOnWindowFocus: false,
16-
},
6+
import { createApp } from "vue";
7+
import { VueQueryPlugin, VueQueryPluginOptions } from "vue-query";
8+
9+
import App from "./App.vue";
10+
11+
const vueQueryPluginOptions: VueQueryPluginOptions = {
12+
queryClientConfig: {
13+
defaultOptions: {
14+
queries: {
15+
refetchOnWindowFocus: false,
1716
},
18-
});
17+
},
1918
},
20-
});
19+
};
20+
21+
createApp(App).use(VueQueryPlugin, vueQueryPluginOptions).mount("#app");
2122
```
2223

2324
### Disabling Per-Query

examples/2.x-basic/src/App.vue

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script lang="ts">
22
import { defineComponent, ref } from "@vue/composition-api";
3-
import { useQueryProvider } from "vue-query";
43
import { VueQueryDevTools } from "vue-query/devtools";
54
65
import Posts from "./Posts.vue";
@@ -10,8 +9,6 @@ export default defineComponent({
109
name: "App",
1110
components: { Posts, Post, VueQueryDevTools },
1211
setup() {
13-
useQueryProvider();
14-
1512
const visitedPosts = ref(new Set());
1613
const isVisited = (id: number) => visitedPosts.value.has(id);
1714

examples/2.x-basic/src/main.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import Vue from "vue";
22
import VueCompositionApi, { createApp, h } from "@vue/composition-api";
3+
import { VueQueryPlugin } from "vue-query";
34

45
import App from "./App.vue";
56

67
Vue.use(VueCompositionApi);
8+
Vue.use(VueQueryPlugin);
79

810
createApp({
911
render() {

examples/basic/src/App.vue

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script lang="ts">
22
import { defineComponent, ref } from "vue";
3-
import { useQueryProvider } from "vue-query";
43
import { VueQueryDevTools } from "vue-query/devtools";
54
65
import Posts from "./Posts.vue";
@@ -10,8 +9,6 @@ export default defineComponent({
109
name: "App",
1110
components: { Posts, Post, VueQueryDevTools },
1211
setup() {
13-
useQueryProvider();
14-
1512
const visitedPosts = ref(new Set());
1613
const isVisited = (id: number) => visitedPosts.value.has(id);
1714

examples/basic/src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApp } from "vue";
2+
import { VueQueryPlugin } from "vue-query";
23

34
import App from "./App.vue";
45

5-
createApp(App).mount("#app");
6+
createApp(App).use(VueQueryPlugin).mount("#app");

examples/multi-client/src/App.vue

-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
<script lang="ts">
22
import { defineComponent } from "vue";
3-
import { useQueryProvider, QueryClient } from "vue-query";
43
import { VueQueryDevTools } from "vue-query/devtools";
54
65
import Todos from "./Todos.vue";
76
87
export default defineComponent({
98
name: "App",
109
components: { Todos, VueQueryDevTools },
11-
setup() {
12-
const myClient = new QueryClient();
13-
useQueryProvider();
14-
useQueryProvider({}, "Foo");
15-
useQueryProvider(myClient, "Bar");
16-
},
1710
});
1811
</script>
1912

examples/multi-client/src/main.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import { createApp } from "vue";
2+
import { VueQueryPlugin, VueQueryPluginOptions, QueryClient } from "vue-query";
23

34
import App from "./App.vue";
45

5-
createApp(App).mount("#app");
6+
const fooClient = new QueryClient();
7+
const barClient = new QueryClient();
8+
9+
const vueQueryPluginOptions: VueQueryPluginOptions = {
10+
additionalClients: [
11+
{
12+
queryClient: fooClient,
13+
queryClientKey: "Foo",
14+
},
15+
{
16+
queryClient: barClient,
17+
queryClientKey: "Bar",
18+
},
19+
],
20+
};
21+
22+
createApp(App).use(VueQueryPlugin, vueQueryPluginOptions).mount("#app");

examples/multi-page/src/App.vue

-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<script lang="ts">
22
import { defineComponent, ref } from "vue";
3-
4-
import { useQueryProvider } from "vue-query";
53
import { VueQueryDevTools } from "vue-query/devtools";
64
import Page from "./Page.vue";
75
86
export default defineComponent({
97
name: "App",
108
components: { VueQueryDevTools, Page },
119
setup() {
12-
useQueryProvider();
13-
1410
const firstPage = ref(1);
1511
1612
const changePage = () => {

examples/multi-page/src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApp } from "vue";
2+
import { VueQueryPlugin } from "vue-query";
23

34
import App from "./App.vue";
45

5-
createApp(App).mount("#app");
6+
createApp(App).use(VueQueryPlugin).mount("#app");

examples/simple/src/App.vue

-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<script lang="ts">
22
import { defineComponent } from "vue";
3-
import { useQueryProvider } from "vue-query";
43
import { VueQueryDevTools } from "vue-query/devtools";
54
65
import Todos from "./Todos.vue";
76
87
export default defineComponent({
98
name: "App",
109
components: { Todos, VueQueryDevTools },
11-
setup() {
12-
useQueryProvider();
13-
},
1410
});
1511
</script>
1612

examples/simple/src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApp } from "vue";
2+
import { VueQueryPlugin } from "vue-query";
23

34
import App from "./App.vue";
45

5-
createApp(App).mount("#app");
6+
createApp(App).use(VueQueryPlugin).mount("#app");

examples/suspense/src/App.vue

-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<script lang="ts">
22
import { defineComponent } from "vue";
3-
import { useQueryProvider } from "vue-query";
43
import { VueQueryDevTools } from "vue-query/devtools";
54
65
import Content from "./Content.vue";
76
87
export default defineComponent({
98
name: "App",
109
components: { Content, VueQueryDevTools },
11-
setup() {
12-
useQueryProvider();
13-
},
1410
});
1511
</script>
1612

examples/suspense/src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createApp } from "vue";
2+
import { VueQueryPlugin } from "vue-query";
23

34
import App from "./App.vue";
45

5-
createApp(App).mount("#app");
6+
createApp(App).use(VueQueryPlugin).mount("#app");

0 commit comments

Comments
 (0)