0
0
GraphQLquery~5 mins

Inline fragments in GraphQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an inline fragment in GraphQL?
An inline fragment lets you conditionally include fields in a query based on the type of the object. It helps when querying interfaces or unions to get fields specific to a certain type.
Click to reveal answer
beginner
How do you write an inline fragment in a GraphQL query?
Use three dots followed by on TypeName inside curly braces. For example:<br>
{ ... on User { name } }
This fetches the name field only if the object is of type User.
Click to reveal answer
intermediate
Why use inline fragments instead of separate queries?
Inline fragments let you get different fields for different types in a single query. This saves time and network calls, like ordering different toppings on one pizza instead of ordering many pizzas separately.
Click to reveal answer
intermediate
Can inline fragments be used with interfaces and unions?
Yes! Inline fragments are especially useful with interfaces and unions because these types can represent multiple object types. Inline fragments let you ask for fields specific to each possible type.
Click to reveal answer
beginner
What happens if the inline fragment type does not match the object type?
If the object type does not match the inline fragment type, the fields inside that fragment are ignored and not included in the result. This keeps the query safe and only returns relevant data.
Click to reveal answer
What symbol starts an inline fragment in GraphQL?
A::
B...
C--
D##
Inline fragments are most useful when querying which GraphQL types?
AInput types
BScalars
CEnums
DInterfaces and Unions
If an inline fragment's type does not match the object, what happens?
AThe fields inside the fragment are ignored
BAll fields are returned anyway
CThe query fails
DThe server returns an error
Which of these is a correct inline fragment syntax?
A... on User { name }
B... User { name }
Con User ... { name }
Dfragment User on { name }
Why use inline fragments instead of separate queries?
ATo avoid using variables
BBecause separate queries are not allowed
CTo reduce network calls and get different fields in one query
DTo make queries longer
Explain what inline fragments are and why they are useful in GraphQL queries.
Think about how you get different info for different types in one request.
You got /4 concepts.
    Write the syntax for an inline fragment that fetches the 'title' field only if the object is of type 'Book'.
    Use ... on TypeName { fields } format.
    You got /4 concepts.