@connectrpc/protoc-gen-connect-query

中文文档
2.3.1发布于 2026/08/09Apache-2.0构建来源已验证近 30 天下载 56.5万

Code generator for connect-query

原始使用文档
文档目录15 节
  1. Installation
  2. Generating Code
  3. example.proto
  4. buf.gen.yaml
  5. With the buf CLI
  6. With protoc
  7. With Node
  8. Generated Output
  9. Plugin options
  10. target
  11. import_extension
  12. js_import_style
  13. keep_empty_files=true
  14. ts_nocheck=true
  15. Example Generated Code

@connectrpc/protoc-gen-connect-query

The code generator for Connect-Query, a expansion pack for TanStack Query (react-query), that enables effortless communication with servers that speak the Connect Protocol.

Learn more about Connect-Query at github.com/connectrpc/connect-query-es.

Installation

protoc-gen-connect-query is a code generator plugin for Protocol Buffer compilers like buf and protoc. It generates clients from your Protocol Buffer schema, and works in tandem with @bufbuild/protoc-gen-es, the code generator plugin for all Protocol Buffer base types. The code those two plugins generate requires the runtime libraries @connectrpc/connect-query, and @bufbuild/protobuf.

To install the plugins and their runtime libraries, run:

npm install --save-dev @connectrpc/protoc-gen-connect-query @bufbuild/protoc-gen-es
npm install @connectrpc/connect-query @bufbuild/protobuf

We use peer dependencies to ensure that code generator and runtime library are compatible with each other. Note that yarn and pnpm only emit a warning in this case.

Generating Code

example.proto

For these examples, consider the following example proto file example.proto:

syntax = "proto3";

package example.v1;

message Nothing {}

message Todo {
  string id = 1;
  string name = 2;
  bool completed = 3;
}

message Todos {
  repeated Todo todos = 1;
}

service TodoService {
  rpc GetTodos(Nothing) returns (Todos);
  rpc AddTodo(Todo) returns (Nothing);
}

This file creates an RPC service with the following:

  • GetTodos takes no inputs and returns an array of Todos.
  • AddTodo adds a new Todo and returns nothing.

buf.gen.yaml

Add a new configuration file buf.gen.yaml

version: v2
plugins:
  # This will invoke protoc-gen-es and write output to src/gen
  - local: protoc-gen-es
    out: src/gen
    opt: target=ts
    # This will invoke protoc-gen-connect-query
  - local: protoc-gen-connect-query
    out: src/gen
    opt: target=ts

With the buf CLI

To use the buf CLI to generate code for all protobuf files within your project, simply run:

npx @bufbuild/buf generate

Note that buf can generate from various inputs, not just local protobuf files. For example, npm run generate buf.build/connectrpc/eliza generates code for the module connectrpc/eliza on the Buf Schema Registry.

With protoc

PATH=$PATH:$(pwd)/node_modules/.bin \
  protoc -I . \
  --es_out src/gen \
  --es_opt target=ts \
  --connect-query_out src/gen \
  --connect-query_opt target=ts \
  example.proto

Note that we are adding node_modules/.bin to the $PATH, so that the protocol buffer compiler can find them. This happens automatically with npm scripts.

Note: Since yarn v2 and above does not use a node_modules directory, you need to change the variable a bit:

PATH=$(dirname $(yarn bin protoc-gen-es)):$(dirname $(yarn bin protoc-gen-connect-es)):$PATH

With Node

Add a line to the scripts section of your package.json to run buf generate.

"scripts": {
    ...
    "buf:generate": "buf generate"
},

Finally, tell Buf to generate code by running your command:

npm run buf:generate

Now you should see your generated code:

.
└── gen/
    ├── example_pb.ts
    └── example-TodoService_connectquery.ts

Generated Output

Connect-Query will create one output file for every service in every protofile. Say you have the following file structure:

.
└── proto/
    ├── pizza.proto
    └── curry.proto

Where pizza.proto contains DetroitStyleService and ChicagoStyleService, and where curry.proto contains VindalooService. Your generated output will look like this:

