@as-integrations/h3

中文文档
2.0.3发布于 2026/02/04MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.2万

An Apollo Server integration for use with h3 or Nuxt

原始使用文档
文档目录6 节
  1. Installation
  2. Usage with Nuxt v3
  3. Usage with h3
  4. Subscriptions with WebSockets
  5. 💻 Development
  6. License

Apollo Server integration for h3 and nuxt

npm version npm downloads Github Actions Codecov

This package allows you to easily integrate Apollo Server with your h3 or Nuxt 3 application.

For defining a GraphQL server in Nuxt 3, you may want to have a look at the GraphQL server toolkit Nuxt module.

Installation

# 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

Usage with Nuxt v3

Create a Server API Route that configures an instance of Apollo Server as described in the documentation and then exports it as the event handler:

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) => {
    /*...*/
  },
})

Usage with h3

Create and configure an instance of Apollo Server as described in the documentation and then register it as a route handler in your h3 application.

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) => {
      /*...*/
    },
  }),
)

Then run your h3 server as usual, e.g. with npx --yes listhen -w --open ./app.ts. Visit http://localhost:3000/api in your browser to access the Apollo Sandbox.

Subscriptions with WebSockets

This package also supports subscriptions over WebSockets. To enable this feature, you need to install the graphql-ws package:

# npm
npm install graphql-ws

# yarn
yarn add graphql-ws

# pnpm
pnpm add graphql-ws

Then you can add a WebSocket handler to your h3 app using the defineGraphqlWebSocketHandler or defineGraphqlWebSocket functions from this package. Here is an example that combines the HTTP and WebSocket handlers in a single app.

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 }),
  }),
)

Then you can connect to the WebSocket endpoint using the Apollo Sandbox or any other client that supports the graphql-ws protocol.

See the WebSocket example for a complete example.

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10).
  • Install dependencies using pnpm install.
  • Run tests using pnpm test and integration tests via pnpm test:integration.

License

Made with 💛

Published under MIT License.

版本与发布记录

@as-integrations/h3 共发布 12 个版本,首次发布于 2022/10/16;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.3latest2026/02/049 个月前
2.0.22025/12/2410 个月前
2.0.02025/03/201.6 年前
1.2.12024/07/102 年前
1.2.02024/07/092 年前
1.1.62023/08/243 年前

关于 @as-integrations/h3 的常见问题

@as-integrations/h3 是什么?

An Apollo Server integration for use with h3 or 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 和提交记录判断。