apollo-federation

作者: apollographql

Apollo Federation 可將多個 GraphQL API(子圖)組合成統一的超級圖表。

npx skills add https://github.com/apollographql/skills --skill apollo-federation

Apollo Federation Schema Authoring

Apollo Federation enables composing multiple GraphQL APIs (subgraphs) into a unified supergraph.

Federation 2 Schema Setup

Every Federation 2 subgraph must opt-in via @link:

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.12",
        import: ["@key", "@shareable", "@external", "@requires", "@provides"])

Import only the directives your subgraph uses. The version shown (v2.12) is illustrative — check the Federation changelog for currently supported versions before copying it verbatim.

A subgraph's @link version is a floor, not the composition version — see Federation versions for the full explanation.

Core Directives Quick Reference

DirectivePurposeExample
@keyDefine entity with unique keytype Product @key(fields: "id")
@shareableAllow multiple subgraphs to resolve fieldtype Position @shareable { x: Int! }
@externalReference field from another subgraphweight: Int @external
@requiresComputed field depending on external fieldsshippingCost: Int @requires(fields: "weight")
@providesConditionally resolve external field@provides(fields: "name")
@overrideMigrate field to this subgraph@override(from: "Products")
@inaccessibleHide from API schemainternalId: ID! @inaccessible
@interfaceObjectAdd fields to entity interfacetype Media @interfaceObject

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

Key Patterns

Entity Definition

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

Entity Contributions Across Subgraphs

# Products subgraph
type Product @key(fields: "id") {
  id: ID!
  name: String!
  price: Int
}

# Reviews subgraph
type Product @key(fields: "id") {
  id: ID!
  reviews: [Review!]!
  averageRating: Float
}

Computed Fields with @requires

type Product @key(fields: "id") {
  id: ID!
  size: Int @external
  weight: Int @external
  shippingEstimate: String @requires(fields: "size weight")
}

Value Types with @shareable

type Money @shareable {
  amount: Int!
  currency: String!
}

Entity Stub (Reference Without Contributing)

type Product @key(fields: "id", resolvable: false) {
  id: ID!
}

Ground Rules

  • ALWAYS use Federation 2.x syntax with @link directive
  • ALWAYS import only the directives your subgraph uses
  • NEVER use @shareable without ensuring all subgraphs return identical values for that field
  • PREFER @key with single ID field for simple entity identification
  • USE rover supergraph compose to validate composition locally
  • USE rover subgraph check to validate against production supergraph

來自 apollographql 的更多技能

apollo-ios
apollographql
Apollo iOS 是一個專為 Apple 平台設計的強型別 GraphQL 客戶端。它能從你的 GraphQL 操作與 schema 生成 Swift 型別,並提供 async/await 客戶端、正規化快取(記憶體或 SQLite 支援)、可插拔的攔截器式 HTTP 傳輸(處理查詢、變更與多部分訂閱),以及可選的 WebSocket 傳輸(graphql-transport-ws),可承載任何操作類型。
apollo-router
apollographql
Apollo Router 是一款以 Rust 編寫的高效能圖形路由器,用於執行 Apollo Federation 2 超級圖。它位於子圖前端,負責查詢規劃、執行與回應組合。
apollo-router-plugin-creator
apollographql
為 Apollo Router 建立原生 Rust 外掛程式。
apollo-server
apollographql
使用 Apollo Server 5.x 跨框架建置 GraphQL 伺服器的完整指南。涵蓋綱要定義、解析器、上下文設定及錯誤處理,支援 TypeScript。支援獨立模式進行原型開發,並可整合 Express、Fastify、Koa 及無伺服器環境。包含解析器模式、認證/授權、外掛、用於防止 N+1 問題的 DataLoader,以及效能最佳化技術。提供資料來源、錯誤處理的參考文件。
graphql-operations
apollographql
撰寫高效、型別安全的 GraphQL 操作並使用片段進行組織的最佳實踐指南。涵蓋查詢、變更、訂閱及片段,包含命名慣例、變數語法與指令用法。強調核心原則:僅請求所需欄位、為所有操作命名、使用變數而非硬編碼值,以及加入 id 欄位以利快取。建議將片段與元件共置,並使用 @include / @skip 指令進行條件式欄位...
graphql-schema
apollographql
業界最佳實踐指南,用於設計直觀、高效且易於維護的 GraphQL 結構。涵蓋核心設計原則,包括以客戶端為中心的類型組織、明確的可空性模式以及向後相容的演進策略。提供關於類型、命名慣例、基於游標的分頁、錯誤建模和安全考量的參考文件。包含介面、聯合、輸入類型、變更和 ID 策略的實用模式,並附有程式碼範例。
rover
apollographql
We need to translate the given English text into Traditional Chinese. The text describes the Apollo Rover CLI tool. We must preserve the name "rover" and any technical terms like GraphQL, federation, supergraph, GraphOS, CI/CD, JSON, etc. Also preserve URLs if any (none here). Do not add any extra commentary or labels. Just translate the text inside <text> tags. The text: "Apollo Rover CLI for managing GraphQL schemas, federation, and local supergraph development. Publish, fetch, and validate subgraph schemas; compose federated supergraphs locally or via GraphOS Includes schema checking (pre-deploy validation), linting, and introspection from running servers rover dev command starts a local Router with automatic schema composition for development workflows Supports CI/CD patterns with check-before-publish validation and JSON output for scripting Requires..." Wait, the source text ends with "Requires" but it's cut off? Actually the user provided: "Apollo Rover CLI for managing GraphQL schemas, federation, and local supergraph development. Publish,
rust-best-practices
apollographql
基於 Apollo GraphQL 最佳實踐手冊的慣用 Rust 編碼標準。涵蓋九個核心領域:編碼風格與慣用語法、Clippy 靜態檢查、效能最佳化、錯誤處理、測試模式、泛型與分派、型態狀態模式、文件撰寫以及指標安全性。強調借用優於複製