0
0
GraphQLquery~20 mins

Search across types in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Search Across Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this GraphQL query searching across types?

Given a GraphQL schema with types Book and Author, and a search query that returns a union of these types, what will be the result of this query?

query {
  search(text: "Alice") {
    __typename
    ... on Book {
      title
      pages
    }
    ... on Author {
      name
      booksWritten
    }
  }
}
A[{"name": "Alice Walker", "booksWritten": 12}, {"title": "Alice in Wonderland", "pages": 200}]
B[{"__typename": "Author", "name": "Alice Walker", "booksWritten": 12}, {"__typename": "Book", "title": "Alice in Wonderland", "pages": 200}]
C[{"__typename": "Book", "title": "Alice in Wonderland", "pages": 200}]
D[]
Attempts:
2 left
💡 Hint

Remember that the search query returns a union type, so you need to check the __typename to know which fields are available.

🧠 Conceptual
intermediate
1:30remaining
Which GraphQL feature allows searching across multiple types in one query?

In GraphQL, how can you write a query that searches across different types like Movie and Director and returns results from both?

AUsing fragments only on one type
BUsing multiple separate queries for each type
CUsing a union type in the schema and inline fragments in the query
DUsing scalar types instead of objects
Attempts:
2 left
💡 Hint

Think about how to combine different types into one result list.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this GraphQL search query across types

What is wrong with this GraphQL query that searches across Article and Author types?

query {
  search(text: "GraphQL") {
    ... on Article {
      title
      content
    }
    ... on Author
      name
      articlesCount
  }
}
AThe search field must be called searchAll
BMissing __typename field in the query
CUsing ... on Article instead of ... on Articles
DMissing curly braces around the Author inline fragment fields
Attempts:
2 left
💡 Hint

Check the syntax of inline fragments carefully.

optimization
advanced
1:30remaining
How to optimize a GraphQL search query across types to reduce data transfer?

You have a search query returning a union of Product and Category. How can you reduce the amount of data sent over the network?

ARequest only the fields you need using inline fragments
BRemove the search filter to get fewer results
CUse a scalar type instead of a union type
DRequest all fields for both types to avoid multiple queries
Attempts:
2 left
💡 Hint

Think about selecting only necessary data.

🔧 Debug
expert
2:30remaining
Why does this GraphQL search query across types return an empty list?

Given this query:

query {
  search(text: "John") {
    __typename
    ... on User {
      username
    }
    ... on Post {
      title
    }
  }
}

But the result is always an empty list, even though there are users and posts with "John" in their data. What is the most likely cause?

AThe search resolver does not implement searching across both User and Post types
BThe query is missing the <code>id</code> field in the fragments
CThe <code>__typename</code> field is causing the query to fail
DThe search argument <code>text</code> is case-sensitive and "John" does not match
Attempts:
2 left
💡 Hint

Think about what the backend resolver does with the search argument.