@falcondev-oss/trpc-vue-query

中文文档
0.7.0发布于 2026/09/23MITTypeScriptESM + CJS构建来源已验证近 30 天下载 3620

A tRPC wrapper around '@tanstack/vue-query'

原始使用文档
文档目录15 节
  1. Installation
  2. Documentation
  3. Usage with Vue
  4. 1. Create client & composable
  5. 2. Use it in your components
  6. 3. Passing vue-query options
  7. 4. Using the useMutation hook
  8. 5. Reactive parameters
  9. Helpers
  10. invalidate()
  11. setQueryData()
  12. key()
  13. Operation context
  14. Usage with trpc-nuxt
  15. Acknowledgements

tRPC Vue Query

NPM version

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-query

Documentation

👉 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>

5. Reactive parameters

Input parameters can be refs — the query refetches when they change.

<script lang="ts" setup>
const productId = ref(1)
const { data: product } = useTRPC().product.getById.useQuery(productId)
</script>

Helpers

invalidate()

Invalidate and refetch a query.

<script lang="ts" setup>
const trpc = useTRPC()
const { mutate: addToCart } = trpc.cart.addProduct.useMutation({
  onSuccess: () => {
    // this will invalidate and refetch the `cart.get` query
    trpc.cart.get.invalidate()
  },
})
</script>

setQueryData()

Update the query data manually.

<script lang="ts" setup>
const trpc = useTRPC()
const { mutate: addToCart } = trpc.cart.addProduct.useMutation({
  onSuccess: (newCart) => {
    // this will update the `cart.get` query data
    trpc.cart.get.setQueryData(newCart)
  },
})
</script>

key()

Get the query key. With the key you can access all the other TanStack Query features.

<script lang="ts" setup>
const trpc = useTRPC()
const cartKey = trpc.cart.get.key()

const productKey = trpc.product.getById.key(1)

// eg. cancel queries by key:
const queryClient = useQueryClient()
await queryClient.cancelQueries({ queryKey: cartKey })
</script>

Operation context

Every request made through a vue-query composable (useQuery, useQueries, useInfiniteQuery, useMutation, queryOptions) is marked with the exported vueQueryContext symbol in the tRPC operation context. Plain query() / mutate() calls are not.

The symbol is declaration-merged into tRPC's OperationContext interface, so op.context[vueQueryContext] is typed inside links.

Links can use this to skip handling that vue-query already does, e.g. error toasts coming from the query/mutation cache:

import { vueQueryContext } from '@falcondev-oss/trpc-vue-query'

const errorToastLink: TRPCLink<AppRouter> =
  () =>
  ({ op, next }) =>
    observable((observer) =>
      next(op).subscribe({
        next: (value) => observer.next(value),
        complete: () => observer.complete(),
        error(err) {
          // vue-query requests are toasted by the query/mutation cache instead
          if (!(vueQueryContext in op.context)) toast.error(err.message)
          observer.error(err)
        },
      }),
    )

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.

版本与发布记录

@falcondev-oss/trpc-vue-query 共发布 30 个版本,首次发布于 2024/03/18;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.7.0latest2026/09/231 个月前
0.6.12026/08/252 个月前
0.5.42026/04/206 个月前
0.5.32026/03/187 个月前
0.5.22025/07/301.3 年前
0.5.12025/07/181.3 年前

其他发布通道:next → 0.5.0-rc.20。

关于 @falcondev-oss/trpc-vue-query 的常见问题

@falcondev-oss/trpc-vue-query 是什么?

A tRPC wrapper around '@tanstack/vue-query'

如何安装 @falcondev-oss/trpc-vue-query?

在项目目录运行 npm install @falcondev-oss/trpc-vue-query,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(^3.5.30)、@trpc/client(^11.13.4)、@trpc/server(^11.13.4)、@tanstack/vue-query(^5.102.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@falcondev-oss/trpc-vue-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@falcondev-oss/trpc-vue-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@falcondev-oss/trpc-vue-query 可以商用吗?

@falcondev-oss/trpc-vue-query 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@falcondev-oss/trpc-vue-query 的下载量有多少?

根据 npm 官方下载统计,@falcondev-oss/trpc-vue-query 在 2026/10/14 – 2026/11/12 这 30 天内被下载 3,620 次,日均约 121 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@falcondev-oss/trpc-vue-query 还在维护吗?

最新版本 0.7.0 发布于 2026/09/23(1 个月前);至今共发布 30 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/03/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。