Graphql schema design

From FerroxLabs/wayland · View interactive version

Graphql schema design is an AI agent skill from the GitHub repository FerroxLabs/wayland. Guides expert-level graphql schema design implementation: api-design and design-patterns decision frameworks, production-ready patterns, and concrete templates for graphql schema design workflows. Use when the user asks about graphql schema design, graphql schema design configuration, or api-design

How to use this skill

Skill file

GraphQL Schema Design

When to Use

Use this skill when:

Do NOT use this skill when:

Process

1. Identify the Domain Model and Access Patterns

Before writing a single type definition, map the domain thoroughly.

2. Define the Type System Foundation

Establish scalar, enum, interface, and union types before building object types.

3. Design the Query Root

Structure the Query type to be discoverable, consistent, and client-friendly.

4. Design Mutations

Mutations require the most careful design because they represent state changes with side effects.

5. Model Relationships and Connections

Graph relationships are the core value proposition of GraphQL -- design them carefully.

6. Apply Naming Conventions and Documentation

Consistent naming is not cosmetic -- it determines how discoverable and maintainable the schema is.

7. Validate Schema Design Decisions

Before finalizing, run the schema through a validation checklist.

8. Document Schema Evolution Strategy

Schema design is not a one-time event -- establish the governance model upfront.

Output Format

## GraphQL Schema Design Review

### Domain Model Summary
- Core entities: [list with brief description]
- Key relationships: [entity → entity with cardinality]
- Primary access patterns: [top 5 queries driving design]
- Write operations: [top 5 mutations driving design]

### Schema Design Decisions

| Decision | Choice | Rationale | Trade-offs |
|----------|--------|-----------|------------|
| Pagination style | Cursor (Relay spec) / Offset | [reason] | [trade-off] |
| Error handling | Payload union / errors array | [reason] | [trade-off] |
| Nullability policy | Strict non-null / permissive | [reason] | [trade-off] |
| ID strategy | Global ID (Node) / local ID | [reason] | [trade-off] |
| Naming convention | Standard camelCase/PascalCase | [reason] | [trade-off] |

### Schema Structure

#### Custom Scalars
[List with justification for each non-standard scalar]

#### Interfaces
[List with the contract each interface enforces]

#### Type Definitions (SDL)

[Full SDL for all types, organized as:]
-- Scalars and Enums
-- Interfaces
-- Core object types (most important entities first)
-- Connection types
-- Input types (query filters, mutation inputs)
-- Payload types (mutation returns)
-- Query root
-- Mutation root

#### Relationship Map
[Diagram or table showing type → type traversal paths]

### Validation Results

| Check | Status | Notes |
|-------|--------|-------|
| Naming conventions | ✅ / ⚠️ / ❌ | [details] |
| All mutations have payload types | ✅ / ⚠️ / ❌ | [details] |
| All collections paginated | ✅ / ⚠️ / ❌ | [details] |
| N+1 risks documented | ✅ / ⚠️ / ❌ | [details] |
| Breaking change check | ✅ / ⚠️ / ❌ | [details] |
| Description coverage | ✅ / ⚠️ / ❌ | [details] |

### Evolution Notes
- Fields marked for deprecation: [list]
- Planned additions: [list]
- Breaking changes required (with migration plan): [list]

