Given the following GraphQL schema snippet:
type Book {
title: String
author: Author
}
type Author {
name: String
age: Int
}And the query:
{
book {
title
author {
name
}
}
}What fields will be included in the response?
Look carefully at which fields are requested inside author.
The query asks for title and inside author only name. So the response includes those fields only.
Identify the query that will cause a syntax error when executed.
Remember that nested fields must be enclosed in braces.
Option A tries to select author and name at the same level without nesting name inside author. This is invalid syntax.
You want to fetch only the titles of all books. Which query is best to minimize data transfer?
Request only the fields you need.
Option D requests only title for each book, minimizing data sent. Other options request extra fields unnecessarily.
Consider a GraphQL type User with fields id, name, and email. If you query only id and name, what will the server return for email?
GraphQL only returns fields explicitly requested.
If a field is not requested in the query, the server does not include it in the response at all.
Given the schema:
type Query {
user(id: ID!): User
}
type User {
id: ID
name: String
friends: [User]
}And the query:
{
user(id: "1") {
id
name
friends {
id
name
friends {
id
}
}
}
}The server returns an error about exceeding query depth. Why?
Think about how deep the query goes into nested fields.
The query requests friends of friends of friends, which can cause very deep recursion. Many GraphQL servers limit query depth to prevent abuse.