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
  • Give every new argument or input field a default value, or make it nullable. Existing clients don't send it, so a required field without a default makes their requests fail validation.

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
  • ALWAYS paginate lists that can grow without limit, such as Query.users or Post.comments: return a connection and give first a default page size
  • NEVER expose database internals in schema
  • NEVER break backwards compatibility without deprecation
  • NEVER add a required argument or input field without a default value
  • 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-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.
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.
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.
apollo-router-plugin-creator
apollographql
Tạo plugin Rust gốc cho Apollo Router.
apollo-server
apollographql
Hướng dẫn hoàn chỉnh để xây dựng máy chủ GraphQL với Apollo Server 5.x trên nhiều framework. Bao gồm định nghĩa schema, resolver, thiết lập context và xử lý lỗi với hỗ trợ TypeScript. Hỗ trợ chế độ standalone để tạo nguyên mẫu và tích hợp với Express, Fastify, Koa cùng môi trường serverless. Bao gồm các mẫu resolver, xác thực/phân quyền, plugin, DataLoader để ngăn chặn N+1 và các kỹ thuật tối ưu hiệu suất. Cung cấp tài liệu tham khảo cho nguồn dữ liệu, lỗi...
graphql-operations
apollographql
Hướng dẫn thực hành tốt nhất để viết các thao tác GraphQL hiệu quả, an toàn về kiểu và tổ chức chúng với các fragment. Bao gồm queries, mutations, subscriptions và fragments với quy ước đặt tên, cú pháp biến và cách sử dụng chỉ thị. Nhấn mạnh các nguyên tắc cốt lõi: chỉ yêu cầu các trường cần thiết, đặt tên cho tất cả các thao tác, sử dụng biến thay vì giá trị cứng, và bao gồm các trường id để có thể lưu vào bộ nhớ đệm. Khuyến nghị đặt fragment cùng với component và sử dụng các chỉ thị @include / @skip cho các trường có điều kiện...
rover
apollographql
CLI Apollo Rover để quản lý schema GraphQL, federation và phát triển supergraph cục bộ. Xuất bản, tải và xác thực schema subgraph; soạn supergraph liên kết cục bộ hoặc qua GraphOS. Bao gồm kiểm tra schema (xác thực trước khi triển khai), linting và introspection từ các máy chủ đang chạy. Lệnh rover dev khởi động Router cục bộ với tính năng soạn schema tự động cho quy trình phát triển. Hỗ trợ các mẫu CI/CD với xác thực trước khi xuất bản và đầu ra JSON cho việc viết script. Yêu cầu...
rust-best-practices
apollographql
Các tiêu chuẩn mã hóa Rust thành ngữ dựa trên sổ tay thực hành tốt nhất của Apollo GraphQL. Bao gồm chín lĩnh vực cốt lõi: phong cách và thành ngữ mã hóa, linting clippy, tối ưu hóa hiệu suất, xử lý lỗi, mẫu kiểm thử, generics và dispatch, mẫu trạng thái kiểu, tài liệu và an toàn con trỏ. Nhấn mạnh việc mượn hơn là sao chép, xử lý lỗi dựa trên Result với thiserror/anyhow, và phân tích hiệu suất với bản phát hành. Bao gồm hướng dẫn tham khảo nhanh về các mẫu sở hữu, tránh panic,...