Rules

  1. NEVER remove a field without first deprecating it for at least 30 days. Field removal is a breaking change for all clients even if no client currently uses the field -- unknown clients, generated SDKs, and cached queries all depend on introspection stability. Use @deprecated(reason: "...") and remove only after confirming zero usage via query analytics.
  1. NEVER change a field from nullable to non-null without treating it as a breaking change. Making a field non-null (String → String!) breaks any client that handles null values for that field. The reverse (non-null to nullable) is safe. When in doubt, start nullable and tighten later only after all clients are verified.
  1. ALWAYS put the errors field on every mutation payload. A mutation that returns createUser: User! has no mechanism to communicate domain errors (email already taken, validation failure) without throwing GraphQL errors, which forces clients to parse error extensions. The errors: [UserError!]! field on a payload type is the correct pattern.
  1. NEVER use generic types as mutation inputs. Reusing output types as input types (createUser(user: User)) breaks immediately when the output type adds computed fields, relations, or server-generated fields like id and createdAt. Always create dedicated CreateUserInput, UpdateUserInput types with exactly the fields the client should provide.
  1. ALWAYS paginate collections that could exceed 20-30 items in production. A field returning [Post!]! with no pagination argument will eventually return thousands of objects, causing memory and latency problems on both server and client. The only exception is truly bounded collections (a user's 2FA recovery codes, a product's fixed set of images with a hard cap of 10).
  1. NEVER design around a specific client's current data shape. Schema design based on "this is what the React component tree looks like today" creates a brittle, client-coupled schema that cannot serve mobile apps, partner integrations, or internal tooling. Design around the domain model; let clients compose what they need.
  1. ALWAYS implement the Node interface on top-level entities. The node(id: ID!): Node root query with global IDs (base64-encoded TypeName:localId) is not just a Relay pattern -- it enables any client to efficiently refetch stale data, powers cache normalization in Apollo Client, and provides a universal entity lookup mechanism.
  1. NEVER put business logic in the schema design itself. Schema defines structure and contracts, not computation rules. Avoid temptations like adding authorization flags directly as scalar fields (canUserEdit: Boolean! computed per-viewer) unless this is a deliberate, documented design choice -- these fields are expensive (one resolver call per object in a list) and couple the schema to viewer-specific state.
  1. ALWAYS distinguish between null-because-absent and null-because-failed. A nullable field thumbnailUrl: String could mean "this product has no thumbnail" (intentional) or "the CDN URL resolver failed" (error). Use the @semanticNonNull pattern in documentation, or model the error case explicitly with a union type. Clients cannot distinguish these cases without schema-level clarity.
  1. NEVER add a field to the schema until you have a resolver implementation plan. Underfetching is a client problem; an unresolvable field is a server contract violation. If a field is added to the schema but the resolver returns null due to unimplemented logic, clients build against a broken contract. Coordinate schema additions with resolver work, or gate new fields behind a feature flag in schema stitching.

Edge Cases

Migrating a REST API to GraphQL Incrementally

When wrapping existing REST endpoints in a GraphQL schema, resist the urge to mirror the REST resource structure 1:1. REST resources are organized around CRUD operations on individual entities; GraphQL schemas should be organized around how clients actually traverse the data graph. A REST endpoint GET /users/{id}/orders?status=COMPLETED becomes user(id: ID!) { orders(filter: { status: COMPLETED }) { ... } } -- the nesting reflects ownership, not endpoint hierarchy. Use schema stitching or a thin gateway layer so the GraphQL schema can evolve independently of the underlying REST endpoints. Start with read-only query types wrapping existing GET endpoints, then introduce mutations only once the query layer is stable.

Polymorphic Types: Interface vs. Union

The choice between interface and union is often confused. Use an interface when the polymorphic types share a meaningful common contract that clients will query against -- SearchResult types that are all Node implementors with id and title fields should be an interface, because clients can write a fragment that applies to all of them. Use a union when the types are genuinely heterogeneous with no shared fields -- PaymentMethod = CreditCard | BankTransfer | CryptoCurrency should be a union because there is no meaningful shared contract. The practical test: if you find yourself adding shared fields to a union type, convert it to an interface. Inline fragments (... on CreditCard { cardNumber lastFour }) work identically on both.

Multi-Tenancy and Viewer-Scoped Data

In multi-tenant SaaS applications, some data is global (product catalog, public user profiles) and some is viewer-scoped (the current user's orders, their organization's settings). Avoid adding currentUser: User fields directly on other types (order { currentUserCanApprove: Boolean! }) because this couples the schema to the authenticated context and makes the schema unusable for batch processing, admin tools, or unauthenticated access. Instead, put viewer-specific capabilities on a dedicated viewer: Viewer root query type that contains the authenticated user's context. The Viewer type holds orders, permissions, organization -- all scoped to the authenticated session -- while global types remain free of viewer context.

Schema Federation Across Microservices

