GraphQL Cheatsheet - GraphQL Query Language & API Reference
All essential GraphQL commands organized by use case, with 42+ entries you can copy and run directly. Find the right command fast when you need it.
Back to LanguagesQuery 9
{ user { id name } }基础查询
{ user(id: 1) { name } }带Options查询
{ users { id name posts { title } } }嵌套查询
query GetUser($id: ID!) { user(id: $id) { name } }带variable的查询
{ user { name email } posts { title } }多field查询
{ user(id: 1) { name @include(if: $withName) } }conditionpackage含field
{ user(id: 1) { name @skip(if: $skipName) } }conditionJump过field
{ me: user(id: 1) { name } you: user(id: 2) { name } }别名查询同type多field
{ users(first: 10) { edges { node { name } } } }Connection式分页查询
Mutation 5
mutation { createUser(name: "Alice") { id } }基础变更
mutation CreateUser($input: CreateUserInput!) { createUser(input: $input) { id } }带variable的变更
mutation { updateUser(id: 1, name: "Bob") { id name } }Update操作
mutation { deleteUser(id: 1) { id } }Delete操作
mutation { bulkCreate(input: [{ name: "A" }, { name: "B" }]) { id } }batchCreate操作
Subscription 3
subscription { newMessage { content } }subscribe新消息
subscription { userUpdated(id: 1) { name } }subscribeUserUpdate
subscription($roomId: ID!) { messageAdded(roomId: $roomId) { id content } }带variable的subscribe
Schema 10
type User { id: ID! name: String! email: String }定义object type
input CreateUserInput { name: String! email: String! }定义Inputtype
enum Role { ADMIN USER GUEST }定义枚举
union SearchResult = User | Post定义联合type
interface Node { id: ID! }定义interface
type User implements Node { id: ID! name: String! }实现interface
scalar DateTime自定义标量type
type Query { user(id: ID!): User }定义查询根
type Mutation { createUser(input: CreateUserInput!): User }定义变更根
directive @auth(requires: Role!) on FIELD_DEFINITION自定义指令Declare
Fragments & Directives 5
fragment UserFields on User { id name email }定义片段
{ user { ...UserFields } }使用片段
{ user { ...UserFields ... on Admin { permissions } } }内联片段处理联合type
@deprecated(reason: "Use newField")标记field废弃
@cacheControl(maxAge: 3600)cache指令
Introspection 5
{ __schema { types { name } } }查询所有type
{ __type(name: "User") { fields { name type { name } } } }查询单个typefield
{ __schema { queryType { name } mutationType { name } } }查询根type
{ __type(name: "User") { fields { name args { name type { name } } } } }查询fieldOptions
{ __schema { directives { name locations } } }查询所有指令
Advanced 5
query($id: ID!, $withPosts: Boolean!) { user(id: $id) { name posts @include(if: $withPosts) { title } } }variable与指令组合
{ users(first: 10, after: "cursor") { pageInfo { hasNextPage endCursor } } }Relay 风格分页
{ user { name } } # 期望 user.name,错误会被聚合返回错误与数据同时返回
query @cost(complexity: 10) { user { name } }查询复杂度指令
extend type User { age: Int }扩展已有type(type扩展)
💡 Tips
- GraphQL 查询返回的field由客户端决定,Service端不需要返回所有field。
- variable用 \$ 前缀,在查询外部定义,避免stringconcat。
- Schema 定义语言(SDL)是 GraphQL 的核心,先定义type再实现解析器。
- 内省查询(__schema/__type)可用于生成 TypeScript type与代码prompt工具。
- 分页recommended采用 Relay Connection规范(edges/node/pageInfo),兼顾Forward与Backward翻页。
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Found an error? Report it
Wrong command or description? Open an issue to help us fix it.
Found an error? Report it