Open History to see the last commit
1---
2id: custom-client
3title: Custom Client
4---
5β
6### Custom client
7β
8Vue Query allows providing custom `QueryClient` for Vue context.
9β
10It 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.
11β
12For this reason, `VueQueryPlugin` accepts either `QueryClientConfig` or `QueryClient` as a plugin options.
13β
14If You provide `QueryClientConfig`, `QueryClient` instance will be created internally and provided to Vue context.
15β
16```tsx
17const vueQueryPluginOptions: VueQueryPluginOptions = {
18 queryClientConfig: {
19 defaultOptions: { queries: { staleTime: 3600 } },
20 },
21}
22app.use(VueQueryPlugin, vueQueryPluginOptions)
23```
24β
25```tsx
26const myClient = new QueryClient(queryClientConfig)
27const vueQueryPluginOptions: VueQueryPluginOptions = {
28 queryClient: myClient,
29}
30app.use(VueQueryPlugin, vueQueryPluginOptions)
31```
32β
33### Custom context key
34β
35You can also customize the key under which `QueryClient` will be accessible in Vue context. This can be useful is you want to avoid name clashing between multiple apps on the same page with Vue2.
36β
37It works both with default, and custom `QueryClient`
38β
39```tsx
40const vueQueryPluginOptions: VueQueryPluginOptions = {
41 queryClientKey: 'Foo',
42}
43app.use(VueQueryPlugin, vueQueryPluginOptions)
44```
45β
46```tsx
47const myClient = new QueryClient(queryClientConfig)
48const vueQueryPluginOptions: VueQueryPluginOptions = {
49 queryClient: myClient,
50 queryClientKey: 'Foo',
51}
52app.use(VueQueryPlugin, vueQueryPluginOptions)
53```
54β
55To use the custom client key, You have to provide it as a query options
56β
57```js
58useQuery({
59 queryKey: ['query1'],
60 queryFn: fetcher,
61 queryClientKey: 'foo',
62})
63```
64β
65Internally custom key will be combined with default query key as a suffix. But user do not have to worry about it.
66β
67```tsx
68const vueQueryPluginOptions: VueQueryPluginOptions = {
69 queryClientKey: 'Foo',
70}
71app.use(VueQueryPlugin, vueQueryPluginOptions) // -> VUE_QUERY_CLIENT:Foo
72```