In Apollo Federation or schema stitching architectures, each subgraph owns a slice of the overall schema. The key design rules are: every entity that is referenced across subgraph boundaries must implement @key with a stable, queryable field (almost always the primary id); subgraphs should extend types only to add fields they own the data for; and no subgraph should extend a type just to add computed fields that belong in the consuming application layer. The main failure mode in federated schemas is ownership ambiguity -- two teams both adding fields to the User type without coordination causes type conflicts at composition time. Establish a schema ownership registry (a simple table in a wiki is sufficient for teams under 20) that maps each type and field to an owning team.

Recursive and Hierarchical Data Structures

Organizational hierarchies, category trees, comment threads, and file systems are common recursive structures. Do not model these with unbounded recursive types (Category { children: [Category!]! }). Instead, add depth control arguments (children(depth: Int = 1): [Category!]!) and document the maximum supported depth. For very deep or very large trees (org charts with 10,000 nodes), consider a flattened list representation at the schema level (organization { allDepartments: [Department!]! } with parentId on each Department) and let the client reconstruct the tree -- this approach is more efficient for large datasets and avoids the query complexity explosion of deeply nested traversals.

Schema Design for Real-Time Subscriptions

Subscriptions share the type system with queries and mutations, but their design requirements differ. A subscription payload should contain the minimum data needed to update the client's local state -- it should not be a full entity dump. Design subscription types to include the event type (UserUpdatedEvent { eventType: UserEventType!, user: User!, changedFields: [String!]! }), not just the current state. This allows clients to apply optimistic updates correctly and distinguish create/update/delete events. Never model a subscription to return a connection type -- subscriptions push individual events, not paginated collections. For filtered subscriptions (onOrderUpdated(orderId: ID!)), document the filter semantics precisely and limit the number of filterable dimensions to prevent server-side subscription fan-out.

Large Schema Governance (50+ Types)

When a schema grows beyond 50 types, tooling and process become as important as design patterns. Split the SDL across multiple files organized by domain (user, product, order, payment) and merge them at build time. Use graphql-schema-linter with a configuration file that enforces description coverage (at least 80% of public fields documented), naming conventions, and deprecation format. Establish a schema review process: every PR that adds or modifies schema definitions requires review by the schema owner (typically a platform or API team member) in addition to code review. Track field usage with GraphQL observability tools to identify candidates for deprecation -- fields with zero usage over 90 days should be deprecated proactively.

Example

Input: "We're building an e-commerce platform with a team of 8 engineers. We have Products with variants (size/color), a Cart and Checkout flow, Orders with line items, and User accounts with addresses. We need to support both a web storefront and a mobile app as clients. What should our GraphQL schema look like?"

Output:

GraphQL Schema Design Review -- E-Commerce Platform

Domain Model Summary

1. Product listing with filtering by category, price range, availability (paginated)

2. Product detail page with all variants, images, and descriptions

3. Cart retrieval with current prices and availability status

4. Order history for authenticated user (paginated, filterable by status)

5. Checkout flow: validate cart → create order → process payment

1. Add/update/remove cart items

2. Create order from cart (checkout)

3. Update user profile and addresses

4. Apply discount codes to cart

5. Cancel order (status transition)

Schema Design Decisions

| Decision | Choice | Rationale | Trade-offs |

|----------|--------|-----------|------------|

| Pagination style | Cursor (Relay spec) | Product and order lists can be large; real-time inventory changes make offset pagination unreliable | Slightly more complex client code vs. offset |

| Error handling | Payload type with errors: [UserError!]! | Checkout and cart operations have rich domain errors (out of stock, invalid coupon) needing typed responses | Clients must check both errors and the entity field |

| Nullability policy | Strict non-null on reliable data; nullable on computed/external-dependent fields | Reduces client null-checking burden on core fields | Server must guarantee non-null fields -- requires careful resolver implementation |

| ID strategy | Global ID implementing Node interface | Both web and mobile clients use Apollo Client; global IDs enable cache normalization without configuration | Base64 encoding adds minor overhead; local IDs exposed via separate legacyId field for REST migration |

| Naming convention | Standard GraphQL conventions: camelCase fields, PascalCase types, SCREAMING_SNAKE_CASE enums | Consistent with client SDK generation tools and GraphQL community norms | No trade-off for new projects |

