Skip to content

Commit 898d865

Browse files
committed
docs: update README
1 parent 0a9349b commit 898d865

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

README.md

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,49 @@ A tRPC wrapper around @tanstack/vue-query. This package provides a set of hooks
1010
pnpm add @falcondev-it/trpc-vue-query
1111
```
1212

13-
## Usage
13+
## Usage with Vue
1414

15-
### 1. Create client
15+
### 1. Create client & composable
1616

1717
```ts
1818
import { createTRPCVueQueryClient } from '@falcondev-it/trpc-vue-query'
1919
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+
},
2739
})
2840
```
2941

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+
3051
### 2. Use it in your components
3152

3253
```vue
3354
<script lang="ts" setup>
34-
const { data: greeting } = trpc.hello.useQuery({ name: 'World' })
55+
const { data: greeting } = useTRPC().hello.useQuery({ name: 'World' })
3556
</script>
3657
<template>
3758
<div>
@@ -44,7 +65,7 @@ const { data: greeting } = trpc.hello.useQuery({ name: 'World' })
4465

4566
```vue
4667
<script lang="ts" setup>
47-
const { data: greeting } = trpc.hello.useQuery(
68+
const { data: greeting } = useTRPC().hello.useQuery(
4869
{ name: 'World' },
4970
{
5071
refetchOnMount: false,
@@ -67,7 +88,7 @@ const { data: greeting } = trpc.hello.useQuery(
6788
```vue
6889
<script lang="ts" setup>
6990
const name = ref('')
70-
const { mutate: updateGreeting } = trpc.hello.update.useMutation({
91+
const { mutate: updateGreeting } = useTRPC().hello.update.useMutation({
7192
onSuccess: () => {
7293
console.log('Greeting updated')
7394
},
@@ -88,27 +109,39 @@ Setup `trpc-nuxt` as described in their [documentation](https://trpc-nuxt.vercel
88109

89110
```ts
90111
import { createTRPCVueQueryClient } from '@falcondev-it/trpc-vue-query'
112+
import { useQueryClient } from '@tanstack/vue-query'
91113
import { httpBatchLink } from 'trpc-nuxt/client'
92114
import type { AppRouter } from '~/server/trpc/routers'
93115

94116
export default defineNuxtPlugin(() => {
117+
const queryClient = useQueryClient()
118+
95119
// ⬇️ 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+
},
102129
})
103130

104131
return {
105132
provide: {
106-
client,
133+
trpc,
107134
},
108135
}
109136
})
110137
```
111138

139+
```ts
140+
export function useTRPC() {
141+
return useNuxtApp().$trpc
142+
}
143+
```
144+
112145
## Acknowledgements
113146

114147
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

Comments
 (0)