.
└── gen/
    ├── pizza_pb.ts
    ├── pizza-DetroitStyleService_connectquery.ts
    ├── pizza-ChicagoStyleService_connectquery.ts
    ├── curry_pb.ts
    └── curry-VindalooService_connectquery.ts

The reason each service gets a separate file is to facilitate intellisense and language server protocol imports. Notice that one file per input proto is generated by protoc-gen-es (pizza_pb.ts and curry_pb.ts), and that one file per service is created by protoc-gen-connect-query (making up the remainder). The Protobuf-ES generated files (*_pb.ts) are important because those files are referenced from the *_connectquery.ts files.

Plugin options

target

This option controls whether the plugin generates JavaScript, TypeScript, or TypeScript declaration files.

Say, for example, you used example.proto:

TargetGenerated output
target=jsexample-TodoService_connectquery.js
target=tsexample-TodoService_connectquery.ts
target=dtsexample-TodoService_connectquery.d.ts

Multiple values can be given by separating them with +, for example target=js+dts.

By default, we generate JavaScript and TypeScript declaration files, which produces the smallest code size and is the most compatible with various bundler configurations. If you prefer to generate TypeScript, use target=ts.

import_extension

By default, protoc-gen-connect-query (and all other plugins based on @bufbuild/protoplugin) doesn't add file extensions to import paths. However, some environments require an import extension. For example, using ECMAScript modules in Node.js requires the .js extension, and Deno requires .ts. With this plugin option, you can add .js/.ts extensions in import paths with the given value. Possible values:

  • import_extension=none: Doesn't add an extension. (Default)
  • import_extension=js: Adds the .js extension.
  • import_extension=ts. Adds the .ts extension.

js_import_style

By default, protoc-gen-connect-query (and all other plugins based on @bufbuild/protoplugin) generate ECMAScript import and export statements. For use cases where CommonJS is difficult to avoid, this option can be used to generate CommonJS require() calls.

Possible values:

  • js_import_style=module generate ECMAScript import / export statements - the default behavior.
  • js_import_style=legacy_commonjs generate CommonJS require() calls.

keep_empty_files=true

This option exists for other plugins but is not applicable to protoc-gen-connect-query because, unlike most other plugins, it does not generate a maximum of one output file for every input proto file. Instead, it generates one output file per service. If you provide a valid proto file that contains no services, protoc-gen-connect-query will have no output.

ts_nocheck=true

protoc-gen-connect-query generates valid TypeScript for current versions of the TypeScript compiler with standard settings. If you use compiler settings that yield an error for generated code, setting this option generates an annotation at the top of each file to skip type checks: // @ts-nocheck.

Example Generated Code

See eliza.proto for example inputs, and look here to see the outputs those files generate.

版本与发布记录

@connectrpc/protoc-gen-connect-query 共发布 33 个版本,首次发布于 2023/08/22;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.3.1latest2026/08/093 个月前
2.2.02025/09/101.2 年前
2.1.12025/07/101.3 年前
2.1.02025/05/231.5 年前
2.0.12024/12/141.9 年前
2.0.02024/11/202 年前

其他发布通道:beta → 2.0.0-beta.3,rc → 2.0.0-rc.3。

关于 @connectrpc/protoc-gen-connect-query 的常见问题

@connectrpc/protoc-gen-connect-query 是什么?

Code generator for connect-query

如何安装 @connectrpc/protoc-gen-connect-query?

在项目目录运行 npm install @connectrpc/protoc-gen-connect-query,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 protoc-gen-connect-query,可以用 npx protoc-gen-connect-query 直接运行。

@connectrpc/protoc-gen-connect-query 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/connectrpc__protoc-gen-connect-query 是否存在。

@connectrpc/protoc-gen-connect-query 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@connectrpc/protoc-gen-connect-query 可以商用吗?

@connectrpc/protoc-gen-connect-query 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@connectrpc/protoc-gen-connect-query 的下载量有多少?

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

@connectrpc/protoc-gen-connect-query 还在维护吗?

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