It means querying multiple different data types in a single search, so you can find results from various categories or objects at once.
Click to reveal answer
intermediate
How can you search across different types in GraphQL?
By using a union or interface type that groups multiple types together, then querying that union or interface to get results from all included types.
Click to reveal answer
beginner
What is a GraphQL union type?
A union type lets you combine multiple object types into one, so a query can return different types of objects in the same field.
Click to reveal answer
intermediate
Why use fragments when searching across types?
Fragments let you specify which fields to get from each type in a union or interface, so you get the right data for each kind of result.
Click to reveal answer
intermediate
Example: How to write a query that searches across 'Book' and 'Author' types?
Use a union type like 'union SearchResult = Book | Author' and query it with fragments for each type to get fields like title for Book and name for Author.
Click to reveal answer
What GraphQL feature allows combining multiple types for a single search?
AInput type
BScalar type
CUnion type
DEnum type
✗ Incorrect
Union types let you combine multiple object types so a field can return different types.
When querying a union type, how do you specify fields for each type?
AUsing fragments
BUsing variables
CUsing directives
DUsing enums
✗ Incorrect
Fragments let you define fields for each type inside a union or interface.
Which of these is NOT a valid use of searching across types?
AGetting user and product info together
BReturning only scalar values
CFinding books and authors in one query
DSearching multiple categories at once
✗ Incorrect
Searching across types involves objects, not just scalar values.
What keyword defines a union type in GraphQL schema?
Aenum
Binterface
Ctype
Dunion
✗ Incorrect
'union' keyword is used to define a union type.
Can you use interfaces instead of unions for searching across types?
AYes, interfaces can also group types
BNo, only unions work
COnly with scalar types
DOnly in mutations
✗ Incorrect
Interfaces can also group types and be used for searching across types.
Explain how to perform a search across multiple types in GraphQL.
Think about how to combine types and get fields from each.
You got /4 concepts.
Describe the difference between union types and interfaces when searching across types.
Consider how each groups types and what fields they require.
You got /3 concepts.
Practice
(1/5)
1. What does the __typename field do in a GraphQL query when searching across types?
easy
A. It tells you the type of each returned object in the result.
B. It filters the results to only one type.
C. It sorts the results alphabetically.
D. It limits the number of results returned.
Solution
Step 1: Understand the role of __typename
The __typename field returns the name of the type of each object in the query result.
Step 2: Differentiate from other options
It does not filter, sort, or limit results; it only identifies the type.
Final Answer:
It tells you the type of each returned object in the result. -> Option A
Quick Check:
__typename shows object type [OK]
Hint: Remember: __typename reveals object type, not filters or limits [OK]
Common Mistakes:
Thinking __typename filters results
Assuming it sorts or limits data
Confusing it with field selection
2. Which of the following is the correct syntax to query two different types, Book and Author, in one GraphQL query using fragments?
easy
A. query { search { ... on Book { title } ... on Author { name } } }
B. query { search { Book { title } Author { name } } }
C. query { search { title, name } }
D. query { search { ...Book { title } ...Author { name } } }
Solution
Step 1: Recognize fragment syntax for multiple types
Fragments use ... on TypeName { fields } to specify fields for each type.
Step 2: Check each option's syntax
query { search { ... on Book { title } ... on Author { name } } } correctly uses inline fragments for Book and Author. Options B, C, and D have invalid syntax.
Final Answer:
query { search { ... on Book { title } ... on Author { name } } } -> Option A
Quick Check:
Fragments use ... on Type [OK]
Hint: Use ... on Type for each type's fields [OK]
Common Mistakes:
Using type names as fields directly
Omitting 'on' keyword in fragments
Trying to list fields without fragments
3. Given this GraphQL query:
query { search { __typename ... on Book { title author } ... on Author { name booksCount } } }
What will be the output if the search returns one Book and one Author?
Hint: Look for __typename and matching fields in output [OK]
Common Mistakes:
Ignoring __typename in output
Mixing fields between types
Assuming syntax errors without cause
4. Identify the error in this GraphQL query that searches across types:
query { search { ... on Book { title author } ... on Author { name booksCount } } }
Assuming booksCount is not a valid field on Author type.
medium
A. The query will ignore the Author fragment entirely.
B. The query will succeed but return null for booksCount.
C. The query will fail because booksCount is not a valid field on Author.
D. The query syntax is invalid due to missing commas.
Solution
Step 1: Check field validity on types
Since booksCount is not defined on Author, the query is invalid.
Step 2: Understand GraphQL error handling
GraphQL returns an error and fails the query if a requested field does not exist on the type.
Final Answer:
The query will fail because booksCount is not a valid field on Author. -> Option C
Quick Check:
Invalid field causes query failure [OK]
Hint: Check if all fields exist on their types before querying [OK]
Common Mistakes:
Assuming invalid fields return null
Ignoring GraphQL schema validation errors
Thinking commas are required between fragments
5. You want to search across three types: Book, Author, and Publisher. Each type has a field name, but Book also has pages, Author has birthYear, and Publisher has location. Which query correctly fetches the name for all types and their specific fields?
hard
A. query { search { ...Book { name pages } ...Author { name birthYear } ...Publisher { name location } } }
B. query { search { name pages birthYear location } }
C. query { search { __typename { name } ... on Book { pages } ... on Author { birthYear } ... on Publisher { location } } }
D. query { search { __typename ... on Book { name pages } ... on Author { name birthYear } ... on Publisher { name location } } }
Solution
Step 1: Use __typename to identify each type
This helps distinguish which object is which type in the results.
Step 2: Use inline fragments with ... on Type syntax
Each fragment fetches the common name and the type-specific fields.
Step 3: Verify syntax correctness
query { search { __typename ... on Book { name pages } ... on Author { name birthYear } ... on Publisher { name location } } } correctly uses fragments and includes __typename. Other options have syntax errors or missing fragments.
Final Answer:
query { search { __typename ... on Book { name pages } ... on Author { name birthYear } ... on Publisher { name location } } } -> Option D
Quick Check:
Fragments + __typename fetch all fields correctly [OK]
Hint: Use __typename and fragments for each type's fields [OK]
Common Mistakes:
Listing all fields without fragments
Omitting __typename for type identification
Incorrect fragment syntax without 'on'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.