List types in GraphQL - Time & Space Complexity
When working with list types in GraphQL, it's important to understand how the time to process data grows as the list gets bigger.
We want to know how the number of operations changes when the list size increases.
Analyze the time complexity of the following GraphQL query using list types.
query GetUsers {
users {
id
name
posts {
title
}
}
}
This query fetches a list of users, and for each user, it fetches their posts as a list.
Look for repeated actions in the query processing.
- Primary operation: Fetching each user and then fetching each post for that user.
- How many times: For n users, and each user having m posts, the posts fetching happens n x m times.
As the number of users and posts grows, the work grows too.
| Input Size (n users, m posts each) | Approx. Operations |
|---|---|
| 10 users, 5 posts each | 10 + (10 x 5) = 60 |
| 100 users, 5 posts each | 100 + (100 x 5) = 600 |
| 100 users, 50 posts each | 100 + (100 x 50) = 5,100 |
Pattern observation: The total operations grow roughly with the number of users times the number of posts per user.
Time Complexity: O(n x m)
This means the time grows proportionally to the number of users multiplied by the number of posts each user has.
[X] Wrong: "Fetching a list of users with posts is just O(n) because we only count users."
[OK] Correct: Each user's posts are also fetched, so the total work depends on both users and posts, not just users.
Understanding how nested lists affect query time helps you explain data fetching costs clearly and shows you can reason about performance in real projects.
"What if the posts list was limited to a fixed number, say 10 posts per user? How would the time complexity change?"
Practice
[Int] means a list of what?Solution
Step 1: Understand list notation in GraphQL
Square brackets[]indicate a list type that holds multiple values of the same type.Step 2: Interpret
This means a list containing zero or more integers.[Int]Final Answer:
A collection of integers -> Option CQuick Check:
List type = multiple values [OK]
- Thinking it holds a single value
- Confusing list with string or boolean
- Ignoring the brackets meaning
Solution
Step 1: Recall GraphQL list syntax
GraphQL uses square brackets[]around a type to define a list.Step 2: Identify correct option
[String]is the correct way to define a list of strings in GraphQL.Final Answer:
[String] -> Option BQuick Check:
List syntax = brackets around type [OK]
- Using language-specific syntax like string[]
- Using parentheses or curly braces instead of brackets
- Missing brackets entirely
type Query {
favoriteNumbers: [Int]
}What is a valid response for
favoriteNumbers?Solution
Step 1: Understand the field type
favoriteNumbersis a list of integers, so the response must be a list (array) of numbers.Step 2: Check each option
[1, 2, 3] is a list of integers. 42 is a single integer, not a list. "[1, 2, 3]" is a string, not a list. {1, 2, 3} uses curly braces which is invalid syntax.Final Answer:
[1, 2, 3] -> Option DQuick Check:
List field returns list value [OK]
- Returning a single value instead of a list
- Returning a string instead of a list
- Using wrong brackets for lists
type User {
tags: [String
}Solution
Step 1: Check list type syntax
List types require both opening and closing square brackets around the type.Step 2: Locate syntax error
The closing bracket "]" is missing afterString, causing a syntax error.Final Answer:
Missing closing bracket "]" for the list type -> Option AQuick Check:
List syntax needs matching brackets [OK]
- Forgetting the closing bracket
- Using wrong brackets like parentheses
- Thinking list can't hold strings
Solution
Step 1: Understand non-null and list syntax
Int!means non-null integer,[Int!]means list of non-null integers, list itself can be null or empty.Step 2: Analyze options
[Int!]: List of non-null integers, list nullable (can be empty).[Int]!: Non-null list of nullable integers.[Int!]!: Non-null list of non-null integers (list cannot be null).Int!: Single non-null integer, not a list.Final Answer:
[Int!] -> Option AQuick Check:
Non-null items inside nullable list = [Int!] [OK]
- Making list itself non-null when it should be nullable
- Confusing non-null item vs non-null list
- Using single type instead of list
Ready to prove it?
Pass the test and this pattern moves unknown
