Schema design principles

Intermediate · 25 min

Design the schema around what clients need, not your tables: model the domain, name actions for intent, be specific, and keep one graph.

Before thisObject types and fieldsInput typesDesigning mutation inputs and payloads

Apollo GraphQL · 25 min

Schema design principles

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Apollo GraphQLReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A schema is a product for client developers. Design it from what screens and clients need, not from your table layout or your service boundaries.
  • Model the business domain with real types: an `author` field returning an `Author`, not an `authorId: Int` the client must join itself.
  • Name mutations after intent, such as `publishPost`, and give each its own input and payload; avoid one `updatePost` that does everything.
  • Be specific: enums for fixed sets, custom scalars for dates, and `ID` for identifiers, so the schema teaches the client what is valid.
  • Document in the schema with descriptions and deprecations; they appear in introspection, so every tool shows them.
  • Keep one graph for all clients instead of a schema per screen, and add fields rather than versions.

The problem: the schema is the one thing everyone touches

A server’s internals can be rewritten behind the schema. The schema cannot be, because clients are built against it. Every field you publish is a promise, and a bad name or a leaked table detail is expensive to take back, as evolving a schema explains.

Good schema design is mostly deciding what to promise. These principles are habits that make the promises cheap to keep.

Start from the client, not the database

Here is a schema that mirrors storage:

type Query {
  getPosts: [PostTable]
  getPostById(post_id: Int): PostTable
}
type PostTable {
  post_id: Int
  author_id: Int
  title: String
  is_published: Int
  created_at: String
}
type Mutation {
  updatePost(post_id: Int, title: String, is_published: Int): PostTable
}

It works, and it is a trap. A client that wants the author’s name has to make a second call. is_published: Int makes the reader guess what 1 and 0 mean. PostTable tells the client there is a table. Every field is nullable by accident, and every argument is loose.

Here is the same capability designed for the people who use it:

"""A blog post. Posts start as drafts and become visible when published."""
type Post {
  id: ID!
  title: String!
  "Null while the post is a draft."
  publishedAt: DateTime
  author: Author!
}

type Query {
  post(id: ID!): Post
  posts(first: Int = 10, after: String): PostConnection!
}

input PublishPostInput { id: ID! }
type PublishPostPayload { post: Post  userErrors: [UserError!]! }

type Mutation {
  publishPost(input: PublishPostInput!): PublishPostPayload!
}

I built both with graphql-js 17.1.0 to be sure they are valid. What changed:

  • Domain names. Post, not PostTable; author is an Author, not an author_id. The resolver does the join, once, so no client has to.
  • Intent. publishPost says what happens. “Published” is a state with rules, and a state change deserves its own verb rather than a flag on a general update.
  • Typed, not stringly. publishedAt: DateTime replaces is_published: Int and created_at: String with one nullable timestamp that carries more information than a flag. A custom scalar for dates is covered in scalar types and custom scalars.
  • Payload and input shapes from designing mutation inputs and payloads.
  • Pagination shape on lists from the start, because a bare [Post] cannot be paginated later without a breaking change; see pagination.

Be specific

Every type is a chance to tell the client what is valid. A field that accepts any String and means “one of draft, published or archived” is an enum waiting to happen; an enum makes invalid values impossible to send, and shows up in autocomplete. Use ID for identifiers, not Int, so you can change the id scheme later and global IDs stay possible. Use custom scalars for dates and emails, and input objects instead of long argument lists.

Describe things in the schema

SDL descriptions are part of the schema. I added them to Post and to publishedAt, then queried introspection:

{ "__type": { "description": "A blog post. Posts start as drafts and become visible when published.",
  "fields": [ { "name": "id", "description": null },
              { "name": "publishedAt", "description": "Null while the post is a draft." } ] } }

That description is what GraphiQL shows on hover and what code generators copy into comments. A comment in a wiki does neither. Write a sentence for each type and for any field whose meaning is not obvious from its name. The value Null while the post is a draft answers the question a client would otherwise have to ask a person.

One graph

It is tempting to build a schema per client: one for the web app, one for mobile, one for the admin panel. Resist it. Clients that want less select less, which is the point of GraphQL. Separate schemas duplicate types and resolvers, and drift. One graph, with authorization on fields that need it, and clients picking what they need.

The graph is also a product, not a mirror of your service layout. When one team owns the schema across many services, federation is how the pieces compose, but clients still see one graph.

Evolve, don’t version

Prefer adding fields and deprecating old ones over cutting a v2. This works because clients ask only for the fields they use, so an additive change breaks no one. The principle shapes design decisions today: leave room. Give mutations payloads. Give lists a connection shape. Avoid booleans that may become states.

What the video covers

This is Michael Watson’s 25-minute talk from Apollo’s channel, “The Do’s and Don’ts for your schema and GraphQL operations.” The description says he works as a Customer Success Engineer, starts with do’s and don’ts for building a schema so clients can consume the graph more easily, then covers pitfalls in GraphQL operations, and shows Apollo tooling. It is from November 2019, so the Apollo tooling it demonstrates (Graph Manager) has since been replaced, and I could not read a transcript to say which specific rules it lists. The two schemas in this lesson are my own, not taken from it. Jeff Dolle’s 2026 talk in the links below is a more recent complement.

Where this fits

You have a set of principles; the first place to apply them is names, which are the hardest thing to change and the thing every client reads. Next: naming conventions.

Check yourself

  1. 01Why is mirroring database tables in the schema usually a mistake?

    Tables encode storage decisions: foreign keys, join tables, integer flags. Clients want the domain, such as `post.author.name`. A mirror forces clients to join and decode, and it ties the API to a table design you may want to change.

  2. 02What is wrong with `updatePost(post_id: Int, title: String, is_published: Int): PostTable`?

    It leaks storage (`PostTable`, `is_published: Int`), hides intent behind a flag, uses a bare Int for an id and returns no way to report failures. A `publishPost(input:)` returning a payload is clearer and safer to extend.

  3. 03Where should the explanation of a field live?

    In the schema as a description, since it ships with introspection to GraphiQL, code generators and docs; a separate document drifts.

  4. 04Should each client team get its own schema?

    Usually not. A single graph lets clients reuse types and avoids duplicated resolvers; shape differences are handled by what each client selects.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark