graphql-schema

Hướng dẫn thực tiễn tốt nhất trong ngành để thiết kế lược đồ GraphQL trực quan, hiệu suất cao và dễ bảo trì. Bao gồm các nguyên tắc thiết kế cốt lõi như tổ chức kiểu dữ liệu tập trung vào máy khách, các mẫu nullability rõ ràng và chiến lược phát triển tương thích ngược. Cung cấp tài liệu tham khảo về các kiểu dữ liệu, quy ước đặt tên, phân trang dựa trên con trỏ, mô hình hóa lỗi và các cân nhắc bảo mật. Bao gồm các mẫu thực tế cho interfaces, unions, input types, mutations và chiến lược ID kèm ví dụ mã...

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)

Thêm skills từ apollographql

apollo-client
apollographql
Apollo Client là một thư viện quản lý trạng thái toàn diện cho JavaScript, cho phép bạn quản lý cả dữ liệu cục bộ và từ xa với GraphQL. Phiên bản 4.x mang lại cải tiến về bộ nhớ đệm, hỗ trợ TypeScript tốt hơn và tương thích với React 19.
official
apollo-client
apollographql
Hướng dẫn toàn diện để xây dựng ứng dụng React với Apollo Client 4.x, bao gồm truy vấn, thay đổi dữ liệu, bộ nhớ đệm và quản lý trạng thái. Hỗ trợ nhiều framework và thiết lập React: ứng dụng phía client (Vite, CRA), Next.js App Router với React Server Components, React Router 7 với streaming SSR, và TanStack Start. Bao gồm các hooks cho truy vấn (useQuery, useLazyQuery), thay đổi dữ liệu (useMutation) và các mẫu dựa trên Suspense (useSuspenseQuery, useBackgroundQuery) cho React 18+ và 19 hiện đại...
official
apollo-connectors
apollographql
Tích hợp REST API vào siêu đồ thị GraphQL bằng cách sử dụng các chỉ thị @source và @connect. Cung cấp quy trình 5 bước có cấu trúc: nghiên cứu cấu trúc API, triển khai lược đồ với các chỉ thị, xác thực qua rover supergraph compose, thực thi các kết nối và kiểm tra phạm vi bao phủ. Hỗ trợ cấu hình yêu cầu bao gồm tiêu đề, tải trọng nội dung, xử lý hàng loạt cho các mẫu N+1 và tiêm biến môi trường qua $env. Xử lý ánh xạ phản hồi với lựa chọn trường, bí danh, lựa chọn phụ cho dữ liệu lồng nhau và thực thể...
official
apollo-federation
apollographql
Apollo Federation cho phép kết hợp nhiều API GraphQL (subgraph) thành một siêu đồ thị thống nhất.
official
apollo-ios
apollographql
Apollo iOS là một trình khách GraphQL có kiểu mạnh dành cho các nền tảng Apple. Nó tạo ra các kiểu Swift từ các thao tác và lược đồ GraphQL của bạn, đồng thời cung cấp một trình khách async/await, bộ nhớ đệm chuẩn hóa (trong bộ nhớ hoặc dùng SQLite), một lớp truyền tải HTTP dựa trên interceptor có thể cắm thêm để xử lý các truy vấn, biến đổi và đăng ký đa phần, cùng với một lớp truyền tải WebSocket tùy chọn (graphql-transport-ws) có thể mang bất kỳ loại thao tác nào.
official
apollo-kotlin
apollographql
Apollo Kotlin là một máy khách GraphQL được định kiểu mạnh, tạo ra các mô hình Kotlin từ các thao tác và lược đồ GraphQL của bạn, có thể được sử dụng trong các dự án Android, JVM và Kotlin Multiplatform.
official
apollo-mcp-server
apollographql
Kết nối các tác nhân AI với API GraphQL thông qua Giao thức Ngữ cảnh Mô hình (Model Context Protocol) với các công cụ nội quan và thao tác tích hợp. Hiển thị các thao tác GraphQL dưới dạng công cụ MCP; hỗ trợ ba nguồn thao tác: tệp cục bộ, bộ sưu tập GraphOS Studio và tệp kê khai truy vấn đã lưu. Cung cấp bốn công cụ nội quan (introspect, search, validate, execute) để khám phá lược đồ và kiểm tra truy vấn tùy chỉnh; chế độ thu nhỏ giảm mức sử dụng token với ký hiệu nhỏ gọn. Xác thực có thể cấu hình qua tiêu đề tĩnh,...
official
apollo-router
apollographql
Apollo Router là một bộ định tuyến đồ thị hiệu suất cao được viết bằng Rust để chạy các siêu đồ thị Apollo Federation 2. Nó nằm phía trước các đồ thị con của bạn và xử lý việc lập kế hoạch truy vấn, thực thi và tổng hợp phản hồi.
official