AGENT SKILL

graphql-schema

Designs GraphQL schemas and resolvers that scale - domain-modeled types, Relay pagination, DataLoader batching to kill N+1, mutation payloads with typed user errors, and depth/complexity limits that stop abusive queries. Use when someone asks "how should I structure this GraphQL type", "my resolvers are hammering the database", "cursor or offset pagination", "how do I version a GraphQL API", or is designing or reviewing a schema or federation split. Do NOT use for REST or RPC endpoint design - use api-design instead; do NOT use for the underlying table design - use database-schema instead; for hunting existing N+1s in a codebase, use n-plus-one-hunter.

License 许可
MIT
所属仓库
SkillMedev/skills
最近更新
2026-10-14
Stars
★ 25
Forks
3

01 前言 Front Matter

name
graphql-schema
metadata
{ "title": "GraphQL Schema" }
description
Designs GraphQL schemas and resolvers that scale - domain-modeled types, Relay pagination, DataLoader batching to kill N+1, mutation payloads with typed user errors, and depth/complexity limits that stop abusive queries. Use when someone asks "how should I structure this GraphQL type", "my resolvers are hammering the database", "cursor or offset pagination", "how do I version a GraphQL API", or is designing or reviewing a schema or federation split. Do NOT use for REST or RPC endpoint design - use api-design instead; do NOT use for the underlying table design - use database-schema instead; for hunting existing N+1s in a codebase, use n-plus-one-hunter.

03 SKILL.md 正文

GraphQL Schema

A GraphQL schema is a public contract that clients build against for years, and a resolver layer is an open invitation for N+1 queries and abusive nesting. This skill designs schemas clients love and servers survive: model the domain rather than the database, batch every relationship resolver, and put hard limits on what a single query may cost - because removing a shipped field or fixing a hot N+1 under load is 10x the effort of designing it right.

Operating procedure

Step 1: Gather inputs

  • The domain nouns and the operations clients actually need (screens/use-cases, not tables).
  • Largest list sizes and expected query shapes; who the clients are (first-party only vs public API - public means stricter limits).
  • Data sources behind each type and which relationships cross a database or service boundary.
  • Single graph or federated subgraphs.

Step 2: Model the graph

  1. Model around domain nouns, not database tables - the schema is for clients; the database is an implementation detail (table design belongs to database-schema).
  2. Use schema-first SDL as the contract; generate types from it.
  3. Prefer non-null (!) by default; make a field nullable only when null is a real, meaningful value or when the field's resolver can fail independently and you want partial results instead of a nulled-out parent chain.
  4. Return rich object types, not scalars, so fields can grow without breaking clients.
  5. Mutations return a payload type carrying the mutated entity and a userErrors list - do not model expected business failures as top-level GraphQL errors.

Step 3: Paginate every list that can grow

  • Cursor-based connections (Relay spec) for anything unbounded; offset pagination breaks under concurrent writes and invites offset: 500000 table scans.
  • Expose edges { node cursor } and pageInfo { hasNextPage endCursor }.
  • Require first/last with a hard cap - 100 is the standard ceiling; default to 20-25 when the client omits it. An uncapped list argument is a self-serve denial-of-service endpoint.

Step 4: Batch every relationship resolver

The classic trap: resolving items.customer fires one query per row. Every resolver that crosses a table or service boundary gets a DataLoader, created per-request (a shared loader leaks data across users and requests).

const customerLoader = new DataLoader(async (ids) => {
  const rows = await db.customers.whereIn('id', ids);
  return ids.map(id => rows.find(r => r.id === id)); // preserve input order; missing = undefined
});
// resolver:
Order.customer = (order) => customerLoader.load(order.customerId);

Verify with a query log: a page-of-50-orders query should issue on the order of 2-3 SQL queries, not 51. Auditing an existing codebase for these is n-plus-one-hunter's job.

