GraphQL / GraphQL Schema and Types

Working with Enums and Interfaces

This tutorial will guide you through Enums and Interfaces in GraphQL. These are special types that offer more flexibility and abstraction in your GraphQL schema. You'll learn when…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Explains how to define and manage GraphQL schemas, types, and fields.

Tutorial: Working with Enums and Interfaces in GraphQL

1. Introduction

In this tutorial, we'll explore Enums and Interfaces in GraphQL. These special types provide more flexibility and abstraction in your GraphQL schema. By the end, you will understand when and how to use them for best results.

You will learn:
- What Enums and Interfaces are in GraphQL
- How to define and use them
- Best practices for working with Enums and Interfaces

Prerequisites:
- Basic understanding of GraphQL and its schema
- Basic programming knowledge

2. Step-by-Step Guide

Enums

In GraphQL, Enums, or Enumerations, are a special kind of scalar that is restricted to a specific set of allowed values. This allows you to:

  • Validate that any arguments of this type are one of the allowed values
  • Communicate through the type system that a field will always be one of a finite set of values

Here's an example:

enum Episode {
  NEWHOPE
  EMPIRE
  JEDI
}

Interfaces

Interfaces are abstract types that allow you to define fields that multiple types will have in common. They are useful when you have a group of related types that share common fields.

Here's an example:

interface Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
}

3. Code Examples

Enums Example

enum Status {
  ACTIVE
  INACTIVE
  SUSPENDED
}

type User {
  id: ID!
  name: String!
  status: Status!
}

In this example, we have a Status Enum with values ACTIVE, INACTIVE, and SUSPENDED. In the User type, the status field must be one of the values defined in the Status Enum.

Interfaces Example

interface Vehicle {
  maxSpeed: Int!
}

type Car implements Vehicle {
  maxSpeed: Int!
  seats: Int!
}

type Plane implements Vehicle {
  maxSpeed: Int!
  wingspan: Int!
}

Here, both Car and Plane types implement the Vehicle interface. Hence, they both have a maxSpeed field. Additionally, Car has a seats field and Plane has a wingspan field.

4. Summary

In this tutorial, we covered Enums and Interfaces in GraphQL. You learned what they are, how to define and use them, and some best practices.

Your next steps could include diving deeper into other special types in GraphQL, such as Unions and Input types, or exploring more advanced features like mutations and subscriptions.

5. Practice Exercises

  1. Define an Enum for days of the week and use it in a Schedule type.
  2. Create an Animal interface with fields name and age, and implement it in Dog and Cat types.

Solutions

  1. Enum for days of the week:
enum Day {
  MONDAY
  TUESDAY
  WEDNESDAY
  THURSDAY
  FRIDAY
  SATURDAY
  SUNDAY
}

type Schedule {
  day: Day!
  event: String!
}
  1. Animal interface:
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!
}

Keep practicing to understand Enums and Interfaces better. Try to use them in your own GraphQL schemas. Happy learning!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Backlink Checker

Analyze and validate backlinks.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Hex to Decimal Converter

Convert between hexadecimal and decimal values.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help