What if you could ask for exactly what you want from mixed data without sorting it yourself?
Why Inline fragments in GraphQL? - Purpose & Use Cases
Imagine you have a big box of mixed toys: cars, dolls, and blocks. You want to list only the cars with their special features, but you have to look at each toy one by one and guess if it is a car before writing down its details.
Doing this by hand is slow and confusing. You might miss some cars or mix up details from dolls or blocks. It's hard to keep track and easy to make mistakes when sorting and describing each toy separately.
Inline fragments let you tell the system exactly how to pick and show details for each type inside a mixed group. You write one query that says, "If this is a car, show these features," and it automatically skips the rest. This saves time and avoids errors.
query { toys { id name ... on Car { wheels engine } ... on Doll { hairColor } } }query { toys { id name ... on Car { wheels engine } } }Inline fragments make it easy to get just the right details from mixed data without extra work or confusion.
When building a shopping app, you can fetch different product types in one request and show only the relevant info for each, like size for clothes and battery life for electronics.
Manual filtering of mixed data is slow and error-prone.
Inline fragments let you specify type-based selections inside one query.
This makes data fetching cleaner, faster, and less confusing.
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
