graphql-schema

Panduan praktik terbaik industri untuk merancang skema GraphQL yang intuitif, berperforma tinggi, dan mudah dipelihara. Mencakup prinsip desain inti termasuk organisasi tipe yang berpusat pada klien, pola nullability eksplisit, dan strategi evolusi yang kompatibel ke belakang. Menyediakan dokumentasi referensi tentang tipe, konvensi penamaan, pagination berbasis kursor, pemodelan kesalahan, dan pertimbangan keamanan. Mencakup pola praktis untuk antarmuka, union, tipe input, mutasi, dan strategi ID dengan contoh kode...

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)

Lebih banyak skill dari apollographql

apollo-federation
apollographql
Apollo Federation memungkinkan penggabungan beberapa API GraphQL (subgraf) menjadi satu supergraf yang terpadu.
apollo-ios
apollographql
Apollo iOS adalah klien GraphQL yang diketik secara kuat untuk platform Apple. Ia menghasilkan tipe Swift dari operasi dan skema GraphQL Anda, serta menyertakan klien async/await, cache yang dinormalisasi (berbasis memori atau SQLite), transport HTTP berbasis interceptor yang dapat dipasang untuk menangani kueri, mutasi, dan langganan multipart, serta transport WebSocket opsional (graphql-transport-ws) yang dapat membawa jenis operasi apa pun.
apollo-router
apollographql
Apollo Router adalah router graf berkinerja tinggi yang ditulis dalam Rust untuk menjalankan supergraf Apollo Federation 2. Router ini berada di depan subgraf Anda dan menangani perencanaan kueri, eksekusi, serta komposisi respons.
apollo-router-plugin-creator
apollographql
Buat plugin Rust asli untuk Apollo Router.
apollo-server
apollographql
Panduan lengkap untuk membangun server GraphQL dengan Apollo Server 5.x di berbagai framework. Mencakup definisi skema, resolver, pengaturan konteks, dan penanganan kesalahan dengan dukungan TypeScript. Mendukung mode mandiri untuk pembuatan prototipe dan integrasi dengan Express, Fastify, Koa, serta lingkungan serverless. Termasuk pola resolver, autentikasi/otorisasi, plugin, DataLoader untuk pencegahan N+1, dan teknik optimasi kinerja. Menyediakan dokumentasi referensi untuk sumber data, kesalahan...
graphql-operations
apollographql
Panduan praktik terbaik untuk menulis operasi GraphQL yang efisien dan aman tipe serta mengaturnya dengan fragmen. Mencakup kueri, mutasi, langganan, dan fragmen dengan konvensi penamaan, sintaks variabel, dan penggunaan direktif. Menekankan prinsip inti: hanya meminta bidang yang diperlukan, beri nama semua operasi, gunakan variabel alih-alih nilai hardcoded, dan sertakan bidang id untuk cacheability. Merekomendasikan penempatan fragmen bersama komponen dan menggunakan direktif @include / @skip untuk bidang kondisional...
rover
apollographql
CLI Apollo Rover untuk mengelola skema GraphQL, federasi, dan pengembangan supergraph lokal. Publikasikan, ambil, dan validasi skema subgraph; buat supergraph terfederasi secara lokal atau melalui GraphOS. Termasuk pemeriksaan skema (validasi pra-deploy), linting, dan introspeksi dari server yang berjalan. Perintah rover dev memulai Router lokal dengan komposisi skema otomatis untuk alur kerja pengembangan. Mendukung pola CI/CD dengan validasi periksa-sebelum-publikasi dan output JSON untuk skrip. Memerlukan...
rust-best-practices
apollographql
Standar pengkodean Rust idiomatik berdasarkan buku pedoman praktik terbaik Apollo GraphQL. Mencakup sembilan area inti: gaya dan idiom pengkodean, linting clippy, optimasi kinerja, penanganan error, pola pengujian, generik dan dispatch, pola state tipe, dokumentasi, dan keamanan pointer. Menekankan peminjaman daripada kloning, penanganan error berbasis Result dengan thiserror/anyhow, dan profiling kinerja dengan build rilis. Termasuk panduan referensi cepat tentang pola kepemilikan, penghindaran panic,...