Bird
Raised Fist0
GraphQLquery~20 mins

Interface types in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of an interface in GraphQL?
easy
A. To define a set of fields that multiple types must implement
B. To create a new scalar type
C. To specify a query operation
D. To define a mutation resolver

Solution

  1. Step 1: Understand the role of interfaces

    Interfaces in GraphQL define common fields that multiple types share.
  2. Step 2: Compare options with interface purpose

    Only To define a set of fields that multiple types must implement correctly states that interfaces define shared fields for multiple types.
  3. Final Answer:

    To define a set of fields that multiple types must implement -> Option A
  4. Quick Check:

    Interface = shared fields [OK]
Hint: Interfaces define shared fields for multiple types [OK]
Common Mistakes:
  • Confusing interfaces with scalar types
  • Thinking interfaces define queries or mutations
  • Assuming interfaces can be instantiated directly
2. Which of the following is the correct syntax to declare an interface named Node with a field id of type ID!?
easy
A. interface Node { id: ID! }
B. interface Node { id: ID }
C. interface Node id: ID!
D. interface Node { id: String! }

Solution

  1. Step 1: Recall GraphQL interface syntax

    Interfaces are declared with the keyword interface, followed by the name and fields with types.
  2. Step 2: Check field type correctness

    The field id must be non-nullable ID!, so interface Node { id: ID! } matches exactly.
  3. Final Answer:

    interface Node { id: ID! } -> Option A
  4. Quick Check:

    Correct interface syntax = interface Node { id: ID! } [OK]
Hint: Use 'interface Name { field: Type! }' syntax exactly [OK]
Common Mistakes:
  • Omitting the exclamation mark for non-nullable
  • Using wrong scalar type like String instead of ID
  • Missing braces around fields
3. Given the interface and types below, what will the query { search { id name } return?
interface SearchResult {
  id: ID!
}
type User implements SearchResult {
  id: ID!
  name: String!
}
type Product implements SearchResult {
  id: ID!
  name: String!
  price: Float!
}
medium
A. A list of objects each with fields id and name
B. A list of objects with only id fields
C. An error because price is missing in the interface
D. A syntax error because name is not in the interface

Solution

  1. Step 1: Review the interface definition

    The SearchResult interface only defines id: ID!. The name field is present in implementing types but not in the interface.
  2. Step 2: Analyze the query against the interface

    Querying name directly on search (SearchResult) fails because it is not defined on the interface. GraphQL requires inline fragments for type-specific fields like ... on User { name }.
  3. Final Answer:

    A syntax error because name is not in the interface -> Option D
  4. Quick Check:

    Directly query only interface fields; use fragments for type-specific [OK]
Hint: Query only interface fields directly; use fragments for type-specific fields [OK]
Common Mistakes:
  • Assuming only interface fields can be queried
  • Expecting error if extra fields exist in types
  • Confusing interface fields with type-specific fields
4. Consider the following schema snippet:
interface Vehicle {
  id: ID!
  speed: Int!
}
type Car implements Vehicle {
  id: ID!
  speed: Int!
  brand: String!
}
type Bike implements Vehicle {
  id: ID!
  brand: String!
}
What is the error in this schema?
medium
A. Car type has an extra field brand not in Vehicle interface
B. Bike type is missing the required speed field from Vehicle interface
C. Vehicle interface cannot have fields of type Int
D. Interface Vehicle must be a type, not an interface

Solution

  1. Step 1: Check interface field requirements

    All types implementing an interface must have all interface fields with matching types.
  2. Step 2: Verify Bike type fields

    Bike implements Vehicle but lacks the speed field required by Vehicle, causing an error.
  3. Final Answer:

    Bike type is missing the required speed field from Vehicle interface -> Option B
  4. Quick Check:

    Implementing types must have all interface fields [OK]
Hint: Check all interface fields are implemented in each type [OK]
Common Mistakes:
  • Thinking extra fields in types cause errors
  • Ignoring missing interface fields in types
  • Confusing interface with type declaration rules
5. You want to design a GraphQL schema where multiple types like Book and Movie share fields id and title, but each has unique fields too. How should you use interfaces to achieve this?
hard
A. Use scalar types for id and title in each type separately
B. Create a union type of Book and Movie without shared fields
C. Define an interface Item with id and title, then have Book and Movie implement Item and add their unique fields
D. Define Book and Movie as separate types without interfaces

Solution

  1. Step 1: Identify shared fields and unique fields

    Both Book and Movie share id and title, but have unique fields.
  2. Step 2: Use interface for shared fields

    Defining an interface Item with shared fields and implementing it in Book and Movie allows reuse and flexibility.
  3. Step 3: Add unique fields in each type

    Book and Movie can add their own fields beyond the interface.
  4. Final Answer:

    Define an interface Item with id and title, then have Book and Movie implement Item and add their unique fields -> Option C
  5. Quick Check:

    Interfaces = shared fields + types add unique fields [OK]
Hint: Use interfaces for shared fields, types add unique fields [OK]
Common Mistakes:
  • Using union types instead of interfaces for shared fields
  • Duplicating shared fields in each type without interface
  • Misusing scalar types for shared fields