@@ -10,28 +10,49 @@ A tRPC wrapper around @tanstack/vue-query. This package provides a set of hooks
10
10
pnpm add @falcondev-it/trpc-vue-query
11
11
```
12
12
13
- ## Usage
13
+ ## Usage with Vue
14
14
15
- ### 1. Create client
15
+ ### 1. Create client & composable
16
16
17
17
``` ts
18
18
import { createTRPCVueQueryClient } from ' @falcondev-it/trpc-vue-query'
19
19
import type { AppRouter } from ' ../your_server/trpc'
20
-
21
- export const trpc = createTRPCVueQueryClient <AppRouter >({
22
- links: [
23
- httpBatchLink ({
24
- url: ' /api/trpc' ,
25
- }),
26
- ],
20
+ import { VueQueryPlugin , useQueryClient } from ' @tanstack/vue-query'
21
+
22
+ app .use (VueQueryPlugin )
23
+ app .use ({
24
+ install(app ) {
25
+ const queryClient = useQueryClient ()
26
+ const trpc = createTRPCVueQueryClient <AppRouter >({
27
+ queryClient ,
28
+ trpc: {
29
+ links: [
30
+ httpBatchLink ({
31
+ url: ' /api/trpc' ,
32
+ }),
33
+ ],
34
+ },
35
+ })
36
+
37
+ app .provide (' trpc' , trpc )
38
+ },
27
39
})
28
40
```
29
41
42
+ ``` ts
43
+ import { createTRPCVueQueryClient } from ' @falcondev-it/trpc-vue-query'
44
+ import type { AppRouter } from ' ../your_server/trpc'
45
+
46
+ export function useTRPC() {
47
+ return inject (' trpc' ) as ReturnType <typeof createTRPCVueQueryClient <AppRouter >>
48
+ }
49
+ ```
50
+
30
51
### 2. Use it in your components
31
52
32
53
``` vue
33
54
<script lang="ts" setup>
34
- const { data: greeting } = trpc .hello.useQuery({ name: 'World' })
55
+ const { data: greeting } = useTRPC() .hello.useQuery({ name: 'World' })
35
56
</script>
36
57
<template>
37
58
<div>
@@ -44,7 +65,7 @@ const { data: greeting } = trpc.hello.useQuery({ name: 'World' })
44
65
45
66
``` vue
46
67
<script lang="ts" setup>
47
- const { data: greeting } = trpc .hello.useQuery(
68
+ const { data: greeting } = useTRPC() .hello.useQuery(
48
69
{ name: 'World' },
49
70
{
50
71
refetchOnMount: false,
@@ -67,7 +88,7 @@ const { data: greeting } = trpc.hello.useQuery(
67
88
``` vue
68
89
<script lang="ts" setup>
69
90
const name = ref('')
70
- const { mutate: updateGreeting } = trpc .hello.update.useMutation({
91
+ const { mutate: updateGreeting } = useTRPC() .hello.update.useMutation({
71
92
onSuccess: () => {
72
93
console.log('Greeting updated')
73
94
},
@@ -88,27 +109,39 @@ Setup `trpc-nuxt` as described in their [documentation](https://trpc-nuxt.vercel
88
109
89
110
``` ts
90
111
import { createTRPCVueQueryClient } from ' @falcondev-it/trpc-vue-query'
112
+ import { useQueryClient } from ' @tanstack/vue-query'
91
113
import { httpBatchLink } from ' trpc-nuxt/client'
92
114
import type { AppRouter } from ' ~/server/trpc/routers'
93
115
94
116
export default defineNuxtPlugin (() => {
117
+ const queryClient = useQueryClient ()
118
+
95
119
// ⬇️ use `createTRPCVueQueryClient` instead of `createTRPCNuxtClient` ⬇️
96
- const client = createTRPCVueQueryClient <AppRouter >({
97
- links: [
98
- httpBatchLink ({
99
- url: ' /api/trpc' ,
100
- }),
101
- ],
120
+ const trpc = createTRPCVueQueryClient <AppRouter >({
121
+ queryClient ,
122
+ trpc: {
123
+ links: [
124
+ httpBatchLink ({
125
+ url: ' /api/trpc' ,
126
+ }),
127
+ ],
128
+ },
102
129
})
103
130
104
131
return {
105
132
provide: {
106
- client ,
133
+ trpc ,
107
134
},
108
135
}
109
136
})
110
137
```
111
138
139
+ ``` ts
140
+ export function useTRPC() {
141
+ return useNuxtApp ().$trpc
142
+ }
143
+ ```
144
+
112
145
## Acknowledgements
113
146
114
147
Huge thanks to [ Robert Soriano] ( https://github.com/wobsoriano ) for creating ` nuxt-trpc ` ! We just adapted his work to work with Vue Query.
0 commit comments