📖 Scenario: You are building a simple GraphQL API for a library system. Each author can write many books, but each book has only one author.
🎯 Goal: Create a GraphQL schema that models the one-to-many relationship between authors and their books. You will define types for Author and Book, set up the fields to connect them, and write a query to fetch authors with their books.
📋 What You'll Learn
Define a GraphQL type Author with fields id (ID!), name (String!), and books (list of Book)
Define a GraphQL type Book with fields id (ID!), title (String!), and author (Author)
Create a query type with a field authors that returns a list of Author
Set up the one-to-many relationship so that each author can have many books, and each book belongs to one author
💡 Why This Matters
🌍 Real World
Modeling one-to-many relationships is common in APIs for libraries, stores, blogs, and many other systems where one entity relates to many others.
💼 Career
Understanding how to design GraphQL schemas with relationships is essential for backend developers building modern APIs.
Progress0 / 4 steps
1
Define the Author and Book types
Create a GraphQL type called Author with fields id of type ID! and name of type String!. Also create a GraphQL type called Book with fields id of type ID! and title of type String!.
GraphQL
Hint
Start by defining the two types with their basic fields. Don't add relationships yet.
2
Add the one-to-many relationship fields
Add a field called books to the Author type that returns a list of Book (use [Book]). Add a field called author to the Book type that returns an Author.
GraphQL
Hint
Use square brackets [] to indicate a list of books for the author.
3
Create the Query type with authors field
Define a Query type with a field called authors that returns a list of Author (use [Author]).
GraphQL
Hint
The Query type is the entry point for fetching data. Add the authors field that returns a list of Author.
4
Complete the schema with relationships
Ensure the full schema includes the Author, Book, and Query types with the one-to-many relationship fields books in Author and author in Book. The authors query should return a list of authors.
GraphQL
Hint
Make sure all parts are included exactly as before to complete the schema.
Practice
(1/5)
1. In GraphQL, what does a one-to-many relationship mean? One-to-many connects one item to many related items. Which statement is true?
easy
A. Many parents share one child.
B. Each child has multiple parents.
C. One parent has many children linked to it.
D. Parents and children are unrelated.
Solution
Step 1: Understand the meaning of one-to-many
One-to-many means one item (parent) connects to many items (children).
Step 2: Match the description to options
One parent has many children linked to it. correctly states one parent has many children, matching the definition.
Final Answer:
One parent has many children linked to it. -> Option C
Quick Check:
One-to-many = One parent, many children [OK]
Hint: Remember: one parent, many children in one-to-many [OK]
Common Mistakes:
Confusing one-to-many with many-to-one
Thinking children have multiple parents
Assuming parents and children are unrelated
2. Which of the following is the correct GraphQL schema snippet to represent a one-to-many relationship between Author and Book?
easy
A. type Author { id: ID! name: String! books: [Book!]! } type Book { id: ID! title: String! authorId: ID! }
B. type Author { id: ID! name: String! book: Book } type Book { id: ID! title: String! author: Author }
C. type Author { id: ID! name: String! books: Book } type Book { id: ID! title: String! authorId: ID! }
D. type Author { id: ID! name: String! books: Book! } type Book { id: ID! title: String! authorId: ID! }
Solution
Step 1: Identify list type for many children
In GraphQL, a one-to-many uses a list type like [Book!]! for multiple books.
Step 2: Check each option's books field
type Author { id: ID! name: String! books: [Book!]! } type Book { id: ID! title: String! authorId: ID! } uses books: [Book!]! correctly; others use single Book or wrong types.
Final Answer:
type Author { id: ID! name: String! books: [Book!]! } type Book { id: ID! title: String! authorId: ID! } -> Option A
Quick Check:
List type for many children = [Type!]! [OK]
Hint: Use square brackets [] for lists in GraphQL schema [OK]
Common Mistakes:
Using single type instead of list for many children
Missing non-null ! on list or items
Confusing field names or types
3. Given this GraphQL query and schema, what will be the output?
type Author { id: ID! name: String! books: [Book!]! } type Book { id: ID! title: String! authorId: ID! }
Query:
{ author(id: "1") { name books { title } } }
Assuming author 1 has two books titled "Book A" and "Book B".
medium
A. {"author":null}
B. {"author":{"name":"Author One","books":{"title":"Book A"}}}
C. {"author":{"name":"Author One","books":null}}
D. {"author":{"name":"Author One","books":[{"title":"Book A"},{"title":"Book B"}]}}
Solution
Step 1: Understand the schema and query
The author has a list of books; query requests author name and all book titles.
Step 2: Match expected output
Author 1 has two books, so books field returns a list with two titles.
Final Answer:
JSON with author name and books list of two titles -> Option D
Quick Check:
One author, many books = list output [OK]
Hint: Lists in schema return arrays in query results [OK]
Common Mistakes:
Expecting single object instead of list for books
Assuming books field is null if empty
Confusing null author with empty books
4. This GraphQL schema snippet has an error for one-to-many relationship. What is wrong?
type User { id: ID! name: String! posts: Post } type Post { id: ID! content: String! userId: ID! }
medium
A. The posts field should be a list type [Post!]! instead of Post.
B. The userId field should be removed from Post.
C. The name field in User should be optional.
D. The id fields should be of type String, not ID.
Solution
Step 1: Check the posts field type for one-to-many
One user can have many posts, so posts must be a list, not a single Post.
Step 2: Verify other fields
userId in Post is correct to link back; name optionality and id type are unrelated.
Final Answer:
posts field must be a list [Post!]! -> Option A
Quick Check:
One-to-many needs list type for many children [OK]
Hint: Use brackets [] for multiple related items in schema [OK]
Common Mistakes:
Using single object instead of list for many children
Removing foreign key fields mistakenly
Changing unrelated field types
5. You want to design a GraphQL schema for a blog where each Category has many Posts, and each Post belongs to one Category. Which schema design correctly models this one-to-many relationship and allows querying all posts in a category?
hard
A. type Category { id: ID! name: String! post: Post } type Post { id: ID! title: String! category: Category } Query { posts: [Post!]! }
B. type Category { id: ID! name: String! posts: [Post!]! } type Post { id: ID! title: String! categoryId: ID! } Query { category(id: ID!): Category }
C. type Category { id: ID! name: String! posts: Post } type Post { id: ID! title: String! categoryId: ID! } Query { category(id: ID!): Category }
D. type Category { id: ID! name: String! posts: [Post] } type Post { id: ID! title: String! category: Category! } Query { category(id: ID!): Category }
Solution
Step 1: Model one-to-many with list in Category
Category should have posts as a list [Post!]! to represent many posts.
Step 2: Use categoryId in Post to link back
Post has categoryId to link to its single category, enabling queries from both sides.
Step 3: Check Query type for fetching category
Query allows fetching category by id, then posts can be queried from category.
Final Answer:
type Category { id: ID! name: String! posts: [Post!]! } type Post { id: ID! title: String! categoryId: ID! } Query { category(id: ID!): Category } -> Option B
Quick Check:
List in parent + foreign key in child = one-to-many [OK]
Hint: Parent has list, child has foreign key ID for one-to-many [OK]
Common Mistakes:
Using single Post instead of list in Category
Missing foreign key in Post
Incorrect Query type for fetching data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.