Given the schema below, what will be the result of the query?
type Query {
book(id: ID!): Book
}
type Book {
id: ID!
title: String!
author: Author!
}
type Author {
id: ID!
name: String!
}Query:
{
book(id: "1") {
title
author {
name
}
}
}Assuming the data has a book with id "1", title "GraphQL Basics", and author name "Alice".
Remember that nested objects must be returned as objects, not strings.
The query requests the book's title and the author's name as a nested object. The correct output nests the author as an object with a name field.
You want to define a field books in your Query type that returns a list of Book objects. Which schema definition is correct?
GraphQL uses square brackets to denote lists.
In GraphQL schema language, lists are represented by square brackets around the type, e.g., [Book].
Which option contains a syntax error in this GraphQL schema snippet?
type Query {
user(id: ID!): User
}
type User {
id: ID!
name: String!
email: String!
}Check the syntax for field declarations in GraphQL schema.
Option A is missing a colon between the field name and its type, which is required in GraphQL schema.
You have a query fetching user with all fields including posts and each post's comments. You only need the user's name and the titles of their posts. Which query is optimized?
Only request the fields you need to reduce data transfer.
Option A requests only the user's name and the titles of posts, avoiding unnecessary fields and nested comments.
Given the schema:
type Query {
book(id: ID!): Book
}
type Book {
id: ID!
title: String!
author: Author!
}
type Author {
id: ID!
name: String!
}Why does this query cause an error?
{
book(id: "1") {
title
author
}
}In GraphQL, object fields require specifying subfields.
The 'author' field is an object type, so you must specify which fields of 'author' you want. Omitting subfields causes an error.