0
0
GraphQLquery~20 mins

Abstract type resolution in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract Type Resolution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a GraphQL query with interface type

Given the following GraphQL schema snippet:

interface Character {
  id: ID!
  name: String!
}
type Human implements Character {
  id: ID!
  name: String!
  homePlanet: String
}
type Droid implements Character {
  id: ID!
  name: String!
  primaryFunction: String
}

And this query:

{
  characters {
    id
    name
    ... on Human {
      homePlanet
    }
    ... on Droid {
      primaryFunction
    }
  }
}

Assuming the data contains one Human and one Droid, what is the shape of the returned data?

A{"characters":[{"id":"1","name":"Luke","primaryFunction":"Astromech"},{"id":"2","name":"R2-D2","homePlanet":"Tatooine"}]}
B{"characters":[{"id":"1","name":"Luke"},{"id":"2","name":"R2-D2"}]}
C{"characters":[{"id":"1","name":"Luke","homePlanet":"Tatooine"},{"id":"2","name":"R2-D2","primaryFunction":"Astromech"}]}
D{"characters":[{"id":"1","name":"Luke","homePlanet":null},{"id":"2","name":"R2-D2","primaryFunction":null}]}
Attempts:
2 left
💡 Hint

Look at the inline fragments ... on Human and ... on Droid to see which fields are included for each type.

🧠 Conceptual
intermediate
1:30remaining
Understanding abstract type resolution in GraphQL

In GraphQL, when a query requests a field of an abstract type (interface or union), how does the server determine which concrete type to use for each result item?

AThe server uses the __typename field or a custom resolveType function to identify the concrete type for each item.
BThe client specifies the concrete type explicitly in the query, so the server does not need to resolve it.
CThe server always returns the first type listed in the schema for the abstract type.
DThe server randomly picks a concrete type for each item when resolving abstract types.
Attempts:
2 left
💡 Hint

Think about how GraphQL knows which fields to return for each item when the type is abstract.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this GraphQL fragment for abstract type resolution

Consider this GraphQL query fragment:

{
  search(text: "robot") {
    ... on Human {
      name
      homePlanet
    }
    ... Droid {
      name
      primaryFunction
    }
  }
}

What is the syntax error in this fragment?

AThe fields inside the fragments must be wrapped in braces.
BThe fragment on Human should be a named fragment, not inline.
CThe search field cannot have inline fragments.
DThe inline fragment on Droid is missing the keyword 'on'.
Attempts:
2 left
💡 Hint

Check the syntax for inline fragments in GraphQL.

🔧 Debug
advanced
2:00remaining
Why does this GraphQL query return null for type-specific fields?

Given this schema:

interface Animal {
  id: ID!
  name: String!
}
type Dog implements Animal {
  id: ID!
  name: String!
  breed: String
}
type Cat implements Animal {
  id: ID!
  name: String!
  livesLeft: Int
}

And this query:

{
  animals {
    id
    name
    ... on Dog {
      breed
    }
    ... on Cat {
      livesLeft
    }
  }
}

The response returns null for breed and livesLeft fields for all animals. What is the most likely cause?

AThe server's resolveType function is not implemented or returns null, so type-specific fields are not resolved.
BThe query is missing the __typename field, so the client cannot distinguish types.
CThe animals field does not return any data, so all fields are null.
DThe schema does not allow inline fragments on interfaces.
Attempts:
2 left
💡 Hint

Think about how the server knows which concrete type each item is.

optimization
expert
3:00remaining
Optimizing abstract type resolution for large GraphQL queries

You have a GraphQL query that fetches a large list of items of an interface type with many inline fragments for different implementations. The query is slow because the server resolves the concrete type for each item individually. Which approach can optimize abstract type resolution performance?

AIncrease the query timeout to allow more time for type resolution.
BBatch resolve types by grouping items by type in the resolveType function to reduce overhead.
CUse client-side caching to avoid repeated queries for the same data.
DRemove inline fragments and query only common interface fields to avoid type resolution.
Attempts:
2 left
💡 Hint

Think about how to reduce repeated work on the server when resolving types.