# @as-integrations/h3 中文文档

规范地址：https://npmcn.com/package/%40as-integrations/h3/

版本：2\.0\.3
npm 元数据更新时间：2026-02-04T15:53:05.353Z
中文译文更新时间：2026-11-06T21:59:31.256Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40as-integrations/h3
原文对照：https://npmcn.com/package/%40as-integrations/h3/?lang=original
项目主页：https://github.com/apollo-server-integrations/apollo-server-integration-h3#readme
源码仓库：https://github.com/apollo-server-integrations/apollo-server-integration-h3

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：23.8 kB，13 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=20\.0\.0
- 运行依赖：1 个
- peer 依赖：@apollo/server ^4\.1\.1 \|\| ^5\.0\.0，crossws ^0\.3\.0，graphql ^16\.0\.0，graphql-ws ^5\.0\.0 \|\| ^6\.0\.0，h3 ^1\.11\.0
- 首次发布：2022-10-15T22:04:07.455Z
- 最新版本发布：2026-02-04T15:53:04.608Z
- 近 30 天下载量：12133（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 2 个）

## 中文简介

用于 h3 或 Nuxt 的阿波罗服务器集成

## 安装

```sh
npm install @as-integrations/h3
```

也可以使用：`pnpm add @as-integrations/h3`、`yarn add @as-integrations/h3`、`bun add @as-integrations/h3`

## README

# H3 和 nuxt 的阿波罗服务器集成

[![npm version](https://img.shields.io/npm/v/@as-integrations/h3?style=flat-square)](https://npmjs.com/package/@as-integrations/h3) [![npm downloads](https://img.shields.io/npm/dm/@as-integrations/h3?style=flat-square)](https://npmjs.com/package/@as-integrations/h3) [![Github Actions](https://img.shields.io/github/workflow/status/apollo-server-integrations/apollo-server-integration-h3/ci/main?style=flat-square)](https://github.com/apollo-server-integrations/apollo-server-integration-h3/actions?query=workflow%3Aci) [![Codecov](https://img.shields.io/codecov/c/gh/apollo-server-integrations/apollo-server-integration-h3/main?style=flat-square)](https://codecov.io/gh/apollo-server-integrations/apollo-server-integration-h3)

此软件包允许您轻松整合 [阿波罗服务器](https://www.apollographql.com/docs/apollo-server/) 还有你的 [h3 (韩语)](https://github.com/unjs/h3) 或者说 [数值 3](https://npmcn.com/package/%40as-integrations/h3/v3.nuxtjs.org) 应用.

> 在 Nuxt 3 中定义 GraphQL 服务器时, 您可能需要查看 [GraphQL 服务器工具包 Nuxt 模块](https://github.com/tobiasdiez/nuxt-graphql-server)。 。 。 .

## 安装

```sh
# npm
npm install @apollo/server graphql @as-integrations/h3

# yarn
yarn add @apollo/server graphql @as-integrations/h3

# pnpm
pnpm add @apollo/server graphql @as-integrations/h3
```

## 与 Nuxt v3 用法

创建一个 [服务器 API 路由](https://v3.nuxtjs.org/guide/directory-structure/server#api-routes) 配置 Apollo 服务器实例 [文档](https://www.apollographql.com/docs/apollo-server/getting-started#step-6-create-an-instance-of-apolloserver) 然后导出为事件处理器 :

```js
import { ApolloServer } from '@apollo/server'
import { startServerAndCreateH3Handler } from '@as-integrations/h3'

const apollo = new ApolloServer({
  // Specify server options like schema and resolvers here
})

export default startServerAndCreateH3Handler(apollo, {
  // Optional: Specify context
  context: (event) => {
    /*...*/
  },
})
```

## 用h3时

创建并配置 Apollo 服务器实例 [文档](https://www.apollographql.com/docs/apollo-server/getting-started#step-6-create-an-instance-of-apolloserver) 并注册为您的路径处理器 `h3` 应用.

```js
import { createApp } from 'h3'
import { ApolloServer } from '@apollo/server'
import { startServerAndCreateH3Handler } from '@as-integrations/h3'

const apollo = new ApolloServer({
  // Specify server options like schema and resolvers here
})

export const app = createApp()
app.use(
  '/api',
  startServerAndCreateH3Handler(apollo, {
    // Optional: Specify context
    context: (event) => {
      /*...*/
    },
  }),
)
```

然后按常规运行您的 h3 服务器, 例如: `npx --yes listhen -w --open ./app.ts`。 。 。 . 访问 http://localhost:30/api 在您的浏览器中访问阿波罗沙盒.

## 使用 WebSockets 订阅

此软件包还支持通过 WebSockets 订阅. 要启用此特性, 您需要安装 `graphql-ws` 软件包 :

```sh
# npm
npm install graphql-ws

# yarn
yarn add graphql-ws

# pnpm
pnpm add graphql-ws
```

然后,你可以在您的 WebSocket 处理器中添加 `h3` 应用 `defineGraphqlWebSocketHandler` 或者说 `defineGraphqlWebSocket` 函数。 以下是一个将HTTP和WebSocket处理器合并在一个应用程序中的例子.

```js
import { createApp } from 'h3'
import { ApolloServer } from '@apollo/server'
import { startServerAndCreateH3Handler } from '@as-integrations/h3'
import { defineGraphqlWebSocketHandler } from '@as-integrations/h3/websocket'
import { makeExecutableSchema } from '@graphql-tools/schema'

// Define your schema and resolvers
const typeDefs = `...`
const resolvers = {
  /*...*/
}
const schema = makeExecutableSchema({ typeDefs, resolvers })

const apollo = new ApolloServer({ schema })

export const app = createApp()
app.use(
  '/api',
  startServerAndCreateH3Handler(apollo, {
    websocket: defineGraphqlWebSocketHandler({ schema }),
  }),
)
```

然后可以使用阿波罗沙盒或其他支持此选项的客户端连接到 WebSocket 端点 `graphql-ws` 协议.

见 [WebSocket 示例](https://npmcn.com/package/%40as-integrations/h3/examples/websocket.ts) 一个完整的例子.

## 发展

-   克隆此寄存器
-   启用 [核心软件包](https://github.com/nodejs/corepack) 使用 `corepack enable` (使用时 `npm i -g corepack` 用于节点.js < 16.10.
-   使用 `pnpm install`。 。 。 .
-   使用 `pnpm test` 和一体化测试 `pnpm test:integration`。 。 。 .

## 许可证

由 所制

原载 [MIT 许可证](https://npmcn.com/package/%40as-integrations/h3/LICENSE)。 。 。 .

## 常见问题

### @as-integrations/h3 是什么？

用于 h3 或 Nuxt 的阿波罗服务器集成

### 如何安装 @as-integrations/h3？

在项目目录运行 npm install @as-integrations/h3，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@apollo/server（^4\.1\.1 \|\| ^5\.0\.0）、crossws（^0\.3\.0）、graphql（^16\.0\.0）、h3（^1\.11\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @as-integrations/h3 支持 TypeScript 吗？

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

### @as-integrations/h3 支持 ESM 还是 CommonJS？

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

### @as-integrations/h3 需要什么版本的 Node\.js？

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

### @as-integrations/h3 可以商用吗？

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

### @as-integrations/h3 的下载量有多少？

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

### @as-integrations/h3 还在维护吗？

最新版本 2\.0\.3 发布于 2026/02/04（9 个月前）；至今共发布 12 个版本，其中过去 12 个月发布了 2 个；首次发布于 2022/10/16。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

