Search across types in GraphQL - Time & Space Complexity
When searching across different types in a GraphQL schema, we want to know how the time to get results changes as the data grows.
We ask: How does the search time increase when there are more items in each type?
Analyze the time complexity of the following GraphQL query that searches multiple types.
query SearchAcrossTypes($term: String!) {
books(filter: { title_contains: $term }) {
id
title
}
authors(filter: { name_contains: $term }) {
id
name
}
publishers(filter: { name_contains: $term }) {
id
name
}
}
This query searches for a term in three different types: books, authors, and publishers, returning matching items from each.
Look for repeated work done during the search.
- Primary operation: Scanning each list of items in books, authors, and publishers to check if the term matches.
- How many times: Once for each item in each type's list.
As the number of items in each type grows, the search work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 items per type | About 30 checks (10 books + 10 authors + 10 publishers) |
| 100 items per type | About 300 checks |
| 1000 items per type | About 3000 checks |
Pattern observation: The total work grows roughly in direct proportion to the total number of items across all types.
Time Complexity: O(n)
This means the search time grows linearly with the total number of items searched across all types.
[X] Wrong: "Searching multiple types at once is faster because it's one query."
[OK] Correct: Even though it's one query, the server still checks each item in every type separately, so the total work adds up.
Understanding how searching multiple types affects performance helps you explain real-world API behavior clearly and confidently.
What if we added pagination to each type's search? How would that change the time complexity?
Practice
__typename field do in a GraphQL query when searching across types?Solution
Step 1: Understand the role of
The__typename__typenamefield returns the name of the type of each object in the query result.Step 2: Differentiate from other options
It does not filter, sort, or limit results; it only identifies the type.Final Answer:
It tells you the type of each returned object in the result. -> Option AQuick Check:
__typenameshows object type [OK]
- Thinking __typename filters results
- Assuming it sorts or limits data
- Confusing it with field selection
Book and Author, in one GraphQL query using fragments?Solution
Step 1: Recognize fragment syntax for multiple types
Fragments use... on TypeName { fields }to specify fields for each type.Step 2: Check each option's syntax
query { search { ... on Book { title } ... on Author { name } } } correctly uses inline fragments forBookandAuthor. Options B, C, and D have invalid syntax.Final Answer:
query { search { ... on Book { title } ... on Author { name } } } -> Option AQuick Check:
Fragments use... on Type[OK]
... on Type for each type's fields [OK]- Using type names as fields directly
- Omitting 'on' keyword in fragments
- Trying to list fields without fragments
query { search { __typename ... on Book { title author } ... on Author { name booksCount } } }What will be the output if the search returns one Book and one Author?
Solution
Step 1: Understand the query structure
The query requests__typenameand fields specific toBookandAuthortypes.Step 2: Match output to query
The output includes type names and requested fields correctly matched to each object.Final Answer:
[{ "__typename": "Book", "title": "1984", "author": "Orwell" }, { "__typename": "Author", "name": "Orwell", "booksCount": 5 }] -> Option BQuick Check:
Output matches requested fields and types [OK]
- Ignoring __typename in output
- Mixing fields between types
- Assuming syntax errors without cause
query { search { ... on Book { title author } ... on Author { name booksCount } } }Assuming
booksCount is not a valid field on Author type.Solution
Step 1: Check field validity on types
SincebooksCountis not defined onAuthor, the query is invalid.Step 2: Understand GraphQL error handling
GraphQL returns an error and fails the query if a requested field does not exist on the type.Final Answer:
The query will fail becausebooksCountis not a valid field onAuthor. -> Option CQuick Check:
Invalid field causes query failure [OK]
- Assuming invalid fields return null
- Ignoring GraphQL schema validation errors
- Thinking commas are required between fragments
Book, Author, and Publisher. Each type has a field name, but Book also has pages, Author has birthYear, and Publisher has location. Which query correctly fetches the name for all types and their specific fields?Solution
Step 1: Use
This helps distinguish which object is which type in the results.__typenameto identify each typeStep 2: Use inline fragments with
Each fragment fetches the common... on Typesyntaxnameand the type-specific fields.Step 3: Verify syntax correctness
query { search { __typename ... on Book { name pages } ... on Author { name birthYear } ... on Publisher { name location } } } correctly uses fragments and includes__typename. Other options have syntax errors or missing fragments.Final Answer:
query { search { __typename ... on Book { name pages } ... on Author { name birthYear } ... on Publisher { name location } } } -> Option DQuick Check:
Fragments + __typename fetch all fields correctly [OK]
- Listing all fields without fragments
- Omitting __typename for type identification
- Incorrect fragment syntax without 'on'
Ready to prove it?
Pass the test and this pattern moves unknown
