Inline fragments let you ask for different fields depending on the type of data you get back. This helps when you have mixed types in one query.
Inline fragments in GraphQL
query {
someField {
... on TypeName {
field1
field2
}
... on AnotherType {
fieldA
fieldB
}
}
}Use ... on TypeName inside your query to specify fields for that type.
Inline fragments help when the server returns different types in the same field.
name and email if the item is a User, or title and price if it is a Product.query {
searchResults {
... on User {
name
email
}
... on Product {
title
price
}
}
}message for all notifications, but also gets severity if it is an Alert, or remindAt if it is a Reminder.query {
notifications {
message
... on Alert {
severity
}
... on Reminder {
remindAt
}
}
}This query fetches a list of items. For each item, it always gets the id. If the item is a Book, it also gets the title and author. If it is a Movie, it gets the title and director.
query {
items {
id
... on Book {
title
author
}
... on Movie {
title
director
}
}
}Inline fragments only work when the server schema supports multiple types for a field.
You can mix inline fragments with common fields outside them.
Inline fragments let you get different fields based on the data type.
They help when querying mixed-type lists or unions.
Use ... on TypeName inside your query to specify fields for each type.
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
