Given this GraphQL schema with types Book and Magazine implementing Publication, and this query:
{
publications {
title
... on Book {
author
}
... on Magazine {
issue
}
}
}Assuming the data has one Book titled "GraphQL Guide" by "Alice" and one Magazine titled "Tech Monthly" with issue 42, what is the query result?
Inline fragments allow you to select fields specific to a type when querying an interface or union.
The query returns a list of publications. For each item, it includes the title field, and then uses inline fragments to include author only if the item is a Book, and issue only if the item is a Magazine. So the Book has author, the Magazine has issue.
Choose the best reason why inline fragments are used in GraphQL queries.
Think about how GraphQL handles interfaces and unions with different possible types.
Inline fragments let you specify fields that only apply to certain types when querying interfaces or unions. This lets you get type-specific data in one query.
Which option contains a syntax error in the use of inline fragments?
{
search(term: "GraphQL") {
... on Book {
title
author
}
... Magazine {
title
issue
}
}
}Check the syntax for inline fragments carefully.
Inline fragments require the keyword on before the type name. The fragment on Magazine is missing on, causing a syntax error.
Which option best explains how inline fragments can optimize GraphQL queries?
Think about how selecting fields conditionally affects the data returned.
Inline fragments let you request fields only for the types that have them, so you avoid fetching irrelevant fields and reduce data size.
Given this query:
{
items {
... on Product {
id
price
}
... on Service {
id
duration
}
... on Product {
name
}
}
}Assuming items returns a list of Product and Service types, what error will this query cause?
Inline fragments on the same type are allowed; GraphQL merges their field selections.
GraphQL allows multiple inline fragments on the same type at the same level. Their field selections are merged (e.g., id, price, and name for Product), so no error occurs.