Bird
Raised Fist0
GraphQLquery~10 mins

Search across types in GraphQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Search across types
Start Search Query
↓
Identify Types to Search
↓
Execute Search on Each Type
↓
Collect Results from All Types
↓
Combine Results into Single List
↓
Return Combined Results to Client
The search query runs on multiple types, collects results, and returns them combined.
Execution Sample
GraphQL
query SearchAll($term: String!) {
  search(term: $term) {
    __typename
    ... on Book {
      title
      author
    }
    ... on Author {
      name
      books {
        title
      }
    }
  }
}
This query searches for a term across Book and Author types and returns matching fields.
Execution Table
StepActionType SearchedSearch TermResults FoundCombined Results
1Start search queryN/A"GraphQL"N/A[]
2Search BooksBook"GraphQL"[{"title": "Learning GraphQL", "author": "Eve"}][{"title": "Learning GraphQL", "author": "Eve"}]
3Search AuthorsAuthor"GraphQL"[{"name": "Eve", "books": [{"title": "Learning GraphQL"}]}][{"title": "Learning GraphQL", "author": "Eve"}, {"name": "Eve", "books": [{"title": "Learning GraphQL"}]}]
4Return combined resultsN/AN/AN/A[{"title": "Learning GraphQL", "author": "Eve"}, {"name": "Eve", "books": [{"title": "Learning GraphQL"}]}]
💡 All types searched and results combined, query returns combined list.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
searchTerm"GraphQL""GraphQL""GraphQL""GraphQL"
bookResults[][{"title": "Learning GraphQL", "author": "Eve"}][{"title": "Learning GraphQL", "author": "Eve"}][{"title": "Learning GraphQL", "author": "Eve"}]
authorResults[][][{"name": "Eve", "books": [{"title": "Learning GraphQL"}]}][{"name": "Eve", "books": [{"title": "Learning GraphQL"}]}]
combinedResults[][{"title": "Learning GraphQL", "author": "Eve"}][{"title": "Learning GraphQL", "author": "Eve"}, {"name": "Eve", "books": [{"title": "Learning GraphQL"}]}][{"title": "Learning GraphQL", "author": "Eve"}, {"name": "Eve", "books": [{"title": "Learning GraphQL"}]}]
Key Moments - 3 Insights
Why do we need to include __typename in the query?
Including __typename helps identify which type each result belongs to when combining results from multiple types, as shown in execution_table step 4.
How are results combined from different types?
Results from each type are collected separately and then merged into one list, as seen in combinedResults variable in variable_tracker after step 3.
What happens if no results are found in one type?
If a type returns no results, its result list is empty but the combined results still include results from other types, ensuring no data loss.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the combinedResults value?
A[{"title": "Learning GraphQL", "author": "Eve"}]
B[{"title": "Learning GraphQL", "author": "Eve"}, {"name": "Eve", "books": [{"title": "Learning GraphQL"}]}]
C[{"name": "Eve", "books": [{"title": "Learning GraphQL"}]}]
D[]
💡 Hint
Check the Combined Results column in execution_table row for step 3.
At which step does the search query return the final combined results?
AStep 4
BStep 1
CStep 3
DStep 2
💡 Hint
Look for the step labeled 'Return combined results' in execution_table.
If the search term changes, which variable in variable_tracker changes accordingly?
AbookResults
BauthorResults
CsearchTerm
DcombinedResults
💡 Hint
Look at the searchTerm variable values across steps in variable_tracker.
Concept Snapshot
Search across types in GraphQL:
- Use a single query with fragments for each type.
- Include __typename to identify result types.
- Search each type separately.
- Combine all results into one list.
- Return combined results to client.
Full Transcript
This visual execution shows how a GraphQL search query runs across multiple types. The query starts by receiving a search term. It then searches the Book type and collects matching books. Next, it searches the Author type and collects matching authors. Each type's results are stored separately. Then, all results are combined into one list. Finally, the combined list is returned to the client. Including __typename in the query helps identify which type each result belongs to. If one type has no results, the others still return their matches. This process ensures a unified search experience across different data types.

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

  1. Step 1: Understand the role of __typename

    The __typename field returns the name of the type of each object in the query result.
  2. Step 2: Differentiate from other options

    It does not filter, sort, or limit results; it only identifies the type.
  3. Final Answer:

    It tells you the type of each returned object in the result. -> Option A
  4. 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

  1. Step 1: Recognize fragment syntax for multiple types

    Fragments use ... on TypeName { fields } to specify fields for each type.
  2. 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.
  3. Final Answer:

    query { search { ... on Book { title } ... on Author { name } } } -> Option A
  4. 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?
medium
A. [{ "title": "1984", "author": "Orwell" }, { "name": "Orwell", "booksCount": 5 }]
B. [{ "__typename": "Book", "title": "1984", "author": "Orwell" }, { "__typename": "Author", "name": "Orwell", "booksCount": 5 }]
C. [{ "__typename": "Book", "name": "1984", "author": "Orwell" }, { "__typename": "Author", "title": "Orwell", "booksCount": 5 }]
D. Error: Cannot query field 'booksCount' on type 'Author'

Solution

  1. Step 1: Understand the query structure

    The query requests __typename and fields specific to Book and Author types.
  2. Step 2: Match output to query

    The output includes type names and requested fields correctly matched to each object.
  3. Final Answer:

    [{ "__typename": "Book", "title": "1984", "author": "Orwell" }, { "__typename": "Author", "name": "Orwell", "booksCount": 5 }] -> Option B
  4. Quick Check:

    Output matches requested fields and types [OK]
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

  1. Step 1: Check field validity on types

    Since booksCount is not defined on Author, the query is invalid.
  2. Step 2: Understand GraphQL error handling

    GraphQL returns an error and fails the query if a requested field does not exist on the type.
  3. Final Answer:

    The query will fail because booksCount is not a valid field on Author. -> Option C
  4. 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

  1. Step 1: Use __typename to identify each type

    This helps distinguish which object is which type in the results.
  2. Step 2: Use inline fragments with ... on Type syntax

    Each fragment fetches the common name and the type-specific fields.
  3. 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.
  4. Final Answer:

    query { search { __typename ... on Book { name pages } ... on Author { name birthYear } ... on Publisher { name location } } } -> Option D
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard