Inline fragments in GraphQL - Time & Space Complexity
When using inline fragments in GraphQL, we want to know how the time to get data changes as the data grows.
We ask: How does the work grow when the number of items or types increases?
Analyze the time complexity of the following code snippet.
query {
searchItems {
id
... on Book {
author
pages
}
... on Movie {
director
duration
}
}
}
This query fetches a list of items, and for each item, it fetches fields depending on the item type using inline fragments.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the searchItems list.
- How many times: Once for each item returned by the search.
As the number of items grows, the server checks each item's type and fetches the matching fields.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 type checks and field fetches |
| 100 | 100 type checks and field fetches |
| 1000 | 1000 type checks and field fetches |
Pattern observation: The work grows directly with the number of items; doubling items doubles the work.
Time Complexity: O(n)
This means the time to get results grows in a straight line with the number of items.
[X] Wrong: "Using inline fragments makes the query slower exponentially because it checks many types."
[OK] Correct: Each item is checked once for its type, so the work grows only with the number of items, not the number of types.
Understanding how inline fragments affect query time helps you explain how GraphQL handles different data shapes efficiently.
"What if the number of inline fragments grows with the number of types? How would the time complexity change?"
Practice
inline fragments in a GraphQL query?Solution
Step 1: Understand inline fragments usage
Inline fragments allow querying different fields depending on the type of an object in a list or union.Step 2: Identify the purpose in queries
This lets you get specific fields for each type without separate queries.Final Answer:
To select fields conditionally based on the object type in a mixed list -> Option AQuick Check:
Inline fragments = conditional fields by type [OK]
- Thinking inline fragments create new types
- Confusing inline fragments with mutations
- Using inline fragments for pagination
Solution
Step 1: Recall inline fragment syntax
The correct syntax uses three dots, thenon, then the type name, followed by fields in braces.Step 2: Match options to syntax
... on TypeName { field1 field2 } matches this exactly:... on TypeName { field1 field2 }.Final Answer:
... on TypeName { field1 field2 } -> Option DQuick Check:
Inline fragment syntax = '... on TypeName { fields }' [OK]
- Omitting the 'on' keyword
- Placing type name inside braces incorrectly
- Using wrong order of keywords
{
searchItems {
id
... on Book {
author
}
... on Movie {
director
}
}
}What fields will be returned for an item of type
Movie?Solution
Step 1: Identify fields outside and inside fragments
The fieldidis always returned for all items. ForMovietype, the inline fragment selectsdirector.Step 2: Combine fields for Movie type
So, the result for aMovieitem includesidanddirector.Final Answer:
id and director -> Option AQuick Check:
Movie fields = id + director [OK]
- Assuming author field returns for Movie
- Ignoring fields outside fragments
- Thinking only fragment fields return
{
items {
... on Book {
title
author
}
... on Movie {
title
director
}
}
}What is the error in this query?
Solution
Step 1: Check query braces
The query opens braces foritemsand two inline fragments but does not close theitemsfield or the root query braces.Step 2: Identify missing closing braces
Proper GraphQL syntax requires matching closing braces for all opened braces.Final Answer:
Missing closing brace for the root field -> Option BQuick Check:
All braces must close properly [OK]
- Forgetting to close braces
- Thinking inline fragments need unique fields
- Believing you cannot query multiple types
SearchResult = Book | Movie | Author. You want to query a list of searchResults and get the title for Book, director for Movie, and name for Author. Which query correctly uses inline fragments to achieve this?Solution
Step 1: Understand union type querying
For a union, you must use inline fragments with... on TypeNameto get fields specific to each type.Step 2: Evaluate each option
{ searchResults { ... on Book { title } ... on Movie { director } ... on Author { name } } } correctly uses inline fragments for each type. { searchResults { title director name } } tries to get all fields directly, which fails. { searchResults { ... { title director name } } } has invalid syntax. { searchResults { ... on SearchResult { title director name } } } incorrectly uses the union name as a type condition.Final Answer:
{ searchResults { ... on Book { title } ... on Movie { director } ... on Author { name } } } -> Option CQuick Check:
Use inline fragments per type in unions [OK]
- Querying all fields without fragments on unions
- Using union name as type condition
- Incorrect fragment syntax
Ready to prove it?
Pass the test and this pattern moves unknown
