apollo-connectors

作者: apollographql

使用@source和@connect指令將REST API整合至GraphQL超級圖譜。提供結構化五步驟流程:研究API結構、使用指令實作綱要、透過rover supergraph compose驗證、執行連接器、測試覆蓋範圍。支援請求配置,包含標頭、請求主體、N+1模式批次處理,以及透過$env注入環境變數。處理回應映射,包含欄位選取、別名、巢狀資料子選取及實體...

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

Apollo Connectors Schema Assistant

MCP Tools

If GraphOS MCP Tools are available, use them:

  • connectors-spec: Fetch the complete Connectors specification before starting any connector work
  • apollo_docs_search: Search for relevant documentation
  • apollo_docs_read: Read specific documentation pages by slug

Documentation paths by topic:

  • Requests: /graphos/connectors/requests/url, /headers, /body, /batching
  • Responses: /graphos/connectors/responses/fields, /error-handling
  • Mapping: /graphos/connectors/mapping, /arrays, /enums, /literals
  • Entities: /graphos/connectors/entities, /patterns

Process

Follow this 5-step process. DO NOT skip any steps.

Step 1: Research

  • Understand the API being called and the structure of responses
  • Ask the user for example API responses if not provided
  • Fetch relevant documentation from MCP tools or reference files
  • DO NOT write any code until research is complete

Step 2: Implement

  • Create the schema using the template below
  • Follow the grammar, methods, and variables in the reference files
  • Ask clarifying questions if unsure about requirements

Step 3: Validate (Compose)

  • Run rover supergraph compose --config ./supergraph.yaml
  • Fix any composition errors before proceeding

Step 4: Execute

  • Run rover connector run --schema <file> -c "<Type.field>" -v "{}"
  • Verify the connector executes correctly

Step 5: Test

  • Create or update test files under /tests/
  • Run rover connector test
  • Ensure full test coverage for each connector

Schema Template

# Note to AI Friends: This is an Apollo Connectors schema. You should use your connectors-spec MCP tool if you have it available before editing this file.

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.15")
  @link(url: "https://specs.apollo.dev/connect/v0.3", import: ["@source", "@connect"])

@source(name: "api_name", http: { baseURL: "https://api.example.com" })

type Query {
  example(id: ID!): Example
    @connect(
      source: "api_name"
      http: { GET: "/example/{$args.id}" }
      selection: """
      id
      name
      """
    )
}

type Example {
  id: ID!
  name: String
}

Version requirements

Use the latest Long-Term Support (LTS) versions of Apollo Federation and Apollo Connectors. Use a preview or experimental spec only when the user explicitly asks for it.

Confirm the current LTS before writing @link URLs or federation_version. Prefer the GraphOS docs tools when they are available:

  • Federation: read /graphos/schema-design/federated-schemas/reference/versions and use the highest version marked LTS. That version is the schema @link (https://specs.apollo.dev/federation/vX.Y). Set federation_version in supergraph.yaml to the latest patch of that same LTS line.
  • Connectors: read /graphos/connectors/getting-started/version-requirements and /graphos/connectors/reference/changelog. Use the latest Connectors spec that is generally available (not marked experimental) and compatible with that Federation LTS (https://specs.apollo.dev/connect/vX.Y).

The template above uses the current LTS: Federation v2.15 and Connectors v0.3. If the docs list a newer LTS, use the docs.

Reference Files

Before implementing connectors, read the relevant reference files:

Key Rules

Selection Mapping

  • Prefer sub-selections over ->map for cleaner mappings
  • Do NOT use $ when selecting fields directly from root
  • Field aliasing: newName: originalField (only when renaming)
  • Sub-selection: fieldName { ... } (to map nested content)
# DO - Direct sub-selection for arrays
$.results {
  firstName: name.first
  lastName: name.last
}

# DO NOT - Unnecessary root $
$ {
  id
  name
}

# DO - Direct field selection
id
name

Entities

  • Add @connect on a type to make it an entity (no @key needed)
  • Create entity stubs in parent selections: user: { id: userId }
  • When you see an ID field (e.g., productId), create an entity relationship
  • Each entity should have ONE authoritative subgraph with @connect

Literal Values

Use $() wrapper for literal values in mappings:

$(1)              # number
$(true)           # boolean
$("hello")        # string
$({"a": "b"})     # object

# In body
body: "$({ a: $args.a })"  # CORRECT
body: "{ a: $args.a }"     # WRONG - will not compose

Headers

http: {
  GET: "/api"
  headers: [
    { name: "Authorization", value: "Bearer {$env.API_KEY}" },
    { name: "X-Forwarded", from: "x-client" }
  ]
}

Batching

Convert N+1 patterns using $batch:

type Product @connect(
  source: "api"
  http: {
    POST: "/batch"
    body: "ids: $batch.id"
  }
  selection: "id name"
) {
  id: ID!
  name: String
}

Ground Rules

  • NEVER make up syntax or directive values not in this specification
  • NEVER use --elv2-license accept (for humans only)
  • ALWAYS ask for example API responses before writing code
  • ALWAYS validate with rover supergraph compose after changes
  • ALWAYS create entity relationships when you see ID fields
  • Prefer $env over $config for environment variables
  • Use rover dev for running Apollo Router locally

來自 apollographql 的更多技能

apollo-federation
apollographql
Apollo Federation 可將多個 GraphQL API(子圖)組合成統一的超級圖表。
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,