Graphql schema design

From Intense-Visions/harness-engineering · View interactive version

Graphql schema design is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Design expressive, evolvable GraphQL schemas with clear type hierarchies and strong nullability contracts

How to use this skill

Skill file

GraphQL Schema Design

Design expressive, evolvable GraphQL schemas with clear type hierarchies and strong nullability contracts

When to Use

Instructions

  1. Start with the domain, not the UI. Model your schema around business entities (Order, Product, User), not around specific screens or components. A well-modeled domain schema serves multiple clients without per-client hacks.
  1. Use non-null by default. Mark fields as String! (non-null) unless the field genuinely can be absent. Non-null fields simplify client code by eliminating null checks. Reserve nullable fields for truly optional data (e.g., middleName: String).
  1. Prefer specific types over generic ones. Use custom scalars (DateTime, URL, EmailAddress) instead of plain String for fields with validation semantics. Use enums for closed sets of values.
scalar DateTime
scalar URL

enum OrderStatus {
  PENDING
  CONFIRMED
  SHIPPED
  DELIVERED
  CANCELLED
}

type Order {
  id: ID!
  status: OrderStatus!
  createdAt: DateTime!
  trackingUrl: URL
}
  1. Use interfaces for shared field contracts. When multiple types share fields and clients query them polymorphically, define an interface. Use unions when types share no fields but appear in the same list.
interface Node {
  id: ID!
}

interface Timestamped {
  createdAt: DateTime!
  updatedAt: DateTime!
}

type User implements Node & Timestamped {
  id: ID!
  createdAt: DateTime!
  updatedAt: DateTime!
  name: String!
}

union SearchResult = User | Product | Order
  1. Design mutations around actions, not CRUD. Name mutations after the business action: cancelOrder, approveRefund, inviteTeamMember — not updateOrder(status: CANCELLED). Each mutation should have a dedicated input type and a dedicated payload type.
input CancelOrderInput {
  orderId: ID!
  reason: String!
}

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

type Mutation {
  cancelOrder(input: CancelOrderInput!): CancelOrderPayload!
}
  1. Always include a UserError type in mutation payloads. This separates expected domain errors (validation failures, business rule violations) from unexpected system errors (which use GraphQL's top-level errors array).
type UserError {
  field: [String!]
  message: String!
  code: ErrorCode!
}
  1. Use the Relay connection spec for paginated lists. Even if you do not use Relay on the client, the Connection/Edge/PageInfo pattern is well-understood, cursor-based, and forward-compatible.
  1. Version through evolution, not URL paths. Add new fields freely. Deprecate old fields with @deprecated(reason: "Use newField instead"). Never remove fields without a deprecation period and client migration.
  1. Keep the schema file as the source of truth. Whether you use schema-first or code-first, ensure there is one canonical .graphql file (or set of files) that documents every type. Generate code from the schema, not the other way around.
  1. Document with descriptions. Add descriptions above types and fields — they appear in GraphiQL/Apollo Studio and serve as living API docs.
"""
A customer order containing one or more line items.
"""
type Order {
  """
  Unique identifier for the order.
  """
  id: ID!
}

Details

Naming conventions: Types are PascalCase, fields are camelCase, enums are SCREAMING_SNAKE_CASE. Input types end with Input, payload types end with Payload.

Nullability trade-offs: Non-null fields are safer for clients but less forgiving for servers — if a non-null resolver throws, the error bubbles up to the nearest nullable parent, potentially nullifying an entire object. Place nullable "firewalls" at strategic points (e.g., nullable list items) to limit blast radius.

Schema stitching vs. federation: For monolithic APIs, a single schema file works. For microservices, prefer Apollo Federation where each service owns its slice of the graph and extends shared types with @key.

Anti-patterns to avoid:

Source

https://graphql.org/learn/schema/

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

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.