Schema Structure

Custom Scalars
Interfaces
Type Definitions (SDL)
# ─── Scalars ────────────────────────────────────────────────────────────
scalar DateTime
scalar Decimal
scalar URL
scalar JSON

# ─── Enums ──────────────────────────────────────────────────────────────
enum OrderStatus {
  PENDING
  PAYMENT_PROCESSING
  CONFIRMED
  SHIPPED
  DELIVERED
  CANCELLED
  REFUNDED
}

enum CartItemStatus {
  AVAILABLE
  LOW_STOCK    # fewer than 5 units remaining
  OUT_OF_STOCK
  PRICE_CHANGED  # price changed since item was added
}

enum AddressType {
  SHIPPING
  BILLING
}

enum SortDirection {
  ASC
  DESC
}

enum ProductSortField {
  PRICE
  NAME
  CREATED_AT
  POPULARITY
}

# ─── Interfaces ──────────────────────────────────────────────────────────
interface Node {
  id: ID!
}

interface UserError {
  message: String!
  code: String!
}

# ─── Error Types ─────────────────────────────────────────────────────────
type ValidationError implements UserError {
  message: String!
  code: String!
  field: String!          # which input field caused the error
}

type NotFoundError implements UserError {
  message: String!
  code: String!
  entityType: String!
}

type OutOfStockError implements UserError {
  message: String!
  code: String!
  productVariant: ProductVariant!
  requestedQuantity: Int!
  availableQuantity: Int!
}

type InvalidCouponError implements UserError {
  message: String!
  code: String!
  couponCode: String!
}

# ─── Core Types ──────────────────────────────────────────────────────────
"""
A registered user account. Guests interact with Cart without a User.
"""
type User implements Node {
  id: ID!
  email: String!
  firstName: String!
  lastName: String!
  """Computed display name combining firstName and lastName."""
  displayName: String!
  isEmailVerified: Boolean!
  createdAt: DateTime!
  updatedAt: DateTime!
  addresses(type: AddressType): [Address!]!
  defaultShippingAddress: Address
  orders(
    filter: OrderFilterInput
    first: Int
    after: String
    last: Int
    before: String
  ): OrderConnection!
}

type Address implements Node {
  id: ID!
  type: AddressType!
  firstName: String!
  lastName: String!
  line1: String!
  line2: String
  city: String!
  stateOrProvince: String!
  postalCode: String!
  country: String!   # ISO 3166-1 alpha-2
  isDefault: Boolean!
}

"""
A product in the catalog. All purchasable items are ProductVariants;
Product holds shared content and metadata.
"""
type Product implements Node {
  id: ID!
  name: String!
  slug: String!
  description: String!
  """Short description for listing cards, max 160 characters."""
  shortDescription: String!
  thumbnailUrl: URL
  imageUrls: [URL!]!
  categories: [Category!]!
  variants: [ProductVariant!]!
  """
  Returns the lowest price across all available variants.
  Null if all variants are out of stock.
  """
  minPrice: Decimal
  maxPrice: Decimal
  isAvailable: Boolean!
  createdAt: DateTime!
  updatedAt: DateTime!
}

"""
A specific purchasable configuration of a Product (e.g., size=L, color=Red).
"""
type ProductVariant implements Node {
  id: ID!
  product: Product!
  sku: String!
  """
  Open-ended attribute map. Keys depend on product type: 
  e.g., {"size": "L", "color": "Red"} or {"material": "Cotton", "fit": "Slim"}.
  """
  attributes: JSON!
  """Human-readable variant label, e.g., 'Large / Red'."""
  label: String!
  price: Decimal!
  compareAtPrice: Decimal   # original price for sale display
  inventoryQuantity: Int!
  isAvailable: Boolean!
  imageUrl: URL
}

type Category implements Node {
  id: ID!
  name: String!
  slug: String!
  description: String
  parentCategory: Category
  """Direct children only. Use depth argument for subtree traversal."""
  childCategories(depth: Int = 1): [Category!]!
  products(
    filter: ProductFilterInput
    orderBy: ProductOrderByInput
    first: Int
    after: String
  ): ProductConnection!
}

