Defining Enums

Preparing your DB schema and EzQL configuration file

For defining Enums in your GraphQL API you need to setup DB columns and an ezql.yml configuration file in the following way.

Let's define User.role field which allows a USER or ADMIN role.

class AddUsers < ActiveRecord::Migration[7.0]
  def change
    create_table :users do |t|
      # Define "integer" column for your enum with default value = 0.
      t.integer :role, default: 0
    end
  end
end
enums:
  # Your enum name
  Role:
    # Possible values for the "Role" enum.
    - User
    - Admin

models:
  # Name of the Model
  User:
    fields:
      # Name of the field(column)
      role:
        # Enum Name defined in "enums" section above.
        type: Role

Resulting Model Enum

Use custom value mapping for enum

With the example above, the enum column's value will be the index of each value. eg: "user = 0", "admin = 1". If you want to control what value will be used for each Enum value you can use a Map(key & value) style configuration.

Resulting Model Enums

Last updated