Given a GraphQL schema where Author has a list of Book objects and each Book has a reference back to its Author, what will be the output of this query?
query {
authors {
name
books {
title
author {
name
}
}
}
}type Author {
id: ID!
name: String!
books: [Book!]!
}
type Book {
id: ID!
title: String!
author: Author!
}
# Sample data:
# Author: {id: "1", name: "Alice", books: [Book1, Book2]}
# Book1: {id: "101", title: "GraphQL Basics", author: Author}
# Book2: {id: "102", title: "Advanced GraphQL", author: Author}Think about how the bidirectional link allows nested querying of author inside each book.
The query fetches authors, their books, and for each book, the author again. Because of the bidirectional relationship, the author inside each book is populated with the author's name.
Which statement best describes a bidirectional relationship in GraphQL schemas?
Think about how you can traverse data in both directions in a schema.
Bidirectional relationships mean two types have fields referencing each other, enabling queries to go from one type to the other and back.
Which option contains a syntax error in defining a bidirectional relationship between Author and Book?
type Author {
id: ID!
name: String!
books: [Book!]!
}
type Book {
id: ID!
title: String!
author: Author!
}Check the type of the books field in Author. Should it be a list or a single object?
The books field should be a list of Book objects, not a single Book. Option D incorrectly defines it as a single Book without list brackets.
You have a bidirectional relationship between Author and Book. You want to fetch authors and their books but avoid redundant nested author data inside each book to reduce query size. Which query modification achieves this?
Think about how to avoid fetching nested data you don't need.
Removing the nested author field inside books prevents fetching redundant author data, reducing query size and improving performance.
Consider this GraphQL query on a bidirectional schema:
query {
authors {
name
books {
title
author {
books {
title
}
}
}
}
}What problem will this query cause when executed?
Think about how the query keeps nesting author and books inside each other.
The query nests author inside books and then books inside author again, causing infinite recursion. This leads to server errors or timeouts.