apollo-federation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseApollo Federation Schema Authoring
Apollo Federation Schema编写指南
Apollo Federation enables composing multiple GraphQL APIs (subgraphs) into a unified supergraph.
Apollo Federation支持将多个GraphQL API(子图)组合成一个统一的超图。
Federation 2 Schema Setup
Federation 2 Schema设置
Every Federation 2 subgraph must opt-in via :
@linkgraphql
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.12",
import: ["@key", "@shareable", "@external", "@requires", "@provides"])Import only the directives your subgraph uses.
每个Federation 2子图必须通过声明启用:
@linkgraphql
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.12",
import: ["@key", "@shareable", "@external", "@requires", "@provides"])仅导入你的子图需要使用的指令。
Core Directives Quick Reference
核心指令速查
| Directive | Purpose | Example |
|---|---|---|
| Define entity with unique key | |
| Allow multiple subgraphs to resolve field | |
| Reference field from another subgraph | |
| Computed field depending on external fields | |
| Conditionally resolve external field | |
| Migrate field to this subgraph | |
| Hide from API schema | |
| Add fields to entity interface | |
| 指令 | 用途 | 示例 |
|---|---|---|
| 定义带有唯一键的实体 | |
| 允许多个子图解析该字段 | |
| 引用来自另一个子图的字段 | |
| 依赖外部字段的计算字段 | |
| 有条件地解析外部字段 | |
| 将字段迁移至本子图 | |
| 在API Schema中隐藏该字段 | |
| 为实体接口添加字段 | |
Reference Files
参考文档
Detailed documentation for specific topics:
- Directives - All federation directives with syntax, examples, and rules
- Schema Patterns - Multi-subgraph patterns and recipes
- Composition - Composition rules, error codes, and debugging
特定主题的详细文档:
- 指令 - 所有联邦指令的语法、示例和规则
- Schema模式 - 多子图模式和实践方案
- 组合 - 组合规则、错误代码和调试方法
Key Patterns
核心模式
Entity Definition
实体定义
graphql
type Product @key(fields: "id") {
id: ID!
name: String!
price: Int
}graphql
type Product @key(fields: "id") {
id: ID!
name: String!
price: Int
}Entity Contributions Across Subgraphs
跨子图的实体贡献
graphql
undefinedgraphql
undefinedProducts subgraph
Products subgraph
type Product @key(fields: "id") {
id: ID!
name: String!
price: Int
}
type Product @key(fields: "id") {
id: ID!
name: String!
price: Int
}
Reviews subgraph
Reviews subgraph
type Product @key(fields: "id") {
id: ID!
reviews: [Review!]!
averageRating: Float
}
undefinedtype Product @key(fields: "id") {
id: ID!
reviews: [Review!]!
averageRating: Float
}
undefinedComputed Fields with @requires
使用@requires的计算字段
graphql
type Product @key(fields: "id") {
id: ID!
size: Int @external
weight: Int @external
shippingEstimate: String @requires(fields: "size weight")
}graphql
type Product @key(fields: "id") {
id: ID!
size: Int @external
weight: Int @external
shippingEstimate: String @requires(fields: "size weight")
}Value Types with @shareable
使用@shareable的值类型
graphql
type Money @shareable {
amount: Int!
currency: String!
}graphql
type Money @shareable {
amount: Int!
currency: String!
}Entity Stub (Reference Without Contributing)
实体存根(仅引用不贡献字段)
graphql
type Product @key(fields: "id", resolvable: false) {
id: ID!
}graphql
type Product @key(fields: "id", resolvable: false) {
id: ID!
}Ground Rules
基本原则
- ALWAYS use Federation 2.x syntax with directive
@link - ALWAYS import only the directives your subgraph uses
- NEVER use without ensuring all subgraphs return identical values for that field
@shareable - PREFER with single ID field for simple entity identification
@key - USE to validate composition locally
rover supergraph compose - USE to validate against production supergraph
rover subgraph check
- 始终使用带有指令的Federation 2.x语法
@link - 始终仅导入你的子图需要使用的指令
- 切勿在未确保所有子图对该字段返回相同值的情况下使用
@shareable - 优先使用单个ID字段的进行简单实体标识
@key - 使用在本地验证组合结果
rover supergraph compose - 使用针对生产环境超图进行验证
rover subgraph check