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…
Section overview
5 resourcesExplains 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
- Define an Enum for days of the week and use it in a
Scheduletype. - Create an
Animalinterface with fieldsnameandage, and implement it inDogandCattypes.
Solutions
- Enum for days of the week:
enum Day {
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
}
type Schedule {
day: Day!
event: String!
}
Animalinterface:
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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article