0
0
GraphQLquery~10 mins

Inline fragments in GraphQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Inline fragments
Start Query
Check Type Condition?
Apply Inline Fragment
Collect Fields
Return Result
The query starts and checks if the current data matches the inline fragment's type condition. If yes, it applies the fragment fields; if no, it skips them, then collects all fields and returns the result.
Execution Sample
GraphQL
query {
  search(text: "apple") {
    ... on Fruit {
      color
    }
    ... on Vegetable {
      size
    }
  }
}
This query searches for items named 'apple' and returns 'color' if the item is a Fruit, or 'size' if it is a Vegetable, using inline fragments.
Execution Table
StepCurrent Item TypeCheck Inline Fragment TypeMatch?ActionFields Collected
1FruitFruitYesApply fragmentcolor
2FruitVegetableNoSkip fragmentcolor
3VegetableFruitNoSkip fragment[]
4VegetableVegetableYesApply fragmentsize
5GrainFruitNoSkip fragment
6GrainVegetableNoSkip fragment
7---Return collected fieldscolor or size or empty
💡 All inline fragments checked for each item type; fields collected accordingly; query execution ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Current Item TypeNoneFruitFruitVegetableVegetableGrain
Fields Collected[][color][color][][size][]
Key Moments - 2 Insights
Why does the query skip some inline fragments for certain item types?
Because inline fragments only apply if the current item's type matches the fragment's type condition, as shown in execution_table rows where 'Match?' is 'No' and action is 'Skip fragment'.
What happens if the item type does not match any inline fragment?
No fields from inline fragments are collected for that item, resulting in empty fields, as seen in execution_table rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what fields are collected when the current item type is 'Fruit'?
Asize
Bcolor
Ccolor and size
Dnone
💡 Hint
Check rows 1 and 2 in execution_table where Current Item Type is 'Fruit' and see which fragment matched.
At which step does the inline fragment for 'Vegetable' get applied for a 'Vegetable' item?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 where Current Item Type is 'Vegetable' and fragment type is 'Vegetable'.
If a new item type 'Berry' is added without a matching inline fragment, what will happen to fields collected?
AFields from Fruit fragment will be collected
BFields from Vegetable fragment will be collected
CNo fields will be collected
DAll fragment fields will be collected
💡 Hint
Refer to execution_table rows 5 and 6 where no fragment matches result in empty fields.
Concept Snapshot
Inline fragments let you conditionally select fields based on the type of the data.
Syntax: ... on TypeName { fields }
If the data matches TypeName, fields inside the fragment are included.
If not, those fields are skipped.
Useful for querying interfaces or unions with different types.
Full Transcript
Inline fragments in GraphQL allow you to ask for fields only if the data is of a certain type. When the query runs, it checks each item’s type against the inline fragment’s type condition. If they match, it collects the fields inside that fragment. If not, it skips them. This way, you can write one query that works for different types of data, like Fruits and Vegetables, and get the right fields for each. The execution table shows how for each item type, the query checks fragments and collects fields accordingly. If no fragment matches, no fields from fragments are collected. This helps you get exactly the data you want depending on the type.