Abstract type resolution in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a GraphQL query asks for data from an abstract type, the system must figure out which concrete type each item belongs to.
We want to understand how the time to do this type checking grows as the number of items increases.
Analyze the time complexity of the following code snippet.
query {
search(text: "book") {
__typename
... on Book {
title
author
}
... on Magazine {
title
issue
}
}
}
This query asks for a list of search results that can be different types. The system must check each result's type to return the right fields.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the concrete type of each item in the search results.
- How many times: Once for each item in the list.
As the number of search results grows, the system must check each item's type one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 type checks |
| 100 | 100 type checks |
| 1000 | 1000 type checks |
Pattern observation: The work grows directly with the number of items; doubling items doubles the checks.
Time Complexity: O(n)
This means the time to resolve types grows in a straight line with the number of items.
[X] Wrong: "The system can resolve all types instantly regardless of how many items there are."
[OK] Correct: Each item must be checked individually, so more items mean more work.
Understanding how abstract type resolution scales helps you explain how GraphQL handles flexible data shapes efficiently.
"What if the system cached type information for items? How would that affect the time complexity?"
Practice
__resolveType function in GraphQL when using interfaces or unions?Solution
Step 1: Understand abstract types in GraphQL
Abstract types like interfaces or unions can represent multiple object types.Step 2: Role of
This function tells GraphQL which concrete type to use for the returned data.__resolveTypeFinal Answer:
To determine the specific object type to return for an abstract type -> Option DQuick Check:
Abstract type resolution = determine specific type [OK]
__resolveType picks the exact type [OK]- Confusing
__resolveTypewith data fetching - Thinking it validates query syntax
- Assuming it defines scalar types
__resolveType function in a GraphQL resolver object?Solution
Step 1: Check the naming and return value
The function must be named exactly__resolveTypeand return a string matching a type name.Step 2: Match the returned value to the data field
Commonly, the field__typenameholds the type name, so returningobj.__typenameis correct.Final Answer:
__resolveType(obj) { return obj.__typename; } -> Option CQuick Check:
Correct function name and return field = __resolveType(obj) { return obj.__typename; } [OK]
__resolveType [OK]- Using wrong function name like resolveType
- Returning incorrect property like type
- Returning undefined or wrong field
__resolveType(obj) {
if (obj.price) return 'Book';
if (obj.author) return 'Author';
return null;
}What will be the resolved type for
{ price: 20, author: 'John' }?Solution
Step 1: Check conditions in order
The function first checks ifobj.priceexists, which is true here.Step 2: Return first matching type
Sinceobj.priceis true, it returns 'Book' immediately without checking further.Final Answer:
Book -> Option AQuick Check:
First true condition returns 'Book' [OK]
- Assuming it returns 'Author' because author field exists
- Thinking it returns null if multiple fields exist
- Expecting an error for multiple matches
__resolveType function:__resolveType(obj) {
if (obj.kind === 'User') return 'User';
if (obj.kind === 'Admin') return 'Admin';
}But your GraphQL query returns
null for the type. What is the likely problem?Solution
Step 1: Check function completeness
The function lacks a return for cases whenobj.kindis neither 'User' nor 'Admin'.Step 2: Understand GraphQL behavior
If no type is returned, GraphQL resolves the type asnull, causing query issues.Final Answer:
Missing a return statement for unmatched cases -> Option BQuick Check:
Always return a type or null explicitly [OK]
__resolveType [OK]- Using wrong function name without underscores
- Assuming missing return defaults to a type
- Believing GraphQL doesn't support
__resolveType
Vehicle implemented by types Car and Bike. Your __resolveType function is:__resolveType(obj) {
return obj.wheels === 4 ? 'Car' : 'Bike';
}If an object has
{ wheels: 0 }, what will happen when querying this interface?Solution
Step 1: Evaluate the ternary condition
The condition checks ifobj.wheels === 4. For 0, this is false.Step 2: Determine returned type
Since condition is false, it returns 'Bike'.Final Answer:
It will resolve to 'Bike' because wheels is not 4 -> Option AQuick Check:
Condition false returns 'Bike' [OK]
__resolveType [OK]- Confusing falsy 0 with true condition
- Expecting runtime error for zero wheels
- Assuming null return causes failure
