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 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.
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.
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.
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.
| Operation | What it expresses |
|---|---|
| Query | Read selected fields. Independent fields may resolve concurrently. |
| Mutation | Change state and select fields from the result. Top-level mutation fields in one operation execute serially. |
| Subscription | Receive 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
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?
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.