Skip to content

Lesson 1 of 9 · 24m

Schema-First Design: Modeling the Domain, Not the Database

Why the schema is a product contract rather than an ORM dump, how to name and shape types, and where teams first paint themselves into corners.

The first production GraphQL schema I inherited looked exactly like the Postgres schema: User, UserProfile, UserSettings, UserEmail, all exposing whatever columns happened to exist. Clients had to know database trivia to render a profile page. Within a year every mobile release depended on table names we could not rename. That is the central lesson: a GraphQL schema is not your data model. It is the vocabulary your product teams use to ask questions.

Start with client use cases

Write down the real questions before writing SDL:

  • "Show this member's display name, avatar, and current plan."
  • "List the invoices attached to this workspace."
  • "Can this actor invite another admin?"

Then model types around those questions:

type Member {
  id: ID!
  displayName: String!
  avatarUrl(size: Int = 128): String
  plan: Plan!
  canInviteAdmin: Boolean!
}

type Workspace {
  id: ID!
  name: String!
  invoices(first: Int, after: String): InvoiceConnection!
}

Notice what is absent: created_at, foreign keys, join tables, serialization artifacts. Those belong behind the graph.

Design mutations as commands

Good mutations read like business operations, not CRUD endpoints:

type Mutation {
  inviteWorkspaceMember(input: InviteWorkspaceMemberInput!): InviteWorkspaceMemberPayload!
}

input InviteWorkspaceMemberInput {
  workspaceId: ID!
  email: String!
  role: WorkspaceRole!
}

type InviteWorkspaceMemberPayload {
  invitation: WorkspaceInvitation
  errors: [UserError!]!
}

The input object gives you room to evolve arguments. The payload gives you room to return domain errors without throwing away partial context. A boolean-only mutation response is a dead end.

Use IDs and references deliberately

Expose opaque IDs. Do not concatenate database IDs into client-visible strings unless you control the codec forever; Relay-style global IDs can help caching, but they also leak implementation shape when teams forget to version them. For references, return objects rather than bare IDs when clients almost always need the object: workspace.owner should be a Member, not ownerId plus a second query.

Prefer precise types over convenient strings

status: String is schema debt. Use enums for closed sets and interfaces/unions for real polymorphism:

enum InvoiceStatus {
  DRAFT
  OPEN
  PAID
  VOID
  UNCOLLECTIBLE
}

union SearchResult = Member | Workspace | Invoice

A union forces clients to handle variants explicitly. That is a feature, not friction.

Field notes

  • If a field exists only because the database has a column, delete it before anyone depends on it.
  • Name types for the consumer's domain (Member, Invoice) rather than storage details (UserRow, BillingTable).
  • Avoid generic data: JSON fields except as a temporary escape hatch with an expiry date.
  • Schema review is API design review; do not treat SDL diffs as implementation detail.
  • Small, boring schemas age better than clever ones.

Was this lesson helpful?

Sign in to rate