graphql-schema

โดย apollographql

คู่มือแนวปฏิบัติที่ดีที่สุดในอุตสาหกรรมสำหรับการออกแบบ GraphQL schemas ที่ใช้งานง่าย มีประสิทธิภาพสูง และบำรุงรักษาได้ ครอบคลุมหลักการออกแบบหลัก เช่น การจัดระเบียบประเภทที่เน้นผู้ใช้เป็นศูนย์กลาง รูปแบบการกำหนดค่า nullability ที่ชัดเจน และกลยุทธ์การพัฒนาที่เข้ากันได้ย้อนหลัง มีเอกสารอ้างอิงเกี่ยวกับประเภท หลักการตั้งชื่อ การแบ่งหน้าแบบ cursor-based การสร้างแบบจำลองข้อผิดพลาด และข้อควรพิจารณาด้านความปลอดภัย รวมถึงรูปแบบที่ใช้งานได้จริงสำหรับ interfaces, unions, input types, mutations และกลยุทธ์ 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)

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

apollo-client
apollographql
Apollo Client เป็นไลบรารีจัดการสถานะที่ครอบคลุมสำหรับ JavaScript ที่ช่วยให้คุณจัดการข้อมูลทั้งในเครื่องและระยะไกลด้วย GraphQL เวอร์ชัน 4.x นำเสนอการแคชที่ปรับปรุงดีขึ้น การรองรับ TypeScript ที่ดีขึ้น และความเข้ากันได้กับ React 19
official
apollo-client
apollographql
คู่มือที่ครอบคลุมสำหรับการสร้างแอปพลิเคชัน React ด้วย Apollo Client 4.x ครอบคลุมการสอบถาม การกลายพันธุ์ การแคช และการจัดการสถานะ รองรับเฟรมเวิร์ก React และการตั้งค่าหลายแบบ: แอปฝั่งไคลเอ็นต์ (Vite, CRA), Next.js App Router พร้อม React Server Components, React Router 7 พร้อมสตรีมมิ่ง SSR และ TanStack Start รวมถึง hooks สำหรับการสอบถาม (useQuery, useLazyQuery), การกลายพันธุ์ (useMutation) และรูปแบบที่ใช้ Suspense (useSuspenseQuery, useBackgroundQuery) สำหรับ React 18+ และ 19 สมัยใหม่...
official
apollo-connectors
apollographql
รวม REST API เข้ากับ GraphQL supergraphs โดยใช้ @source และ @connect directives มีกระบวนการ 5 ขั้นตอนที่มีโครงสร้าง: ศึกษาโครงสร้าง API, ใช้ schema กับ directives, ตรวจสอบผ่าน rover supergraph compose, ดำเนินการ connectors, และทดสอบครอบคลุม รองรับการกำหนดค่าคำขอรวมถึง headers, body payloads, การทำ batch สำหรับรูปแบบ N+1, และการแทรกตัวแปรสภาพแวดล้อมผ่าน $env จัดการการแมปการตอบสนองด้วยการเลือกฟิลด์, การตั้งชื่ออื่น, การเลือกย่อยสำหรับข้อมูลที่ซ้อนกัน, และ entity...
official
apollo-federation
apollographql
Apollo Federation ช่วยให้สามารถรวม GraphQL API หลายตัว (ซับกราฟ) เข้าด้วยกันเป็นซูเปอร์กราฟแบบรวมศูนย์
official
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) ที่สามารถรองรับการดำเนินการทุกประเภท
official
apollo-kotlin
apollographql
Apollo Kotlin เป็นไคลเอนต์ GraphQL ที่มีการกำหนดชนิดข้อมูลอย่างเข้มงวด ซึ่งสร้างโมเดล Kotlin จากการดำเนินการและสคีมา GraphQL ของคุณ สามารถใช้ในโปรเจกต์ Android, JVM และ Kotlin Multiplatform ได้
official
apollo-mcp-server
apollographql
เชื่อมต่อ AI agents กับ GraphQL APIs ผ่าน Model Context Protocol พร้อมเครื่องมือ introspection และ operation ในตัว เปิดเผย GraphQL operations เป็น MCP tools; รองรับแหล่ง operation สามประเภท: ไฟล์ในเครื่อง, คอลเลกชัน GraphOS Studio, และ persisted query manifests มีเครื่องมือ introspection สี่อย่าง (introspect, search, validate, execute) สำหรับการสำรวจ schema และทดสอบ query แบบ ad-hoc; โหมด minification ลดการใช้ token ด้วยสัญกรณ์แบบกะทัดรัด กำหนดค่าการรับรองความถูกต้องผ่าน static headers,...
official
apollo-router
apollographql
Apollo Router เป็นกราฟเราเตอร์ประสิทธิภาพสูงที่เขียนด้วยภาษา Rust สำหรับรันซูเปอร์กราฟของ Apollo Federation 2 โดยจะอยู่ด้านหน้าซับกราฟของคุณและจัดการการวางแผนคิวรี การดำเนินการ และการประกอบคำตอบ
official