# @as-integrations/hapi 中文文档

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

版本：2\.0\.2
npm 元数据更新时间：2026-05-28T22:47:38.801Z
中文译文更新时间：2026-11-06T21:57:39.369Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：22.6 kB，11 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=16
- 运行依赖：0 个
- peer 依赖：@hapi/hapi ^20\.2\.2 \|\| ^21\.0\.0，@apollo/server ^4\.0\.0 \|\| ^5\.0\.0
- 首次发布：2022-11-10T08:55:53.686Z
- 最新版本发布：2025-09-08T23:09:21.557Z
- 近 30 天下载量：97503（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 0 个）

## 中文简介

Apollo 服务器集成后与 Hapi 一起使用

## 安装

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

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

## README

[![Apollo Server](https://avatars.githubusercontent.com/u/17189275?s=200)](https://www.apollographql.com/) [![Hapi](https://raw.githubusercontent.com/hapijs/assets/master/images/hapi.png)](https://hapi.dev/)

[![NPM version](https://badge.fury.io/js/@as-integrations%2Fhapi.svg)](https://www.npmjs.com/package/@as-integrations/hapi) [![NPM downloads](https://img.shields.io/npm/dm/@as-integrations/hapi.svg?style=flat)](https://www.npmjs.com/package/@as-integrations/hapi)

# Hapi 阿波罗服务器集成

## **导言**

**阿波罗服务器集成,与哈庇一起使用.**

这是一个简单的软件包,可以方便地将自己的Hapi服务器执行与阿波罗服务器实例连接.

## **所需资源**

-   **[节点.js v16](https://nodejs.org/)** 或稍后时间
-   **[Hapi v20.x (英语)](https://www.hapi.dev/)** 或稍后时间
-   **[图QL.js v16](https://graphql.org/graphql-js/)** 或稍后时间
-   **[阿波罗服务器 v4 QQ v5](https://www.apollographql.com/docs/apollo-server/)** 或稍后时间

注: 阿波罗服务器 v5 需要节点JS v20.x或更高

## **安装**

```bash
npm install @as-integrations/hapi @apollo/server graphql @hapi/hapi
```

## **使用量**

设置 [哈皮,哈皮,哈比,哈比,哈比,哈比,哈比,哈比,哈比,哈比](https://www.hapi.dev/) 打开 [阿波罗服务器](https://www.apollographql.com/docs/apollo-server/) 就像你通常会,然后通过使用 `hapiApollo` 插件 :

```typescript
import { Server } from '@hapi/hapi';
import { ApolloServer, BaseContext } from "@apollo/server";
import hapiApollo from "@as-integrations/hapi";
// ...

// create the apollo server instance and start it
const apolloServer = new ApolloServer<BaseContext>({
  typeDefs,
  resolvers,
});

await apolloServer.start();

// create the hapi server
const hapi = new Server({
  host: 'localhost',
  port: 5000
});

// ...

// register the plugin with the hapi server and provide the apollo instance in plugin config
await hapi.register({
  plugin: hapiApollo,
  options: {
    apolloServer,
    path: '/graphql'
  }
});

await hapi.start();
```

说明: **你必须打电话等待 `apollo.start()` 在使用整合之前.**

## **ES5 使用量**

如果您正在使用ES5或者正在使用要求()来使用此模块,您可能需要像这样引用默认导出 :

```
const hapiApollo = require("@as-integrations/hapi").default;
```

## **A. 背景情况**

阿波罗服务器 v4 已将上下文设置移出 `ApolloServer` 建筑师.

定义你自己的上下文功能并将其传递给 `context` 选项.

例如:

```typescript
await hapi.register({
  plugin: hapiPlugin,
  options: {
    context: async ({ request }) => ({
      token: request.headers.token
    }),
    apolloServer
  }
});
```

## **路由配置**

此模块创建的 Hapi 路由的其他配置可以通过 获取Route(获得/选项路由)或后Route(后路由). 两种支持选项、规则和虚拟 配置 .

例如,要在邮递路线上添加最大上传大小:

```typescript
await hapi.register({
  plugin: hapiPlugin,
  options: {
    context: async ({ request }) => ({
      token: request.headers.token
    }),
    apolloServer,
    postRoute: {
      options: {
        payload: {
          maxBytes: 1024
        }
      }
    }
  }
});
```

## **贡献者**

-   戴维·卡斯特罗(签名)[长相](https://github.com/arimus)(中文(简体) )
-   特雷弗·舍尔(英语:Trevor Scheer (film))[偷车手](https://github.com/trevor-scheer)(中文(简体) )
-   吉克办公室[吉克办公室](https://github.com/geekoffice)(中文(简体) )

## 常见问题

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

Apollo 服务器集成后与 Hapi 一起使用

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

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

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

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

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

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

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

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

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

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

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

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

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

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

