graphql-schema

作者: apollographql

業界最佳實踐指南,用於設計直觀、高效且易於維護的 GraphQL 結構。涵蓋核心設計原則,包括以客戶端為中心的類型組織、明確的可空性模式以及向後相容的演進策略。提供關於類型、命名慣例、基於游標的分頁、錯誤建模和安全考量的參考文件。包含介面、聯合、輸入類型、變更和 ID 策略的實用模式,並附有程式碼範例。

npx skills add https://github.com/apollographql/skills --skill graphql-schema

GraphQL Schema Design Guide

This guide covers best practices for designing GraphQL schemas that are intuitive, performant, and maintainable. Schema design is primarily a server-side concern that directly impacts API usability.

Schema Design Principles

1. Design for Client Needs

  • Think about what queries clients will write
  • Organize types around use cases, not database tables
  • Expose capabilities, not implementation details

2. Be Explicit

  • Use clear, descriptive names
  • Make nullability intentional
  • Document with descriptions

3. Design for Evolution

  • Plan for backwards compatibility
  • Use deprecation before removal
  • Avoid breaking changes

Quick Reference

Type Definition Syntax

"""
A user in the system.
"""
type User {
  id: ID!
  email: String!
  name: String
  posts(first: Int = 10, after: String): PostConnection!
  createdAt: DateTime!
}

Nullability Rules

PatternMeaning
StringNullable - may be null
String!Non-null - always has value
[String]Nullable list, nullable items
[String!]Nullable list, non-null items
[String]!Non-null list, nullable items
[String!]!Non-null list, non-null items

Best Practice: Use [Type!]! for lists - empty list over null, no null items.

Input vs Output Types

# Output type - what clients receive
type User {
  id: ID!
  email: String!
  createdAt: DateTime!
}

# Input type - what clients send
input CreateUserInput {
  email: String!
  name: String
}

# Mutation using input type
type Mutation {
  createUser(input: CreateUserInput!): User!
}

Interface Pattern

interface Node {
  id: ID!
}

type User implements Node {
  id: ID!
  email: String!
}

type Post implements Node {
  id: ID!
  title: String!
}

Union Pattern

union SearchResult = User | Post | Comment

type Query {
  search(query: String!): [SearchResult!]!
}

Reference Files

Detailed documentation for specific topics:

  • Types - Type design patterns, interfaces, unions, and custom scalars
  • Naming - Naming conventions for types, fields, and arguments
  • Pagination - Connection pattern and cursor-based pagination
  • Errors - Error modeling and result types
  • Security - Security best practices for schema design

Key Rules

Type Design

  • Define types based on domain concepts, not data storage
  • Use interfaces for shared fields across types
  • Use unions for mutually exclusive types
  • Keep types focused (single responsibility)
  • Avoid deep nesting - flatten when possible

Field Design

  • Fields should be named from client's perspective
  • Return the most specific type possible
  • Make expensive fields explicit (consider arguments)
  • Use arguments for filtering, sorting, pagination

Mutation Design

  • Use single input argument pattern: mutation(input: InputType!)
  • Return affected objects in mutation responses
  • Model mutations around business operations, not CRUD
  • Consider returning a union of success/error types

ID Strategy

  • Use globally unique IDs when possible
  • Implement Node interface for refetchability
  • Base64-encode compound IDs if needed

Ground Rules

  • ALWAYS add descriptions to types and fields
  • ALWAYS use non-null (!) for fields that cannot be null
  • ALWAYS use [Type!]! pattern for lists
  • NEVER expose database internals in schema
  • NEVER break backwards compatibility without deprecation
  • PREFER dedicated input types over many arguments
  • PREFER enums over arbitrary strings for fixed values
  • USE ID type for identifiers, not String or Int
  • USE custom scalars for domain-specific values (DateTime, Email, URL)

來自 apollographql 的更多技能

apollo-client
apollographql
Apollo Client 是一個全面的 JavaScript 狀態管理庫,讓您能透過 GraphQL 管理本地與遠端資料。4.x 版本帶來了改進的快取機制、更佳的 TypeScript 支援,以及 React 19 相容性。
official
apollo-client
apollographql
全面指南,涵蓋使用 Apollo Client 4.x 建構 React 應用程式,包含查詢、變更、快取及狀態管理。支援多種 React 框架與設定:客戶端應用程式(Vite、CRA)、搭配 React Server Components 的 Next.js App Router、支援串流 SSR 的 React Router 7,以及 TanStack Start。包含用於查詢(useQuery、useLazyQuery)、變更(useMutation)的鉤子,以及基於 Suspense 的模式(useSuspenseQuery、useBackgroundQuery),適用於現代 React 18+ 與 19...
official
apollo-connectors
apollographql
使用@source和@connect指令將REST API整合至GraphQL超級圖譜。提供結構化五步驟流程:研究API結構、使用指令實作綱要、透過rover supergraph compose驗證、執行連接器、測試覆蓋範圍。支援請求配置,包含標頭、請求主體、N+1模式批次處理,以及透過$env注入環境變數。處理回應映射,包含欄位選取、別名、巢狀資料子選取及實體...
official
apollo-federation
apollographql
Apollo Federation 可將多個 GraphQL API(子圖)組合成統一的超級圖表。
official
apollo-ios
apollographql
Apollo iOS 是一個專為 Apple 平台設計的強型別 GraphQL 客戶端。它能從你的 GraphQL 操作與 schema 生成 Swift 型別,並提供 async/await 客戶端、正規化快取(記憶體或 SQLite 支援)、可插拔的攔截器式 HTTP 傳輸(處理查詢、變更與多部分訂閱),以及可選的 WebSocket 傳輸(graphql-transport-ws),可承載任何操作類型。
official
apollo-kotlin
apollographql
Apollo Kotlin 是一個強型別的 GraphQL 客戶端,能從你的 GraphQL 操作和綱要中生成 Kotlin 模型,可用於 Android、JVM 和 Kotlin Multiplatform 專案。
official
apollo-mcp-server
apollographql
透過模型上下文協定將AI代理連接至GraphQL API,內建內省與操作工具。將GraphQL操作暴露為MCP工具;支援三種操作來源:本地檔案、GraphOS Studio集合與持久化查詢清單。提供四種內省工具(introspect、search、validate、execute)用於結構探索與臨時查詢測試;壓縮模式透過簡潔標記減少Token用量。可透過靜態標頭設定認證...
official
apollo-router
apollographql
Apollo Router 是一款以 Rust 編寫的高效能圖形路由器,用於執行 Apollo Federation 2 超級圖。它位於子圖前端,負責查詢規劃、執行與回應組合。
official