Skip to the page

GraphQL in production · mid

Schema design and changing a schema safely

A schema is a contract with every client already installed. Design nullability, money and pagination on purpose, and change fields in ways old apps survive: add, never remove or tighten.

The notes

What you must know

  • Fields are nullable by default; ! makes a field non-null. If a non-null field fails, the null moves up to the nearest nullable parent. Make a field non-null only when it can never be missing.
  • Money is never Float. Use an integer amount in the smallest unit (paise) plus a currency, as a Money type or a custom scalar.
  • Pagination with Relay-style connections: orders(first: Int = 20, after: String) returns edges { cursor node } and pageInfo { hasNextPage endCursor }. Cursor paging stays correct while new rows arrive. Cap first.
  • Mutations take one input type and return a result type or a union (for example PlaceOrderSuccess or PaymentDeclined), so expected failures are typed data and not generic errors.
  • Safe changes: add types, add fields, add optional arguments. Breaking changes: remove or rename a field, make an output field nullable, make an argument required, change a type. Adding a value to an enum can also break old clients that switch on it.
  • Old apps generate models from the old schema. A generated client can treat null in a field that was non-null as a parse error, and the screen crashes. Making Address! into Address breaks them.
  • Safe way to change: keep the old field, add a new field or union for new apps, mark the old one @deprecated(reason: ...), watch usage per field and per client version, and remove it only when usage is zero.

The red pen

Common traps: mistake → fix

  • Mistake: Making a field nullable is a safe change because it is more relaxed

    Fix: For output fields it is breaking: old generated clients expect non-null.

  • Mistake: GraphQL needs no versioning, so I can change anything

    Fix: Clients cannot be forced to update, so you must evolve by adding and deprecating.

  • Mistake: Float is fine for prices if I round it

    Fix: Float has rounding errors. Use integer paise or a decimal type.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why is Float wrong for money in a schema?
  2. What does the exclamation mark mean, and what happens when a non-null field returns null?
  3. Why do connections use cursors and not page numbers?
  4. Which schema changes are breaking? Why is making a field nullable one of them?
  5. An Android release crashed after a backend change to one field. How do you fix it now and prevent it?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview