0
0
GraphQLquery~20 mins

Interface types in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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
    }
  }
}
ASyntaxError: Cannot query field 'homePlanet' on type 'Droid'
B[{"id": "1", "name": "Luke Skywalker"}, {"id": "2", "name": "R2-D2"}]
C[{"id": "1", "name": "Luke Skywalker", "homePlanet": "Tatooine"}, {"id": "2", "name": "R2-D2", "primaryFunction": "Astromech"}]
D[{"id": "1", "name": "Luke Skywalker", "primaryFunction": "Astromech"}, {"id": "2", "name": "R2-D2", "homePlanet": "Tatooine"}]
Attempts:
2 left
💡 Hint
Remember that inline fragments allow querying fields specific to the implementing types.
🧠 Conceptual
intermediate
1:30remaining
Purpose of GraphQL Interface Types
What is the main purpose of using interface types in GraphQL schemas?
ATo enforce that only one type can exist in the schema with certain fields.
BTo define a set of fields that multiple types must implement, enabling polymorphic queries.
CTo create a union of unrelated types without shared fields.
DTo restrict queries to only scalar fields.
Attempts:
2 left
💡 Hint
Think about how interfaces help with querying different types that share common fields.
📝 Syntax
advanced
2: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!
}
A
type Car implements Vehicle {
  id: ID
  speed: Int!
  wheels: Int!
}
B
type Car {
  id: ID!
  speed: Int!
  wheels: Int!
  implements Vehicle
}
C
type Car implements Vehicle {
  id: ID!
  wheels: Int!
}
D
type Car implements Vehicle {
  id: ID!
  speed: Int!
  wheels: Int!
}
Attempts:
2 left
💡 Hint
Check that all interface fields are present with correct types and syntax.
🔧 Debug
advanced
2:00remaining
Debugging Interface Query Error
Given this query:
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?
ABecause 'wheels' is not a field on the Vehicle interface and cannot be queried directly on the interface type.
BBecause the interface Vehicle must include the 'wheels' field.
CBecause the query is missing inline fragments to specify type-specific fields.
DBecause 'speed' is missing from the Bike type implementation.
Attempts:
2 left
💡 Hint
Fields queried on an interface must be declared in the interface itself.
optimization
expert
2:30remaining
Optimizing Interface Queries with Fragments
You have a GraphQL interface 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?
A
query {
  animals {
    ...AnimalFields
  }
}

fragment AnimalFields on Animal {
  id
  name
  ... on Dog {
    barkVolume
  }
  ... on Cat {
    clawSharpness
  }
}
B
query {
  animals {
    id
    name
    ... on Dog {
      barkVolume
    }
    ... on Cat {
      clawSharpness
    }
  }
}
C
query {
  animals {
    id
    name
    barkVolume
    clawSharpness
  }
}
D
query {
  animals {
    id
    name
  }
  dogs {
    barkVolume
  }
  cats {
    clawSharpness
  }
}
Attempts:
2 left
💡 Hint
Fragments help reuse fields and keep queries organized.