"""
The current user's shopping cart. 
Anonymous carts are identified by a cartToken stored client-side.
"""
type Cart implements Node {
  id: ID!
  items: [CartItem!]!
  subtotal: Decimal!
  discountAmount: Decimal!
  estimatedTax: Decimal!
  estimatedTotal: Decimal!
  appliedCouponCode: String
  """
  Any items with status OUT_OF_STOCK or PRICE_CHANGED require 
  user acknowledgment before checkout can proceed.
  """
  hasIssues: Boolean!
  updatedAt: DateTime!
}

type CartItem implements Node {
  id: ID!
  productVariant: ProductVariant!
  quantity: Int!
  unitPrice: Decimal!        # price at time of adding to cart
  currentPrice: Decimal!     # current live price
  lineTotal: Decimal!        # quantity × currentPrice
  status: CartItemStatus!
}

"""
A completed or in-progress purchase. Line items are immutable snapshots.
"""
type Order implements Node {
  id: ID!
  orderNumber: String!       # human-readable, e.g., "ORD-20240315-4821"
  status: OrderStatus!
  lineItems: [OrderLineItem!]!
  shippingAddress: Address!
  billingAddress: Address!
  subtotal: Decimal!
  discountAmount: Decimal!
  taxAmount: Decimal!
  shippingAmount: Decimal!
  total: Decimal!
  appliedCouponCode: String
  """Null until order ships."""
  trackingNumber: String
  """Null until order ships."""
  estimatedDeliveryAt: DateTime
  placedAt: DateTime!
  updatedAt: DateTime!
}

"""
Immutable snapshot of a ProductVariant at purchase time.
Preserves price and product name even if the variant is later modified.
"""
type OrderLineItem implements Node {
  id: ID!
  """
  References the live ProductVariant. May be null if variant was deleted.
  Always use the snapshot fields (snapshotName, snapshotPrice) for display.
  """
  productVariant: ProductVariant
  snapshotName: String!      # "Blue Widget - Large / Red"
  snapshotSku: String!
  snapshotPrice: Decimal!
  quantity: Int!
  lineTotal: Decimal!
}

# ─── Connection Types (Relay Cursor spec) ───────────────────────────────
type ProductConnection {
  edges: [ProductEdge!]!
  pageInfo: PageInfo!
  totalCount: Int!
}

type ProductEdge {
  node: Product!
  cursor: String!
}

type OrderConnection {
  edges: [OrderEdge!]!
  pageInfo: PageInfo!
  totalCount: Int!
}

type OrderEdge {
  node: Order!
  cursor: String!
}

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
  startCursor: String
  endCursor: String
}

# ─── Input Types ─────────────────────────────────────────────────────────
input ProductFilterInput {
  categoryId: ID
  minPrice: Decimal
  maxPrice: Decimal
  isAvailable: Boolean
  searchQuery: String
}

input ProductOrderByInput {
  field: ProductSortField!
  direction: SortDirection!
}

input OrderFilterInput {
  status: OrderStatus
  placedAfter: DateTime
  placedBefore: DateTime
}

input AddCartItemInput {
  productVariantId: ID!
  quantity: Int!
}

input UpdateCartItemInput {
  cartItemId: ID!
  quantity: Int!   # set to 0 to remove
}

input ApplyCouponInput {
  cartId: ID!
  couponCode: String!
}

input CheckoutInput {
  cartId: ID!
  shippingAddressId: ID!
  billingAddressId: ID!
  """
  Client must confirm awareness of cart issues (out of stock, price changes)
  by setting this to true if Cart.hasIssues is true.
  """
  acknowledgeIssues: Boolean
}

input CreateAddressInput {
  type: AddressType!
  firstName: String!
  lastName: String!
  line1: String!
  line2: String
  city: String!
  stateOrProvince: String!
  postalCode: String!
  country: String!
  isDefault: Boolean
}

input UpdateUserProfileInput {
  firstName: String
  lastName: String
  email: String
}

# ─── Payload Types ───────────────────────────────────────────────────────
type AddCartItemPayload {
  cart: Cart
  cartItem: CartItem
  errors: [UserError!]!
}

