# @apollo/graphql-testing-library 中文文档

规范地址：https://npmcn.com/package/%40apollo/graphql-testing-library/

版本：0\.3\.0
npm 元数据更新时间：2026-01-22T17:30:01.993Z
中文译文更新时间：2026-11-06T11:00:30.007Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：86.3 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：10 个
- peer 依赖：msw ^2\.0\.0，graphql ^15\.0\.0 \|\| ^16\.0\.0
- 首次发布：2024-06-14T18:08:36.133Z
- 最新版本发布：2024-08-29T19:03:57.775Z
- 近 30 天下载量：205948（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：11（近 12 个月 0 个）

## 中文简介

[![Apollo GraphQL Logo](https://raw.githubusercontent.com/apollographql/apollo-client-devtools/main/assets/apollo-wordmark.svg)](https://www.apollographql.com/)

# 图QL 测试i

## 安装

```sh
npm install @apollo/graphql-testing-library
```

也可以使用：`pnpm add @apollo/graphql-testing-library`、`yarn add @apollo/graphql-testing-library`、`bun add @apollo/graphql-testing-library`

## README

[![Apollo GraphQL Logo](https://raw.githubusercontent.com/apollographql/apollo-client-devtools/main/assets/apollo-wordmark.svg)](https://www.apollographql.com/)

# 图QL 测试库

测试鼓励使用 GraphQL 构建的 TypeScript 应用程序的良好做法的公用设备.

[![npm version](https://badge.fury.io/js/%40apollo%2Fgraphql-testing-library.svg)](https://badge.fury.io/js/%40apollo%2Fgraphql-testing-library) ![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/alessbell/3fd56e82b55e134ee9cf57f28b0b3d49/raw/jest-coverage-comment__main.json) ![workflow](https://github.com/apollographql/graphql-testing-library/actions/workflows/test.yml/badge.svg) [![Join our Discord server](https://img.shields.io/discord/1022972389463687228.svg?color=7389D8&labelColor=6A7EC2&logo=discord&logoColor=ffffff&style=flat-square)](https://discord.gg/graphos)

* * *

**图QL 测试库** 提供便于生成的公用事业 [模拟服务工人](https://mswjs.io/) 任意 GraphQL API 的处理器.

MSW就是那个 [测试图书馆推荐](https://testing-library.com/docs/react-testing-library/example-intro/#full-example) 如何在测试中公开模拟API的通信 而不刺耳 `window.fetch`。 。 。 .

此库目前支持递增的传送功能 `@defer` 财务报告和审定财务报表 `@stream` 以及WebSockets等其他运输工具, [目前为MSW的β](https://github.com/mswjs/msw/discussions/2010)。 。 。 .

> 这个项目不属于 ["试文库"](https://github.com/testing-library) 激励它的生态系统。 我们只是粉丝:)

## 安装

这个图书馆 `peerDependencies` 清单 `msw` 时间 `^2.0.0` 财务报告和审定财务报表 `graphql` 时间 `^15.0.0 || ^16.0.0`。使用您首选的软件包管理器与此库一起安装 :

```
npm install --save-dev @apollo/graphql-testing-library msw graphql
pnpm add --save-dev @apollo/graphql-testing-library msw graphql
yarn add --dev @apollo/graphql-testing-library msw graphql
bun add --dev @apollo/graphql-testing-library msw graphql
```

## 使用量

### `createHandler`

```typescript
import { createHandler } from "@apollo/graphql-testing-library";

// We suggest using @graphql-tools/mock and @graphql-tools/schema
// to create a schema with mock resolvers.
// See https://the-guild.dev/graphql/tools/docs/mocking for more info.
import { addMocksToSchema } from "@graphql-tools/mock";
import { makeExecutableSchema } from "@graphql-tools/schema";
import typeDefs from "./schema.graphql";

// Create an executable schema
const schema = makeExecutableSchema({ typeDefs });

// Add mock resolvers
const schemaWithMocks = addMocksToSchema({
  schema,
  resolvers: {
    Query: {
      products: () =>
        Array.from({ length: 5 }, (_element, id) => ({
          id: `product-${id}`,
        })),
    },
  },
});

// `createHandler` returns an object with a `handler` and `replaceSchema`
// function: `handler` is a MSW handler that will intercept all GraphQL
// operations, and `replaceSchema` allows you to replace the mock schema
// the `handler` use to resolve requests against.
const { handler, replaceSchema } = createHandler(schemaWithMocks, {
  // It accepts a config object as the second argument where you can specify a
  // delay duration, which uses MSW's delay API:
  // https://mswjs.io/docs/api/delay
  // Default: "real" (100-400ms in browsers, 20ms in Node-like processes)
  delay: number | "infinite" | "real",
});
```

## 常见问题

### @apollo/graphql-testing-library 是什么？

图QL 测试i

### 如何安装 @apollo/graphql-testing-library？

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

### @apollo/graphql-testing-library 支持 TypeScript 吗？

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

### @apollo/graphql-testing-library 支持 ESM 还是 CommonJS？

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

### @apollo/graphql-testing-library 可以商用吗？

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

### @apollo/graphql-testing-library 的下载量有多少？

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

### @apollo/graphql-testing-library 还在维护吗？

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

