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

Skills เพิ่มเติมจาก apollographql

apollo-ios
apollographql
Apollo iOS เป็น GraphQL ไคลเอนต์แบบ strongly-typed สำหรับแพลตฟอร์ม Apple โดยสร้างประเภท Swift จากการดำเนินการและสคีมา GraphQL ของคุณ และมาพร้อมกับไคลเอนต์แบบ async/await, แคชแบบ normalized (ในหน่วยความจำหรือ backed โดย SQLite), การขนส่ง HTTP แบบ interceptor-based ที่เสียบได้ซึ่งจัดการ queries, mutations, และ multipart subscriptions, และการขนส่ง WebSocket แบบเลือกได้ (graphql-transport-ws) ที่สามารถรองรับการดำเนินการทุกประเภท
apollo-router
apollographql
Apollo Router เป็นกราฟเราเตอร์ประสิทธิภาพสูงที่เขียนด้วยภาษา Rust สำหรับรันซูเปอร์กราฟของ Apollo Federation 2 โดยจะอยู่ด้านหน้าซับกราฟของคุณและจัดการการวางแผนคิวรี การดำเนินการ และการประกอบคำตอบ
apollo-router-plugin-creator
apollographql
สร้างปลั๊กอิน Rust ดั้งเดิมสำหรับ Apollo Router
apollo-server
apollographql
คู่มือฉบับสมบูรณ์สำหรับการสร้างเซิร์ฟเวอร์ GraphQL ด้วย Apollo Server 5.x ในทุกเฟรมเวิร์ก ครอบคลุมการกำหนดสคีมา ตัวแก้ไข การตั้งค่าคอนเทกซ์ และการจัดการข้อผิดพลาดพร้อมรองรับ TypeScript รองรับโหมดสแตนด์อโลนสำหรับการสร้างต้นแบบ และการผสานรวมกับ Express, Fastify, Koa และสภาพแวดล้อมแบบไร้เซิร์ฟเวอร์ รวมถึงรูปแบบตัวแก้ไข การรับรองความถูกต้อง/การอนุญาต ปลั๊กอิน DataLoader สำหรับป้องกันปัญหา N+1 และเทคนิคการปรับปรุงประสิทธิภาพ ให้เอกสารอ้างอิงสำหรับแหล่งข้อมูล ข้อผิดพลาด...
graphql-operations
apollographql
คู่มือแนวทางปฏิบัติที่ดีที่สุดสำหรับการเขียน GraphQL operations ที่มีประสิทธิภาพและปลอดภัยต่อชนิดข้อมูล พร้อมการจัดระเบียบด้วย fragments ครอบคลุม queries, mutations, subscriptions และ fragments พร้อมหลักการตั้งชื่อ ไวยากรณ์ตัวแปร และการใช้ directives เน้นหลักการสำคัญ: ขอเฉพาะฟิลด์ที่จำเป็น ตั้งชื่อ operations ทั้งหมด ใช้ตัวแปรแทนค่าคงที่ และรวมฟิลด์ id เพื่อให้แคชได้ แนะนำให้วาง fragments ไว้ร่วมกับ components และใช้ directives @include / @skip สำหรับฟิลด์แบบมีเงื่อนไข...
graphql-schema
apollographql
คู่มือแนวปฏิบัติที่ดีที่สุดในอุตสาหกรรมสำหรับการออกแบบ GraphQL schemas ที่ใช้งานง่าย มีประสิทธิภาพสูง และบำรุงรักษาได้ ครอบคลุมหลักการออกแบบหลัก เช่น การจัดระเบียบประเภทที่เน้นผู้ใช้เป็นศูนย์กลาง รูปแบบการกำหนดค่า nullability ที่ชัดเจน และกลยุทธ์การพัฒนาที่เข้ากันได้ย้อนหลัง มีเอกสารอ้างอิงเกี่ยวกับประเภท หลักการตั้งชื่อ การแบ่งหน้าแบบ cursor-based การสร้างแบบจำลองข้อผิดพลาด และข้อควรพิจารณาด้านความปลอดภัย รวมถึงรูปแบบที่ใช้งานได้จริงสำหรับ interfaces, unions, input types, mutations และกลยุทธ์ ID พร้อมตัวอย่างโค้ด...
rover
apollographql
Apollo Rover CLI สำหรับจัดการ GraphQL schemas, federation และการพัฒนา supergraph ในเครื่อง เผยแพร่ ดึงข้อมูล และตรวจสอบความถูกต้องของ subgraph schemas; ประกอบ supergraph แบบ federated ในเครื่องหรือผ่าน GraphOS รวมถึงการตรวจสอบ schema (การตรวจสอบก่อน deploy), การ linting และการ introspection จากเซิร์ฟเวอร์ที่กำลังทำงาน คำสั่ง rover dev เริ่ม Router ในเครื่องพร้อมการประกอบ schema อัตโนมัติสำหรับขั้นตอนการพัฒนา รองรับรูปแบบ CI/CD ด้วยการตรวจสอบก่อนเผยแพร่และเอาต์พุต JSON สำหรับการเขียนสคริปต์ ต้องมี...
rust-best-practices
apollographql
แนวทางการเขียนโค้ด Rust ตามหลัก Idiomatic โดยอ้างอิงจากคู่มือแนวปฏิบัติที่ดีที่สุดของ Apollo GraphQL ครอบคลุมเก้าหลักสำคัญ: รูปแบบและสำนวนการเขียนโค้ด, การใช้ Clippy ตรวจสอบโค้ด, การปรับปรุงประสิทธิภาพ, การจัดการข้อผิดพลาด, รูปแบบการทดสอบ, Generics และ Dispatch, รูปแบบ Type State, การจัดทำเอกสาร, และความปลอดภัยของพอยน์เตอร์ เน้นการใช้ Borrowing แทน Cloning, การจัดการข้อผิดพลาดแบบ Result ร่วมกับ thiserror/anyhow, และการวัดประสิทธิภาพด้วย Release Builds รวมถึงคำแนะนำด่วนเกี่ยวกับรูปแบบ Ownership, การหลีกเลี่ยง Panic,...