Step 5: Enforce query cost limits

  • Depth limit: 8-10 for public APIs; first-party-only graphs can allow up to ~15. Anything deeper is either a malicious query or a client that should be paginating.
  • Complexity/cost limit: assign each field a cost (scalar 1, object 2, list = child cost × requested page size) and reject queries over a budget in the low thousands of points, tuned so the heaviest legitimate client query passes with ~2x headroom.
  • Reject at validation time, before execution - a rejected query should cost you parsing, not database load.
  • For public APIs, add per-client rate limiting denominated in cost points, not request count, since one GraphQL request can equal 500 REST calls.

Step 6: Plan evolution and federation

  • Version by evolving additively; deprecate with @deprecated(reason: "use newField") and remove only after client traffic on the field hits zero. Never break a shipped field (deprecation rollout strategy: api-versioning-strategist).
  • Federation: split by subgraph ownership; use @key to define entity references; each subgraph owns its fields and the gateway composes. Keep entities resolvable by their key in every subgraph that extends them.

Step 7: Harden the edges

  • Authorization per-field, not just at the query entry point - a nested path reaches any type the graph connects.
  • Never expose raw database errors; map to typed, safe messages.
  • Errors mid-list: return partial results with the errors array rather than failing the whole response.
  • Caching: GraphQL-over-POST defeats HTTP caching - use persisted queries (also a security win: only known queries execute) or response caching keyed on query + variables.

Worked artifact: schema design, bad vs good

Bad - tables leaked into the schema, unbounded list, scalar foreign key, throw-only mutation:

type Query {
  orders_tbl(user_id: Int): [OrderRow]
}
type OrderRow {
  id: Int
  status_code: Int      # magic number, client must decode
  customer_id: Int      # scalar FK - client must make a second query
}
type Mutation {
  updateOrder(id: Int, status: Int): Boolean   # failures = thrown errors only
}

Good - domain types, enum, object relationships, capped connection, error-carrying payload:

type Query {
  order(id: ID!): Order
  orders(first: Int! = 25, after: String): OrderConnection!   # first capped at 100
}
type Order {
  id: ID!
  status: OrderStatus!
  items: [LineItem!]!
  customer: Customer!            # object, resolved via DataLoader
}
enum OrderStatus { PENDING PAID SHIPPED }
type OrderConnection {
  edges: [OrderEdge!]!
  pageInfo: PageInfo!
}
type Mutation {
  updateOrderStatus(input: UpdateOrderStatusInput!): UpdateOrderStatusPayload!
}
type UpdateOrderStatusPayload {
  order: Order
  userErrors: [UserError!]!      # expected failures as data, not exceptions
}
type UserError { field: [String!], code: String!, message: String! }

Deliverable

Produce the SDL schema plus a resolver plan listing: every relationship field with its DataLoader, the pagination caps per connection, the depth and complexity limits with the cost model, the authorization rule per sensitive field, and the deprecation policy for future changes.

Do NOT

  • Do not mirror database tables into the schema - you freeze your storage layout into a public contract.
  • Do not ship an unbounded list field; every list that can grow gets a connection with a capped page size.
  • Do not resolve relationships row-by-row; no relationship resolver ships without a batch loader.
  • Do not share DataLoader instances across requests - cross-user cache leakage.
  • Do not model expected business failures as thrown GraphQL errors; use payload userErrors so clients can handle them typed.
  • Do not remove or retype a shipped field; deprecate additively and wait for zero traffic.
  • Do not rely on entry-point auth; enforce per-field.

Quality bar

  • Every type is a domain noun; no _tbl, _id-scalar-FK, or status-code-integer smells remain.
  • Every list is a connection with an enforced page cap; depth and complexity limits are enabled and tested with a hostile deeply-nested query.
  • A logged trace of the heaviest client query shows batched loads (single-digit queries per request).
  • Every mutation returns a payload with userErrors; every error path returns safe messages plus partial results where possible.
  • Federation entities resolve by @key from every extending subgraph.

04 同仓库更多 Skills

SkillMedev
SkillMedev/skills

