0.5.0 ⢠Published 6 months ago
@falcondev-oss/trpc-vue-query v0.5.0
tRPC Vue Query
A tRPC wrapper around @tanstack/vue-query. This package provides a set of hooks to use tRPC with Vue Query.
Installation
pnpm add @falcondev-oss/trpc-vue-queryDocumentation
š https://trpc-vue-query.falcondev.io/getting-started š
Usage with Vue
1. Create client & composable
import { createTRPCVueQueryClient } from '@falcondev-oss/trpc-vue-query'
import { VueQueryPlugin, useQueryClient } from '@tanstack/vue-query'
import type { AppRouter } from '../your_server/trpc'
app.use(VueQueryPlugin)
app.use({
install(app) {
const queryClient = app.runWithContext(useQueryClient)
const trpc = createTRPCVueQueryClient<AppRouter>({
queryClient,
trpc: {
links: [
httpBatchLink({
url: '/api/trpc',
}),
],
},
})
app.provide('trpc', trpc)
},
})import { createTRPCVueQueryClient } from '@falcondev-oss/trpc-vue-query'
import type { AppRouter } from '../your_server/trpc'
export function useTRPC() {
return inject('trpc') as ReturnType<typeof createTRPCVueQueryClient<AppRouter>>
}2. Use it in your components
<script lang="ts" setup>
const { data: greeting } = useTRPC().hello.useQuery({ name: 'World' })
</script>
<template>
<div>
<h1>{{ greeting }}</h1>
</div>
</template>3. Passing vue-query options
<script lang="ts" setup>
const { data: greeting } = useTRPC().hello.useQuery(
{ name: 'World' },
{
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false,
staleTime: 1000 * 60 * 5,
},
)
</script>
<template>
<div>
<h1>{{ greeting }}</h1>
</div>
</template>4. Using the useMutation hook
<script lang="ts" setup>
const name = ref('')
const { mutate: updateGreeting } = useTRPC().hello.update.useMutation({
onSuccess: () => {
console.log('Greeting updated')
},
})
</script>
<template>
<div>
<input v-model="name" type="text" />
<button @click="() => updateGreeting({ name })">Update greeting</button>
</div>
</template>Usage with trpc-nuxt
Setup trpc-nuxt as described in their documentation. Then update the plugins/client.ts file:
import { createTRPCVueQueryClient } from '@falcondev-oss/trpc-vue-query'
import { useQueryClient } from '@tanstack/vue-query'
import { httpBatchLink } from 'trpc-nuxt/client'
import type { AppRouter } from '~/server/trpc/routers'
export default defineNuxtPlugin(() => {
const queryClient = useQueryClient()
// ā¬ļø use `createTRPCVueQueryClient` instead of `createTRPCNuxtClient` ā¬ļø
const trpc = createTRPCVueQueryClient<AppRouter>({
queryClient,
trpc: {
links: [
httpBatchLink({
url: '/api/trpc',
}),
],
},
})
return {
provide: {
trpc,
},
}
})export function useTRPC() {
return useNuxtApp().$trpc
}Acknowledgements
Huge thanks to Robert Soriano for creating trpc-nuxt! We just adapted his work to work with Vue Query.
0.5.0-rc.11
11 months ago
0.5.0-rc.10
11 months ago
0.5.0-rc.9
11 months ago
0.5.0-rc.13
11 months ago
0.5.0-rc.12
11 months ago
0.5.0-rc.15
11 months ago
0.5.0-rc.14
11 months ago
0.5.0-rc.17
11 months ago
0.5.0-rc.16
11 months ago
0.5.0-rc.18
9 months ago
0.5.0-rc.8
12 months ago
0.5.0-rc.20
6 months ago
0.5.0
6 months ago
0.5.0-rc.7
1 year ago
0.5.0-rc.2
1 year ago
0.5.0-rc.4
1 year ago
0.5.0-rc.3
1 year ago
0.5.0-rc.6
1 year ago
0.5.0-rc.5
1 year ago
0.5.0-rc.1
1 year ago
0.5.0-rc.0
1 year ago
0.4.2
2 years ago
0.4.1
2 years ago
0.4.0
2 years ago