0
0
GraphQLquery~5 mins

Fragments for reusable selections in GraphQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a fragment in GraphQL?
A fragment is a reusable piece of a query that lets you define a set of fields once and include them in multiple queries or mutations to avoid repetition.
Click to reveal answer
beginner
How do you define a fragment in GraphQL?
You define a fragment using the fragment keyword, followed by a name, the type it applies to, and the fields inside curly braces. Example:
fragment userFields on User { id name email }
Click to reveal answer
beginner
How do you use a fragment inside a GraphQL query?
You include a fragment in a query by using three dots followed by the fragment name. Example:
{ user(id: 1) { ...userFields } }
Click to reveal answer
intermediate
Why are fragments useful in GraphQL?
Fragments help keep queries clean and consistent by reusing field selections. They reduce errors and make it easier to update fields in many places at once.
Click to reveal answer
intermediate
Can fragments be used with different types in GraphQL?
Fragments are defined for a specific type. To reuse fields across different types, you can use interfaces or unions with fragments on those types, but a fragment itself applies to one type only.
Click to reveal answer
What keyword is used to define a fragment in GraphQL?
Afragment
Bselect
Cinclude
Duse
How do you include a fragment named userFields inside a query?
A...userFields
B... on userFields
C...userFields()
D...fragment userFields
Fragments in GraphQL help to:
ACreate new types
BReuse field selections to avoid repetition
CDefine mutations
DChange the schema
Can a fragment be used on multiple different types directly?
AYes, but only in mutations
BYes, fragments are universal
COnly if types share the same name
DNo, a fragment applies to one specific type
Which of the following is a correct fragment definition?
Afragment userFields { id name }
Bfragment on User { id name }
Cfragment userFields on User { id name }
Dfragment userFields User { id name }
Explain what GraphQL fragments are and how they help when writing queries.
Think about how you might copy-paste fields in many queries and how fragments solve that.
You got /4 concepts.
    Describe the syntax to define and use a fragment in a GraphQL query.
    Remember the three dots when including a fragment.
    You got /5 concepts.