Lesson 6 / 25
Enums, Interfaces, Unions and Inputs
Modelling variety.
Richer types
Enums restrict a field to fixed values (OrderStatus). Interfaces define common fields that several types implement (Node { id: ID! }), and unions say a field returns one of several unrelated types (SearchResult = Product | User); clients use inline fragments to pick type-specific fields. Input types describe structured arguments for mutations and filters; output object types cannot be used as inputs.
Enum, interface, union and input
GraphQL SDL.
enum OrderStatus { PENDING PAID SHIPPED CANCELLED }
interface Node { id: ID! }
type Product implements Node {
id: ID!
name: String!
price: Money!
}
union SearchResult = Product | User
input ProductFilter {
category: String
maxPrice: Float
inStock: Boolean
}
input AddToCartInput {
productId: ID!
quantity: Int! = 1
}Use enums for closed sets
Enums give clients autocomplete and validation; strings invite typos.
Quick check: Which type describes structured arguments to a mutation?
- A subscription
- A union
- An interface
- An input type
Answer
An input type — Inputs are separate from output types.