# Enums, Interfaces, Unions and Inputs — GraphQL

Source: https://www.skillbyai.com/en/graphql/s-abstract

> 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.

```graphql
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.

**Quiz:** Which type describes structured arguments to a mutation?

- [ ] A subscription
- [ ] A union
- [ ] An interface
- [x] An input type

*Answer:* An input type. Inputs are separate from output types.
