Interface types let you define a common set of fields that different data types share. This helps organize data and makes queries simpler.
Interface types in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
GraphQL
interface InterfaceName {
field1: Type1
field2: Type2
}
type TypeA implements InterfaceName {
field1: Type1
field2: Type2
extraField: ExtraType
}
type TypeB implements InterfaceName {
field1: Type1
field2: Type2
anotherField: AnotherType
}Interfaces define fields without specifying how they work.
Types that implement the interface must include all its fields.
Examples
Animal with two fields. Both Dog and Cat types implement it and add their own fields.GraphQL
interface Animal {
name: String
age: Int
}
type Dog implements Animal {
name: String
age: Int
breed: String
}
type Cat implements Animal {
name: String
age: Int
color: String
}Vehicle interface is shared by Car and Bike. Each type adds unique fields.GraphQL
interface Vehicle {
make: String
model: String
}
type Car implements Vehicle {
make: String
model: String
doors: Int
}
type Bike implements Vehicle {
make: String
model: String
type: String
}Sample Program
This example defines a Person interface with id and name. Both Employee and Customer implement it. The query fetches all people showing only the shared fields.
GraphQL
interface Person {
id: ID!
name: String!
}
type Employee implements Person {
id: ID!
name: String!
salary: Float
}
type Customer implements Person {
id: ID!
name: String!
purchaseCount: Int
}
# Query to get all persons with their common fields
query {
people {
id
name
}
}Important Notes
Interfaces help keep your API organized and consistent.
You can use fragments in queries to get fields from specific types implementing an interface.
Summary
Interfaces define shared fields for multiple types.
Types must implement all interface fields.
Use interfaces to write flexible and reusable queries.
Practice
1. What is the main purpose of an
interface in GraphQL?easy
Solution
Step 1: Understand the role of interfaces
Interfaces in GraphQL define common fields that multiple types share.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.Final Answer:
To define a set of fields that multiple types must implement -> Option AQuick 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
Solution
Step 1: Recall GraphQL interface syntax
Interfaces are declared with the keywordinterface, followed by the name and fields with types.Step 2: Check field type correctness
The fieldidmust be non-nullableID!, so interface Node { id: ID! } matches exactly.Final Answer:
interface Node { id: ID! } -> Option AQuick 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
Solution
Step 1: Review the interface definition
The SearchResult interface only definesid: ID!. Thenamefield is present in implementing types but not in the interface.Step 2: Analyze the query against the interface
Queryingnamedirectly onsearch(SearchResult) fails because it is not defined on the interface. GraphQL requires inline fragments for type-specific fields like... on User { name }.Final Answer:
A syntax error becausenameis not in the interface -> Option DQuick 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
Solution
Step 1: Check interface field requirements
All types implementing an interface must have all interface fields with matching types.Step 2: Verify Bike type fields
Bike implements Vehicle but lacks thespeedfield required by Vehicle, causing an error.Final Answer:
Bike type is missing the requiredspeedfield from Vehicle interface -> Option BQuick 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
Solution
Step 1: Identify shared fields and unique fields
Both Book and Movie shareidandtitle, but have unique fields.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.Step 3: Add unique fields in each type
Book and Movie can add their own fields beyond the interface.Final Answer:
Define an interfaceItemwithidandtitle, then haveBookandMovieimplementItemand add their unique fields -> Option CQuick 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
