← Lessons

quiz vs the machine

Gold1500

System Design

GraphQL Schema Design

Modeling a typed graph clients can query exactly, fetching only the fields they need.

5 min read · core · beat Gold to climb

A different shape of API

GraphQL exposes a single endpoint backed by a typed schema. Instead of many fixed responses, the client writes a query naming exactly the fields it wants, and the server returns just those, eliminating the over and under fetching common in REST.

Schema building blocks

  • Types define objects with named, typed fields.
  • Queries are the read entry points into the graph.
  • Mutations are the write entry points.
  • Resolvers are functions that fetch the value for each field.

Designing the graph well

  • Model relationships as edges between types so a query can traverse from user to orders to items in one round trip.
  • Mark fields non null only when truly guaranteed, since a null in a non null field fails the whole branch.
  • Think in terms of the client's needs, not your database tables.

The strength is precision and a strongly typed contract. The cost is that arbitrary client queries can be expensive, which you control with depth and complexity limits.

Key idea

GraphQL exposes a typed graph where clients request exactly the fields they need, traded against query cost you must bound.

Check yourself

Answer to earn rating on the learn ladder.

1. What problem does GraphQL field selection solve compared to typical REST?

2. What fetches the value for a single GraphQL field?

3. Why bound query depth and complexity in GraphQL?