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
Recall & Review
beginner
What is an interface type in GraphQL?
An interface type in GraphQL is a way to define a set of fields that multiple object types can share. It acts like a contract that those object types must follow.
Click to reveal answer
beginner
How do you declare an interface type in GraphQL schema?
You use the interface keyword followed by the interface name and the fields it contains. For example:
interface Character { id: ID! name: String! }
Click to reveal answer
beginner
How does an object type implement an interface in GraphQL?
An object type implements an interface by using the implements keyword and including all fields defined in the interface. For example:
type Human implements Character { id: ID! name: String! homePlanet: String }
Click to reveal answer
intermediate
Why use interface types in GraphQL?
Interfaces help to reuse common fields across different object types and allow queries to be more flexible by returning different types that share the same interface.
Click to reveal answer
intermediate
Can you query an interface type directly in GraphQL?
Yes, you can query an interface type, but you must specify which fields you want from the interface and also use inline fragments to get fields specific to the implementing object types.
Click to reveal answer
What keyword is used to define an interface type in GraphQL?
Ainterface
Btype
Cimplements
Dunion
✗ Incorrect
The interface keyword is used to define an interface type in GraphQL.
If an object type implements an interface, what must it do?
AIgnore the interface fields
BOnly include some fields from the interface
CInclude all fields defined in the interface
DRename the interface fields
✗ Incorrect
An object type must include all fields defined in the interface it implements.
Which of the following is true about querying interfaces?
AInterfaces are only used for mutations
BYou cannot query interfaces directly
CYou can query an interface and get fields from all implementing types without specifying them
DYou can query an interface but need inline fragments to get fields from specific types
✗ Incorrect
When querying an interface, you specify interface fields and use inline fragments to get fields from specific implementing types.
What is a main benefit of using interface types in GraphQL?
AThey allow reuse of common fields across types
BThey speed up database queries
CThey replace unions
DThey prevent any type from having unique fields
✗ Incorrect
Interfaces allow reuse of common fields across different object types.
Which keyword does an object type use to show it follows an interface?
Ainherits
Bimplements
Cextends
Duses
✗ Incorrect
The implements keyword is used by object types to indicate they follow an interface.
Explain what an interface type is in GraphQL and why it is useful.
Think about how different characters in a story might share common traits.
You got /3 concepts.
Describe how you would query data from an interface type and its implementing object types.
Remember you need to ask for common fields and then specify extra fields for each type.
You got /3 concepts.
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
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 A
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
Step 1: Recall GraphQL interface syntax
Interfaces are declared with the keyword interface, followed by the name and fields with types.
Step 2: Check field type correctness
The field id must be non-nullable ID!, so interface Node { id: ID! } matches exactly.
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
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.
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 }.
Final Answer:
A syntax error because name is not in the interface -> Option D
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
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 the speed field required by Vehicle, causing an error.
Final Answer:
Bike type is missing the required speed field from Vehicle interface -> Option B
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
Step 1: Identify shared fields and unique fields
Both Book and Movie share id and title, 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 interface Item with id and title, then have Book and Movie implement Item and add their unique fields -> Option C