Given the schema with a union SearchResult = Photo | Person, and the query below, what is the expected output?
query {
search(text: "john") {
... on Photo {
id
url
}
... on Person {
id
name
}
}
}Remember that when querying union types, you must use inline fragments to specify fields for each type.
The query uses inline fragments to select fields from each type in the union. The result is a list of objects matching either Photo or Person with their respective fields.
Choose the correct statement about union types in GraphQL.
Think about what types can be included in a union and how they differ from interfaces.
Union types can only include object types, not scalar types or interfaces. They allow a field to return one of several object types without requiring shared fields.
Which option shows the correct syntax for defining a union type SearchResult that includes Photo and Person?
union SearchResult = Photo | Person
Recall the exact syntax for union type declarations in GraphQL SDL.
The correct syntax uses the keyword union, the union name, an equals sign, and the member types separated by pipes.
You have a union type SearchResult = Photo | Person. You want to fetch only the id and name for Person and only the id and url for Photo. Which query is the most efficient?
Think about how inline fragments help fetch only needed fields per type.
Using inline fragments to specify fields per type avoids fetching unnecessary fields, reducing data transfer.
Given the union type SearchResult = Photo | Person, why does this query fail?
query {
search(text: "xyz") {
id
name
}
}Recall how to query fields on union types in GraphQL.
When querying union types, you must use inline fragments to specify fields for each possible type. Querying fields directly on the union causes an error.