有氧运动计划

zone-2-cardio-plan

当有人要求制定稳态有氧训练计划、进行有氧基础训练或达到第二区间、安排轻松或耐力型的每周有氧训练、按心率区间训练,或提高静息心率、最大摄氧量或耐力时,可以使用此方案。该方案构建了一个包含训练计划结构、训练方式选择和进度安排的心率区间有氧训练模块。本方案仅提供一般健身指导,不构成医疗建议。请勿用于设计力量或举重训练计划(请使用力量训练计划)、高强度间歇训练 (HIIT) 或循环训练(请使用体能训练和 HIIT 计划),或灵活性/柔韧性训练(请使用灵活性训练计划)。

★ 25
SkillMedev
SkillMedev/skills

youtube-script-writer

Writes retention-engineered long-form YouTube scripts on a hook/body/payoff architecture - a first-30-seconds hook that survives the retention cliff, a beat-sheet body with mini-hooks and bridges, timed pattern interrupts, and a payoff that over-delivers - with B-roll and editing cues marked inline. Use when someone says "script my next video", "my retention graph dies at the start", "tighten this draft", or "outline a video on X". Do NOT use for the 3-second opener of TikTok, Reels, or Shorts - use video-hook-writer instead; do NOT use for spoken voiceover synced to on-screen tutorial actions - use narration-script instead; do NOT use for a sales demo built on a buyer's discovery notes - use demo-script instead; do NOT use to design the thumbnail and title - use thumbnail-concept instead.

★ 25
SkillMedev
SkillMedev/skills

win-back-campaign

Designs segmented win-back email sequences for dormant users - dormancy tiers, a three-beat sequence with a single proportional incentive, and a suppression rule - and measures success by re-activation, not opens. Use when someone says "our users went quiet", "design a win-back campaign", "how do I re-engage lapsed customers", "should we email people who haven't logged in for months", or lifecycle messaging has stopped landing with a lapsed segment. Do NOT use for recovering an unfinished checkout - use abandoned-cart-sequence instead. Do NOT use for diagnosing and fixing why active customers cancel - use churn-reduction instead. Do NOT use for building always-on onboarding or nurture flows - use email-drip-builder instead.

★ 25
SkillMedev
SkillMedev/skills

wikipedia-style

Writes neutral, verifiable, encyclopedic prose following Wikipedia's core content policies and manual of style - NPOV, inline citations, notability-backed claims, and a strict ban on promotional language. Use when someone asks "write this like a Wikipedia article", "draft a Wikipedia page for this company or person", "make this neutral and encyclopedic", or "why does my article draft keep getting rejected". Do NOT use for persuasive or opinion writing - use op-ed-writer instead; for thesis-driven academic papers, use academic-essay; for verifying claims in existing text, use fact-checker.

★ 25
SkillMedev
SkillMedev/skills

whitepapers

Structures and writes authoritative B2B whitepapers - a standalone executive summary, a data-sized problem statement, methodology, evidence-driven findings, recommendations, and a soft call to action - flagging every claim that needs a real citation. Use when a marketer asks "write a whitepaper on this topic", "turn our research into a lead-gen asset", "make this credible enough for executives", or a draft reads like a product pitch instead of analysis. Do NOT use for customer-story-driven proof - use case-study-builder instead; for policy audiences, use policy-brief.

★ 25
SkillMedev
SkillMedev/skills

white-space-analysis

Finds unmet market gaps by ranking jobs-to-be-done on importance versus satisfaction, overlaying them against current solutions and workarounds, and validating that each gap is real, winnable, and worth winning. Use when someone asks "where are the gaps in this market", "what's underserved here", "where should we play next", or is hunting for product, feature, or market-entry opportunities. Do NOT use for profiling competitors and building battle cards - use competitive-intelligence instead; for putting a dollar figure on an identified opportunity - use market-sizing instead; for extracting jobs-to-be-done from raw customer interviews - use jtbd-extractor instead.

★ 25