Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Querying an Interface Field
Given the GraphQL schema with an interface
Character and two types Human and Droid implementing it, what is the output of this query?query {
characters {
id
name
... on Human {
homePlanet
}
... on Droid {
primaryFunction
}
}
}GraphQL
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
}
query {
characters {
id
name
... on Human {
homePlanet
}
... on Droid {
primaryFunction
}
}
}Attempts:
2 left
💡 Hint
Remember that inline fragments allow querying fields specific to the implementing types.
✗ Incorrect
The query uses inline fragments to fetch fields specific to each implementing type of the Character interface. Humans have 'homePlanet', and Droids have 'primaryFunction'. The result includes these fields accordingly.
🧠 Conceptual
intermediate1:30remaining
Purpose of GraphQL Interface Types
What is the main purpose of using interface types in GraphQL schemas?
Attempts:
2 left
💡 Hint
Think about how interfaces help with querying different types that share common fields.
✗ Incorrect
Interfaces define a contract of fields that multiple types implement, allowing clients to query those fields polymorphically regardless of the concrete type.
📝 Syntax
advanced2:00remaining
Correct Interface Implementation Syntax
Which of the following GraphQL type definitions correctly implements the interface
Vehicle with fields id and speed?GraphQL
interface Vehicle {
id: ID!
speed: Int!
}Attempts:
2 left
💡 Hint
Check that all interface fields are present with correct types and syntax.
✗ Incorrect
Option D correctly implements the Vehicle interface by including all required fields with correct types and syntax. Option D has wrong syntax for implements. Option D has a nullable id which violates the interface. Option D misses the speed field.
🔧 Debug
advanced2:00remaining
Debugging Interface Query Error
Given this query:
and this schema:
Why does the query cause an error?
query {
vehicles {
id
speed
wheels
}
}and this schema:
interface Vehicle {
id: ID!
speed: Int!
}
type Car implements Vehicle {
id: ID!
speed: Int!
wheels: Int!
}
type Bike implements Vehicle {
id: ID!
speed: Int!
}Why does the query cause an error?
Attempts:
2 left
💡 Hint
Fields queried on an interface must be declared in the interface itself.
✗ Incorrect
The query tries to fetch 'wheels' directly on the interface type 'Vehicle', but 'wheels' is only defined on 'Car'. To query 'wheels', inline fragments are needed to specify the 'Car' type.
❓ optimization
expert2:30remaining
Optimizing Interface Queries with Fragments
You have a GraphQL interface
Which query is the most efficient and clear way to fetch all animals with their common and specific fields?
Animal implemented by Dog and Cat. Both have fields id and name. Dogs have barkVolume, Cats have clawSharpness.Which query is the most efficient and clear way to fetch all animals with their common and specific fields?
Attempts:
2 left
💡 Hint
Fragments help reuse fields and keep queries organized.
✗ Incorrect
Option A uses a fragment to define common and type-specific fields once, then spreads it in the query. This improves clarity and maintainability. Option A repeats fields inline. Option A queries fields not present on all types causing errors. Option A queries separate root fields, not using the interface.