@apollo/subgraph

中文文档
2.15.1发布于 2026/09/15MITTypeScriptCommonJS构建来源已验证近 30 天下载 423.1万

Apollo Federation subgraph utilities built on graphql-js

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. API
  4. Development
  5. Contact
  6. Contributing
  7. Security
  8. License

Continuous Integration MIT License NPM Join the community forum

@apollo/subgraph

Apollo Federation subgraph utilities for the graphql-js ecosystem.

@apollo/subgraph is built on top of graphql-js and provides transformation logic to make your GraphQL schemas Federation compatible. buildSubgraphSchema adds common Federation type definition (e.g. Any scalar, _Entity union, Federation directives, etc) and allows you to easily specify your Federated entity resolvers.

Installation

Requires Node.js 22 or later.

npm install @apollo/subgraph graphql

Usage

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) — builds the executable subgraph schema. Accepts a DocumentNode, or an array of documents or { typeDefs, resolvers } modules.
  • printSubgraphSchema(schema) — prints the complete subgraph schema, federation directives and types included.
  • addResolversToSchema(schema, resolvers) — attaches a resolver map to an existing schema, understanding __resolveReference alongside the usual __resolveType / __isTypeOf.
  • entitiesResolver({ representations, context, info }) — the _entities resolver, exported for libraries that assemble their own root fields.

Entity references are resolved through __resolveReference, either from a resolver map or from extensions.apollo.subgraph.resolveReference on the type.

Development

npm install
npm run build
npm test

The compatibility/ workspace runs the Apollo Federation subgraph compatibility suite against this library, using docker compose for the router and reference subgraphs. See its README.

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

Contact

If you have a specific question about the library or code, please start a discussion in the Apollo community forums.

Contributing

See CONTRIBUTING.md for details.

After you have your local branch set up, take a look at our open issues to see where you can contribute.

Security

For more info on how to contact the team for security issues, see our Security Policy.

License

This library is licensed under The MIT License (MIT).

版本与发布记录

@apollo/subgraph 共发布 193 个版本,首次发布于 2021/10/08;过去 12 个月发布了 30 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.15.1latest2026/09/151 个月前
2.15.02026/08/292 个月前
2.15.0-alpha.02026/08/292 个月前
15.0.0-alpha.12026/08/292 个月前
2.14.42026/08/192 个月前
2.14.32026/07/233 个月前

其他发布通道:latest-1 → 0.6.1,testing → 2.7.4-testing.1,connectors → 2.9.0-connectors.9,beta → 2.9.0-beta.0,preview → 2.13.0-preview.2,next → 2.13.3。

关于 @apollo/subgraph 的常见问题

@apollo/subgraph 是什么?

Apollo Federation subgraph utilities built on graphql-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 和提交记录判断。