Relationship design patterns in GraphQL - Time & Space Complexity
When working with relationship design patterns in GraphQL, it's important to know how the time to get data changes as the data grows.
We want to understand how the number of related items affects the work done to fetch them.
Analyze the time complexity of the following GraphQL query fetching related data.
query {
users {
id
name
posts {
id
title
}
}
}
This query fetches a list of users and for each user, it fetches their posts.
Look for repeated actions in the query execution.
- Primary operation: For each user, fetching their posts is repeated.
- How many times: Once per user, so the number of users times the number of posts per user.
As the number of users and posts grows, the work grows too.
| Input Size (n users, m posts/user) | Approx. Operations |
|---|---|
| 10 users, 5 posts each | 10 + (10 x 5) = 60 |
| 100 users, 5 posts each | 100 + (100 x 5) = 600 |
| 1000 users, 5 posts each | 1000 + (1000 x 5) = 6000 |
Pattern observation: The total work grows roughly by multiplying the number of users by the number of posts per user.
Time Complexity: O(n x m)
This means the time to get all data grows with both how many users there are and how many posts each user has.
[X] Wrong: "Fetching posts for all users is just as fast as fetching users alone."
[OK] Correct: Because for each user, the system must also fetch their posts, so the work multiplies, not stays the same.
Understanding how nested data fetching grows helps you explain and design efficient queries in real projects.
What if we changed the query to fetch comments for each post as well? How would the time complexity change?
Practice
Solution
Step 1: Understand relationship types
One-to-many means one record links to many records, like one author to many books.Step 2: Match example to pattern
Since one author can have many books, this fits one-to-many.Final Answer:
One-to-many -> Option AQuick Check:
One author -> many books = One-to-many [OK]
- Confusing many-to-many with one-to-many
- Thinking one-to-one fits multiple items
- Mixing up direction of relationship
Student and Course types?Solution
Step 1: Identify many-to-many syntax
Many-to-many uses lists on both sides, so fields must be arrays like [Course!]! and [Student!]!.Step 2: Check options for correct list syntax
type Student { courses: [Course!]! } type Course { students: [Student!]! } uses lists with non-nullable items and non-nullable lists, which is correct GraphQL syntax.Final Answer:
type Student { courses: [Course!]! } type Course { students: [Student!]! } -> Option CQuick Check:
Lists on both sides = many-to-many syntax [OK]
- Using single objects instead of lists for many-to-many
- Missing non-nullable markers (!)
- Confusing one-to-one syntax with many-to-many
type Author {
id: ID!
books: [Book!]!
}
type Book {
id: ID!
title: String!
author: Author!
}What will the query
{ author(id: "1") { books { title } } } return if author 1 has two books titled "A" and "B"?Solution
Step 1: Understand schema and query
Author has a list of books. Query asks for author with id "1" and their books' titles.Step 2: Predict query result
Since author 1 has two books "A" and "B", the books field returns a list with both titles.Final Answer:
{"data":{"author":{"books":[{"title":"A"},{"title":"B"}]}}} -> Option BQuick Check:
List of books with titles A and B returned [OK]
- Expecting a single book object instead of a list
- Assuming null if multiple books exist
- Confusing error with missing field
type User {
id: ID!
posts: Post
}
type Post {
id: ID!
content: String!
}What is the main problem with this design if a user can have many posts?
Solution
Step 1: Analyze the 'posts' field type
The 'posts' field is declared as a single Post, but users can have many posts.Step 2: Identify correct type for multiple posts
To represent many posts, 'posts' should be a list like [Post!]! instead of a single Post.Final Answer:
The 'posts' field should be a list, not a single Post object. -> Option AQuick Check:
Multiple posts need list type, not single object [OK]
- Thinking single object can hold many items
- Ignoring list syntax for multiple relations
- Assuming missing fields cause this error
Solution
Step 1: Understand many-to-many self-relationship
Users can follow many users and be followed by many users, so both sides need lists.Step 2: Check schema for correct list usage
type User { followers: [User!]! following: [User!]! } // both lists represent many-to-many uses lists for both followers and following, correctly modeling many-to-many self-relationship.Final Answer:
type User { followers: [User!]! following: [User!]! } -> Option DQuick Check:
Lists on both sides model many-to-many self-relationship [OK]
- Using single User instead of list for followers/following
- Mixing list and single object types
- Forgetting non-nullable list syntax
Ready to prove it?
Pass the test and this pattern moves unknown
