• Design
  • Shotty
  • Blog
  • Reading
  • Photos
Menu

Jacob Ruiz

Product Designer
  • Design
  • Shotty
  • Blog
  • Reading
  • Photos
graphql-notes@2x.png

GraphQL Enums

June 14, 2019

Problem:

We want a user to have one of two possible roles: BASIC or PREMIUM.

Currently, a client could potentially set the role of the user to any arbitrary string, like CHICKEN, which would obviously not make sense in our app.

We need a mechanism to establish a contract that the user.role can only be set to a pre-approved value.

Solution:

Enums

Enums are a way to create a list of pre-approved values for a certain type. In our case, we want to define an enum named Role that contains two possible values: BASIC and PREMIUM.

This mechanism tells our program that a Role type can only ever have the value BASIC or PREMIUM.

Implementation:

How to use enums to improve our role-based permission system

Step 1

Define the Role enum in the top level of the schema.

Step 2

Specify the Role enum as the return type for the role field on the User type (in schema/user.js).

Step 3

Use the Role enum as the argument type for our updateRole mutation:

This has the added benefit of giving us autocomplete in GraphQL Playground when setting the user’s role with this mutation:

Screen Shot 2019-06-14 at 1.42.23 PM.png

Sweet! Less room for error.

← Inter Is A Free, Cross-Platform Alternative To San FranciscoRole-based permissions in GraphQL →
shotty-skinny2x.jpg

Shotty - Faster Access To Your Screenshots on Mac

Shotty is an award-winning Mac app I created to give you instant access to all your recent screenshots, right from the menu bar. You can even add annotations on-the-fly. Stop wasting time digging through Finder for your screenshots. I promise it’ll change your workflow forever (just read the App Store reviews!).



Most popular

information-architecture

Information Architecture: The Most Important Part of Design You're Probably Overlooking

Follow @JacobRuizDesign