0
0
GraphQLquery~30 mins

Enum types in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
GraphQL Enum Types for User Roles
📖 Scenario: You are building a user management system where each user has a specific role. Roles can be ADMIN, EDITOR, or VIEWER. You want to use GraphQL enum types to define these roles clearly in your schema.
🎯 Goal: Create a GraphQL schema that defines an enum type called UserRole with the values ADMIN, EDITOR, and VIEWER. Then use this enum type in a User type to specify the user's role.
📋 What You'll Learn
Define an enum type called UserRole with values ADMIN, EDITOR, and VIEWER.
Create a User type with fields id (ID!), name (String!), and role (UserRole!).
Add a query type users that returns a list of User objects.
💡 Why This Matters
🌍 Real World
Enum types in GraphQL help define fixed sets of options, like user roles, status codes, or categories, making APIs clearer and safer.
💼 Career
Understanding enums is essential for backend developers working with GraphQL APIs to enforce valid data and improve API design.
Progress0 / 4 steps
1
Define the UserRole enum type
Create a GraphQL enum type called UserRole with the values ADMIN, EDITOR, and VIEWER.
GraphQL
Need a hint?

Use the enum keyword followed by the enum name and list the values inside curly braces.

2
Create the User type using UserRole enum
Create a GraphQL type called User with fields id of type ID!, name of type String!, and role of type UserRole!.
GraphQL
Need a hint?

Define the User type with the specified fields and use the UserRole enum for the role field.

3
Add a users query returning a list of User
Add a Query type with a field called users that returns a list of User objects. Use the syntax [User!]! to indicate a non-null list of non-null users.
GraphQL
Need a hint?

Define the Query type with a users field that returns a list of User objects.

4
Complete the schema with schema definition
Add the schema definition specifying that the query root type is Query.
GraphQL
Need a hint?

Use the schema keyword to define the root query type.