SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

System design
API ContractsRESTGraphQLRPCChoosing an API contract
API design

GraphQL

Trace a typed query from selected fields to the backend reads that resolve them.

Lesson goal

Write a profile query, read its response against the schema, and count the backend work behind a nested field.

Before you begin — REST.

A profile screen needs a user’s name and a list of recent posts with comment counts. If the existing HTTP API exposes separate user, post, and comment resources, the screen might fetch fields it never displays and make extra requests to fill in the missing ones. GraphQL lets a client select related fields from a typed schema in one operation.

The example REST client requests users, posts, and comments separately. The GraphQL client selects related fields through one endpoint.

Read the schema as a contract

The Schema Definition Language (SDL) describes the fields clients may request, their arguments, and their return types. In this excerpt, start at Query.user, then follow its return type to see which profile fields are available.

type Query {
  user(id: ID!): User
}

type User {
  id: ID!
  name: String!
  posts(limit: Int = 10): [Post!]!
}

type Post {
  id: ID!
  title: String!
  commentsCount: Int!
}

Here user(id: ID!): User requires an ID but allows the lookup to return null. A successful posts field returns a list whose items are non-null posts. An empty list [] is valid. If a resolver returns null for a field declared non-null, the error propagates to the nearest nullable parent.

Select fields for the screen

Keep the operation stable and supply changing values through variables. Read the operation, then the values and the matching result.

query GetProfile($id: ID!, $limit: Int!) {
  user(id: $id) {
    name
    posts(limit: $limit) {
      title
      commentsCount
    }
  }
}

The client supplied an ID to locate the user. It selected only the name, post titles, and counts for display, so the ID itself is absent from the response. A different screen could request the ID using the same schema.

Count the reads behind a query

A resolver produces the value of a field. Some fields are already present on a parent object. Others require a database or service call. The client’s single operation can therefore trigger several separate reads. GraphQL field execution

Suppose the server looks up the user once, loads the list of three posts once, and counts comments separately for each post. That costs five reads. Within the posts portion, the one list read followed by one read per post is called the N+1 problem. If the data source can return counts for all three post IDs together, the same response costs three reads in this example. The improvement comes from how the server loads the data.

One GraphQL query takes five reads with separate comment-count lookups. Grouping the three counts into one lookup reduces the total to three reads.

A server can also bound a list’s length or limit query cost to keep one operation from requesting too much work. A single endpoint that receives many operations and variables needs cache keys that distinguish their results.

Read, change, or follow data over time

OperationWhat it expresses
QueryRead selected fields. Independent fields may resolve concurrently.
MutationChange state and select fields from the result. Top-level mutation fields in one operation execute serially.
SubscriptionReceive a stream of results as events occur.

For example, a schema could define createPost(input: CreatePostInput!): Post! on its Mutation type. The caller supplies a title and content and can select id and title from the created post in the same operation.

Many servers accept a JSON request at POST /graphql. Some also accept read-only queries over GET. When a valid operation executes, the HTTP response can be successful even if a field failed. Inspect both data and errors to see what the screen can use. A failure before execution, such as an invalid HTTP request, can also produce an HTTP error. GraphQL over HTTP

Check your understanding

The screen now requests ten posts. Under the unbatched design above, how many reads happen? Which change keeps the operation’s result shape intact while reducing that count?

Compare your reasoning

If ten posts are returned, the server makes twelve reads. One loads the user, one loads the post list, and ten load the counts. Loading all ten counts in one grouped read reduces this to three. The client can keep the same selection.

Key Takeaways

  • A schema defines the fields, arguments, relationships, and nullability a client can depend on.
  • A selection determines which fields the response contains. Variables supply values without rewriting the operation.
  • Resolvers determine the data-access work behind the selection. Count backend reads as well as client requests.

How is this lesson?

Previous lessonRESTNext lessonRPC
Edit on GitHubReport on GitHub
On this page
%

On this page

Read the schema as a contractSelect fields for the screenCount the reads behind a queryRead, change, or follow data over timeCheck your understandingKey Takeaways