Thuta Learning
GraphQL
IntermediateWeb Developmentbeginner

Enums and Interfaces

What you'll walk away with

  • Explain the core ideas behind Enums and Interfaces
  • Run the sample GraphQL query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

An `enum` restricts a field to an exact set of allowed values, catching typos at the validation level instead of at runtime. An `interface` is a contract that lets multiple object types share common fields (such as `id` and `title`); a type promises to implement every interface field using the `implements` keyword. Clients can query interface fields and get common data even without knowing the concrete type.

Connect it to a real scenario

Define a `Difficulty` enum with `BEGINNER | INTERMEDIATE | ADVANCED` and use it on `Tutorial.difficulty`. Declare a `Content` interface with `id` and `title`, and have both `Tutorial` and `Exercise` implement it, so a homepage search-result list can render both content types through the same shared fields.

Try the working example

graphql
enum Difficulty {
  BEGINNER
  INTERMEDIATE
  ADVANCED
}

interface Content {
  id: ID!
  title: String!
}

type Tutorial implements Content {
  id: ID!
  title: String!
  difficulty: Difficulty!
}

type Exercise implements Content {
  id: ID!
  title: String!
  points: Int!
}
You should see
You can write enums and interfaces and share fields across types in a schema.

5-minute try-it

Write a `Status` enum (`DRAFT | PUBLISHED | ARCHIVED`) and add a `status: Status!` field to `Tutorial`.

One important caution

Changing an interface field's shape in an implementing type (rather than only narrowing return type covariantly) causes a schema validation error.

GraphQL — Schemas and TypesGraphQL

Easy traps

  • Changing an interface field's shape in an implementing type (rather than only narrowing return type covariantly) causes a schema validation error.
  • Validate sample queries and mutations on a local or test server with recoverable data before applying them to production.

Exercise

Write a `Status` enum (`DRAFT | PUBLISHED | ARCHIVED`) and add a `status: Status!` field to `Tutorial`.

You'll know it worked when: You can write enums and interfaces and share fields across types in a schema.

Enums and Interfaces | Thuta Learning