# @apollo/subgraph 中文文档

规范地址：https://npmcn.com/package/%40apollo/subgraph/

版本：2\.15\.1
npm 元数据更新时间：2026-09-14T19:29:16.211Z
中文译文更新时间：2026-11-06T10:55:42.326Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：97 kB，39 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=22\.0\.0
- 运行依赖：2 个
- peer 依赖：graphql ^16\.11\.0
- 首次发布：2021-10-07T21:45:13.381Z
- 最新版本发布：2026-09-14T19:29:15.856Z
- 近 30 天下载量：4230668（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：193（近 12 个月 30 个）

## 中文简介

阿波罗联邦以图克勒-js为基础的子图公用事业

## 安装

```sh
npm install @apollo/subgraph
```

也可以使用：`pnpm add @apollo/subgraph`、`yarn add @apollo/subgraph`、`bun add @apollo/subgraph`

## README

[![Continuous Integration](https://github.com/apollographql/subgraph-js/workflows/Continuous%20Integration/badge.svg)](https://github.com/apollographql/subgraph-js/actions?query=workflow%3A%22Continuous+Integration%22) [![MIT License](https://img.shields.io/github/license/apollographql/subgraph-js.svg)](https://npmcn.com/package/%40apollo/subgraph/LICENSE) [![NPM](https://img.shields.io/npm/v/%40apollo%2Fsubgraph)](https://www.npmjs.com/package/@apollo/subgraph) [![Join the community forum](https://img.shields.io/badge/join%20the%20community-forum-blueviolet)](https://community.apollographql.com/)

# @ apollo/ 子图

阿波罗联邦 `graphql-js` 生态系统.

`@apollo/subgraph` 建在顶部 `graphql-js` 并提供了转换逻辑 使您的 GraphQL 计程器兼容 . `buildSubgraphSchema` 添加常见 联合会类型定义(如:. `Any` 斯卡尔, `_Entity` 工会、联邦指令、, ,并允许您简单指定您的联邦实体解析器.

## 安装

要求节点.js 22或后.

```sh
npm install @apollo/subgraph graphql
```

## 使用量

```ts
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { buildSubgraphSchema } from '@apollo/subgraph';
import gql from 'graphql-tag';

const typeDefs = gql`
  extend schema
    @link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key"])

  type Query {
    product(id: ID!): Product
  }

  type Product @key(fields: "id") {
    id: ID!
    name: String
  }
`;

const resolvers = {
  Query: {
    product: (_source, { id }) => products.find((product) => product.id === id),
  },
  Product: {
    // Called for each entity representation the router sends to `_entities`.
    __resolveReference: (reference) =>
      products.find((product) => product.id === reference.id),
  },
};

const server = new ApolloServer({
  schema: buildSubgraphSchema([{ typeDefs, resolvers }]),
});

await startStandaloneServer(server, { listen: { port: 4001 } });
```

### API 密码

-   **`buildSubgraphSchema(modulesOrSDL)`** ——构建可执行子图计划. 接受一个 `DocumentNode`,或一系列文档或 `{ typeDefs, resolvers }` 模块.
-   **`printSubgraphSchema(schema)`** 印出完整的子图计划, 包括联邦指令和类型.
-   **`addResolversToSchema(schema, resolvers)`** 将解析器地图附在 现有计划、理解 `__resolveReference` 和平常一样 `__resolveType` 编号 : `__isTypeOf`。 。 。 .
-   **`entitiesResolver({ representations, context, info })`** - 那个 `_entities` 解析器,导出给集合自己根字段的库.

实体参考资料通过 `__resolveReference`或来自 解析器映射或从 `extensions.apollo.subgraph.resolveReference` 在类型.

## 发展

```sh
npm install
npm run build
npm test
```

这个 [`compatibility/`](https://npmcn.com/package/%40apollo/subgraph/compatibility) 工作空间运行 [阿波罗联邦子图兼容套件](https://github.com/apollographql/apollo-federation-subgraph-compatibility) 针对这个库,使用道克来作路由器和参考子图. 见其 [读取](https://npmcn.com/package/%40apollo/subgraph/compatibility/README.md)。 。 。 .

```sh
npm run compatibility --workspace @apollo/subgraph-compatibility
```

## 联系人

如果您对库或代码有特定问题,请在 [阿波罗社区论坛](https://community.apollographql.com/)。 。 。 .

## 捐款

见 [捐款额](https://npmcn.com/package/%40apollo/subgraph/CONTRIBUTING.md) 详细情况.

在你建立你的当地分支后,看看我们的公开问题,看看你们可以在哪里作出贡献.

## 警卫

有关如何联系小组处理安全问题的更多信息,见我们 [安全政策](https://github.com/apollographql/federation-jvm/security/policy)。 。 。 .

## 许可证

这个图书馆的许可依据是 [《麻省理工学院许可证》](https://npmcn.com/package/%40apollo/subgraph/LICENSE)。 。 。 .

## 常见问题

### @apollo/subgraph 是什么？

阿波罗联邦以图克勒-js为基础的子图公用事业

### 如何安装 @apollo/subgraph？

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

### @apollo/subgraph 支持 TypeScript 吗？

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

### @apollo/subgraph 支持 ESM 还是 CommonJS？

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

### @apollo/subgraph 需要什么版本的 Node\.js？

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

### @apollo/subgraph 可以商用吗？

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

### @apollo/subgraph 的下载量有多少？

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

### @apollo/subgraph 还在维护吗？

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

