0
0
GraphQLquery~20 mins

Union types in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union Types Mastery
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 using union types?

Given the schema with a union SearchResult = Photo | Person, and the query below, what is the expected output?

GraphQL
query {
  search(text: "john") {
    ... on Photo {
      id
      url
    }
    ... on Person {
      id
      name
    }
  }
}
A[{"id": "p1", "name": "John Doe"}, {"id": "u1", "url": "http://example.com/photo1.jpg"}]
B]}"gpj.1otohp/moc.elpmaxe//:ptth" :"lru" ,"1u" :"di"{ ,}"eoD nhoJ" :"eman" ,"1p" :"di"{[
C[{"id": "p1", "url": "http://example.com/photo1.jpg", "name": "John Doe"}]
DSyntaxError: Cannot query fields on union type without inline fragments
Attempts:
2 left
💡 Hint

Remember that when querying union types, you must use inline fragments to specify fields for each type.

🧠 Conceptual
intermediate
1:30remaining
Which statement about GraphQL union types is true?

Choose the correct statement about union types in GraphQL.

AUnion types can include scalar types like String or Int.
BUnion types are used to define fields that return lists of a single object type.
CUnion types require all member types to share the same fields.
DUnion types allow a field to return one of several object types but cannot include interfaces.
Attempts:
2 left
💡 Hint

Think about what types can be included in a union and how they differ from interfaces.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this GraphQL union type definition

Which option shows the correct syntax for defining a union type SearchResult that includes Photo and Person?

GraphQL
union SearchResult = Photo | Person
Aunion SearchResult = Photo | Person
Bunion SearchResult: Photo | Person
Cunion SearchResult { Photo | Person }
Dtype SearchResult = Photo | Person
Attempts:
2 left
💡 Hint

Recall the exact syntax for union type declarations in GraphQL SDL.

optimization
advanced
2:00remaining
How to optimize a query using union types to reduce data transfer?

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?

Aquery { search(text: "abc") { ... on Person { id name url } ... on Photo { id url name } } }
Bquery { search(text: "abc") { id name url } }
Cquery { search(text: "abc") { ... on Person { id name } ... on Photo { id url } } }
Dquery { search(text: "abc") { id } }
Attempts:
2 left
💡 Hint

Think about how inline fragments help fetch only needed fields per type.

🔧 Debug
expert
2:30remaining
Why does this GraphQL query with union types fail?

Given the union type SearchResult = Photo | Person, why does this query fail?

query {
  search(text: "xyz") {
    id
    name
  }
}
ABecause the search field does not accept arguments.
BBecause fields must be queried inside inline fragments for union types.
CBecause the query is missing the __typename field.
DBecause the union type must be replaced with an interface in the query.
Attempts:
2 left
💡 Hint

Recall how to query fields on union types in GraphQL.