Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create GraphQL Enums with Hasura Enum Tables


    We'll tackle setting up user types by creating a users_types table with a value text primary key. We'll add the users type as a foreign key to our new table. Finally we'll insert values as a SQL query and track it as a migration.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Enums will help restrict what potential values can be inside of a particular column. Hasura has a way to set that up. Let's hypothetically say that each particular user has a specific type. We want to enforce that they're either a free account or a pro account.

    To do this, we go into our data tab. We'll create a new table. We'll call it "Users Types." This will be the potential types for potential users. For enum tables to work inside of Hasura, you need to have a singular column with a text as a primary key. This doesn't matter what it's called. It just needs to have the primary key set to a text value.

    You could then additionally add a description. That description will then inform the graphical interface to explain exactly what each specific enum actually means. We'll go ahead and add that table.

    Now that we're here, we want to fill it with data. However, generally speaking if you're developing locally and then going to apply the migrations to your staging and/or production environment, you want to say them as migrations.

    Currently, to do that we need to go into our SQL. We need to create an insert statement that will insert enums into our table. The SQL insert statement will look like this. Insert into. We'll supply our table, Users Types, and give it two columns, the value and the description.

    Then we need to supply the values that we want to insert, so values. Then we will say that we want the first enum value to be free. We'll say, "This is an unpaid account." Then we'll supply another one. We'll say, "Pro." We'll say, "This is a paid account."

    Ensure that you do single quotes here. If you do double quotes, then that will be treated as a column rather than a string value. The next thing we do is then say, "This is a migration" and give it a name so that it can save this particular thing in a understandable format.

    Doesn't really matter what it's called. Just so that you can recognize the name if you need to reference this particular migration later. We'll just call it "Run Users Types Enum." Then we'll go ahead and click run.

    With that inserted, we can go and take a look at our Users Types. We can see that there are the two types, free and pro, with the descriptions. Now we need to go to our users table. Go to modify. We're going to set up a foreign key on this type text. We'll say, "Add a foreign key." The reference table will be our Users Types.

    We'll then say that the type on our user is referring to the value on our Users Types. We'll go ahead and save that. Now with that all set up, we can go to our Users Types table. We go to our modify. We can scroll down and see that set this table as enum. It has some other table requirements that we have already talked about. I'll go ahead and toggle this on. Click OK. Now this is an enum table.

    Now in our graphical interface we can see that when we try to query for specific users, we can say, "Where the type is equal to." We have our GraphQL enums supplied by a Users Types table with a toggled-on enum.