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
endenums:
# 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: RoleResulting Model Enum
# Code generated by EzQL, DO NOT EDIT.
class User < ApplicationRecord
enum role: [:user, :admin]
endUse 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.
class AddUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
# Enum column can be "integer or string" with proper default value.
t.string :role, default: 1
end
end
endenums:
# Your enum name
Role:
# Possible values for the "Role" enum.
# We can also use "string" values, but it is not allowed to mix both "integer and string" together.
Admin: 99
User: 1
models:
# Name of the Model
User:
fields:
# Name of the field(column)
role:
# Enum Name defined in "enums" section above.
type: RoleResulting Model Enums
# Code generated by EzQL, DO NOT EDIT.
class User < ApplicationRecord
enum role: {admin: 99, user: 1}
endPreviousMany To Many (through Pivot / Junction)NextPre/Post generate scripts (eg: for Code formatter)
Last updated