type UpdateCartItemPayload {
  cart: Cart
  errors: [UserError!]!
}

type ApplyCouponPayload {
  cart: Cart
  errors: [UserError!]!
}

type CheckoutPayload {
  order: Order
  errors: [UserError!]!
}

type CreateAddressPayload {
  address: Address
  errors: [UserError!]!
}

type UpdateUserProfilePayload {
  user: User
  errors: [UserError!]!
}

type CancelOrderPayload {
  order: Order
  errors: [UserError!]!
}

# ─── Viewer Type (authenticated context) ─────────────────────────────────
"""
The authenticated user's session context. 
Use viewer-scoped queries instead of passing userId arguments.
"""
type Viewer {
  user: User!
  cart: Cart!
}

# ─── Query Root ──────────────────────────────────────────────────────────
type Query {
  """Fetch any entity by its global Node ID."""
  node(id: ID!): Node

  """The authenticated user's session. Returns null for unauthenticated requests."""
  viewer: Viewer

  product(id: ID!): Product
  productBySlug(slug: String!): Product

  products(
    filter: ProductFilterInput
    orderBy: ProductOrderByInput
    first: Int = 20
    after: String
  ): ProductConnection!

  category(id: ID!): Category
  categoryBySlug(slug: String!): Category

  """Top-level categories only. Use Category.childCategories for subtrees."""
  rootCategories: [Category!]!

  """
  Look up a cart by its opaque token. Used for anonymous/guest carts.
  Authenticated users should use viewer { cart } instead.
  """
  cart(token: String!): Cart
}

# ─── Mutation Root ────────────────────────────────────────────────────────
type Mutation {
  addCartItem(input: AddCartItemInput!): AddCartItemPayload!
  updateCartItem(input: UpdateCartItemInput!): UpdateCartItemPayload!
  applyCoupon(input: ApplyCouponInput!): ApplyCouponPayload!
  checkout(input: CheckoutInput!): CheckoutPayload!
  cancelOrder(orderId: ID!): CancelOrderPayload!

  createAddress(input: CreateAddressInput!): CreateAddressPayload!
  updateUserProfile(input: UpdateUserProfileInput!): UpdateUserProfilePayload!
}
Relationship Map

| From | Field | To | Cardinality | Paginated |

|------|-------|----|-------------|-----------|

| User | addresses | Address | 1→many | No (bounded by address book size ~10) |

| User | orders | OrderConnection | 1→many | Yes (cursor) |

| Viewer | cart | Cart | 1→1 | No |

| Product | variants | [ProductVariant] | 1→many | No (bounded ~50) |

| Product | categories | [Category] | many→many | No (bounded ~5 per product) |

| Category | products | ProductConnection | many→many | Yes (cursor) |

| Category | childCategories | [Category] | 1→many | No (depth argument limits scope) |

| Cart | items | [CartItem] | 1→many | No (bounded by cart max ~100 items) |

| CartItem | productVariant | ProductVariant | many→1 | No |

| Order | lineItems | [OrderLineItem] | 1→many | No (bounded at checkout time) |

| OrderLineItem | productVariant | ProductVariant | many→1 | No (nullable -- variant may be deleted) |

Validation Results

| Check | Status | Notes |

|-------|--------|-------|

| Naming conventions | ✅ | All types PascalCase, fields camelCase, enums SCREAMING_SNAKE_CASE |

| All mutations have payload types | ✅ | All 7 mutations return XxxPayload with errors: [UserError!]! |

| All large collections paginated | ✅ | products, category.products, user.orders use cursor connections |

| Bounded collections not paginated | ✅ | cart.items, order.lineItems, product.variants are bounded by domain |

| N+1 risks documented | ⚠️ | CartItem.productVariant, OrderLineItem.productVariant, Product.categories all require DataLoader batching in resolvers |

| Node interface coverage | ✅ | User, Product, ProductVariant, Cart, CartItem, Order, OrderLineItem, Address, Category all implement Node |

| Breaking change baseline | ✅ | Initial schema -- establish as v0 baseline in schema registry before first deploy |

| Description coverage | ✅ | All types described; non-obvious fields have inline documentation |

Evolution Notes

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.