Bidirectional relationships in GraphQL - Time & Space Complexity
When working with bidirectional relationships in GraphQL, it's important to understand how the time to fetch data grows as the related data grows.
We want to know how the number of operations changes when we query connected data in both directions.
Analyze the time complexity of the following GraphQL query fetching bidirectional relationships.
query {
authors {
id
name
books {
id
title
author {
id
name
}
}
}
}
This query fetches authors, their books, and for each book, the author again, showing a bidirectional link.
Look for repeated data fetching steps in the query.
- Primary operation: Loop over all authors, then for each author loop over their books, then for each book fetch the author again.
- How many times: The author list is traversed once, books are traversed for each author, and author info is fetched again for each book.
As the number of authors and books grows, the operations increase accordingly.
| Input Size (n authors, m books per author) | Approx. Operations |
|---|---|
| 10 authors, 5 books each | 10 + (10 * 5) + (10 * 5) = 10 + 50 + 50 = 110 |
| 100 authors, 5 books each | 100 + (100 * 5) + (100 * 5) = 100 + 500 + 500 = 1100 |
| 1000 authors, 5 books each | 1000 + (1000 * 5) + (1000 * 5) = 1000 + 5000 + 5000 = 11000 |
Pattern observation: The operations grow roughly in proportion to the number of authors times the number of books per author.
Time Complexity: O(n * m)
This means the time grows proportionally to the number of authors multiplied by the number of books each author has.
[X] Wrong: "Fetching the author inside each book doesn't add extra cost because it's the same author."
[OK] Correct: Even if it's the same author, the query repeats fetching author data for every book, so the operations add up with each book.
Understanding how bidirectional data fetching scales helps you design efficient queries and avoid slow responses in real apps.
What if we removed fetching the author inside each book? How would the time complexity change?
Practice
Solution
Step 1: Understand what bidirectional relationships do
They link two types so you can access related data from either type easily.Step 2: Compare options to this purpose
Only To connect two types so you can query related data from both sides describes this linking and querying from both sides.Final Answer:
To connect two types so you can query related data from both sides -> Option AQuick Check:
Bidirectional relationships = connect types both ways [OK]
- Confusing bidirectional with performance optimization
- Thinking it encrypts data
- Assuming it limits queries
Author and Book types in GraphQL SDL?Solution
Step 1: Check the relationship types
An author can have many books, sobooksshould be a list ofBook. Each book has one author, soauthoris a singleAuthor.Step 2: Match this to options
Only type Author { books: [Book] } type Book { author: Author } correctly uses[Book]for books andAuthorfor author.Final Answer:
type Author { books: [Book] } type Book { author: Author } -> Option DQuick Check:
List on many side, single on one side = type Author { books: [Book] } type Book { author: Author } [OK]
- Using single type where list is needed
- Swapping list and single types
- Using wrong type names
type User { posts: [Post] } type Post { author: User }What will be the result of this query?
{ user { posts { author { id } } } }Solution
Step 1: Understand the schema and query
User has posts, each post has an author. Query asks for user's posts and each post's author id.Step 2: Predict the query result
Since fields exist and are linked bidirectionally, the query returns posts with author ids nested properly.Final Answer:
{ "data": { "user": { "posts": [{ "author": { "id": "..." } }, ...] } } } -> Option AQuick Check:
Bidirectional query returns nested data correctly [OK]
- Thinking circular references cause runtime errors
- Assuming missing fields cause syntax errors
- Expecting null when data exists
type Category { products: [Product] } type Product { category: Category }But your query for
category { products { category { id } }} returns null for category inside products. What is the likely cause?Solution
Step 1: Analyze the schema and query
Schema defines bidirectional links. Query asks for category's products and each product's category id.Step 2: Identify why nested category is null
If nested category is null, likely the resolver forcategoryinProductis missing or incorrect.Final Answer:
Thecategoryfield inProductis not properly resolved or linked -> Option CQuick Check:
Missing resolver causes null nested fields [OK]
- Assuming GraphQL can't do nested bidirectional queries
- Thinking list types cause nulls
- Blaming query syntax without error
Employee and Manager where each employee has one manager, and each manager can have many employees. Which schema correctly models this with bidirectional fields?Solution
Step 1: Understand the relationship
Each employee has one manager (single), each manager has many employees (list).Step 2: Match schema fields to this
Employee'smanageris a singleManager. Manager'semployeesis a list ofEmployee.Final Answer:
type Employee { manager: Manager } type Manager { employees: [Employee] } -> Option BQuick Check:
One-to-many bidirectional = single on one side, list on other [OK]
- Swapping list and single types
- Using wrong types for fields
- Confusing employee and manager roles
Ready to prove it?
Pass the test and this pattern moves unknown
