Schema Design Patterns — GraphQL

A schema is a contract with clients that aren't updated together. Types in PascalCase, fields in camelCase, enum values in SCREAMING_SNAKE_CASE, mutations…

A restaurant menu that has been printed and handed to regulars can't be changed on a whim. Rename a dish and the kitchen no longer recognises old orders. It's safer to add the new dish, mark the old one "being replaced", and remove it once nobody orders it any more.

A GraphQL schema is the same. It's a contract between the server and all of its clients, and the Bookshop has three: the shop website, the cashier app and the admin dashboard. The three can't be updated at the same moment. Good schema design lets the schema grow without breaking the clients that already exist.

Naming

Part Style Example in the Bookshop
Type, input, enum, union PascalCase, a noun Book, CoverUpload, StockStatus
Field and argument camelCase coverUrl, priceCents, bookId
Enum value SCREAMING_SNAKE_CASE IN_STOCK, SOLD_OUT
Mutation the verb first, then the noun createBook, updateStock, attachCover
Input and payload mutation name + Input / Payload RestockBookInput, RestockBookPayload

A mutation that starts with a verb says straight away what happens. A type name carries no verb (Book, not GetBookResponse), because the same type serves many operations.

An input type and a payload type per mutation

updateStock(id: ID!, stock: Int!): Book is easy to write, but hard to grow. The shape many large APIs use:

type Mutation {
  restockBook(input: RestockBookInput!): RestockBookPayload!
}

input RestockBookInput {
  bookId: ID!
  stock: Int!
}

type RestockBookPayload {
  book: Book
  userErrors: [UserError!]!
}

type UserError {
  field: [String!]
  message: String!
}

Nullability: where ! is safe

A ! is a promise: the value is always there. If that promise fails even once, its null climbs to the nearest place that may be null (the "Schema & Type System" lesson).

Deprecation, not a new version

GraphQL needs no /v2. You add the new field, mark the old one @deprecated with a reason, and remove it once nobody uses it any more:

type Book {
  price: Int! @deprecated(reason: "Use priceCents.")
  priceCents: Int!
}

Old clients keep working: { books { title price status } } is still answered in full. Tools such as GraphiQL and linters warn about it; graphql-js's NoDeprecatedCustomRule writes it like this: The field Book.price is deprecated. Use priceCents.

Now guess: the team renames price to priceCents outright, with no deprecation. The shop website, not yet updated, still sends { books { title price status } }. What comes back: price as null, or something worse?

Renaming outright is very different. Once price becomes priceCents, the shop website's query fails validation: Cannot query field "price" on type "Book". The whole operation is refused, so even the title and status don't arrive. The same happens if updateStock(id, stock) is changed to updateStock(input:) in one go: the cashier app gets Unknown argument "id" on field "Mutation.updateStock". The way to do it: add restockBook(input:) beside it, deprecate updateStock, then watch each field's usage drop to zero before removing it.

Enums, not strings

status: StockStatus! beats status: String!. An enum lists the possible values in the schema, and a wrong value is refused before any resolver runs (Value "EMPTY" does not exist in "StockStatus" enum.). Turning the enum into a String later breaks clients that write books(status: SOLD_OUT): String cannot represent a non string value: SOLD_OUT. Adding a new enum value needs care too, because a client with a switch may meet a value it doesn't know yet.

The Node interface and global IDs

Relay, the GraphQL client from Meta, uses the global object identification pattern: every object can be fetched again through one node field.

interface Node {
  id: ID!
}

type Book implements Node {
  id: ID!
  title: String!
}

type Query {
  node(id: ID!): Node
}

The mini GraphQL engine in the Lab doesn't run interfaces yet, so this part is only written as SDL.

Schema-first or code-first

⚠️ Schema decisions that break clients later

TL;DR: A schema is a contract with clients that aren't updated together. Types in PascalCase, fields in camelCase, enum values in SCREAMING_SNAKE_CASE, mutations starting with a verb. Each mutation uses its own input type (groups arguments, can be reused, safe to extend) and a payload type (say book plus userErrors). ! only for values that are certainly always there; in large schemas most fields are nullable. Don't make a new version: add the field, put @deprecated(reason:) on the old one, remove it once its usage is zero; renaming outright gets old queries refused (Cannot query field "price" on type "Book".). Use enums, not strings. A Node interface with global IDs (btoa("Book:1")) lets any object be fetched through node(id:). A union picks one of several object types, an interface shares fields. Schema-first writes the SDL first; code-first generates the SDL from code, so it is always in sync with the implementation.

⚡ An input and a payload for each mutation

Write:

type Mutation {
  restockBook(input: RestockBookInput!): RestockBookPayload!
}

input RestockBookInput {
  bookId: ID!
  stock: Int!
}

Meaning: An input type groups related arguments into one object, and it's easy to reuse. The client sends just one $input variable, and an optional field can be added later without breaking old clients.

⚡ Deprecation, not a new version

Write:

type Book {
  price: Int! @deprecated(reason: "Use priceCents.")
  priceCents: Int!
}
Guess first: the team renames price to priceCents outright, with no deprecation. The shop website still asks for price. What happens?

Its whole query fails validation: Cannot query field "price" on type "Book". Not even the title and status arrive.

Meaning: Add the new field, mark the old one @deprecated, then remove it once nobody uses it any more.

⚡ An exclamation mark is a promise

Meaning: Use ! for values that really are always there, like id and title. Fields an access rule may refuse, or that come from another service, are better nullable, so one failure removes one field, not the whole response.

What you will learn