Enum Helper
Types::EnumHelpers builds GraphQL enum types without writing a class for each one. It ships with the cm-graphql companion library.
Quick exampleβ
field :listing_type, Types::EnumHelpers.enum_from_model(Listing, "listing_type"), null: true
enum_from_modelβ
Builds a GraphQL enum from a model's enum attribute β one value per enum key.
| Argument | Description |
|---|---|
| model | The ActiveRecord model. |
| enum name | The enum's name on the model, as a string. |
graphql_enum_name: | Optional type name. Defaults to <Model><TitleizedEnumName>Enum. |
field :listing_type, Types::EnumHelpers.enum_from_model(Listing, "listing_type"), null: true
# Override the generated type name
field :listing_type, Types::EnumHelpers.enum_from_model(Listing, "listing_type", graphql_enum_name: "MyCustomName"), null: true
enum_from_arrayβ
Builds a GraphQL enum from an array of string values β useful for values that aren't backed by a model enum, such as sortable columns.
| Argument | Description |
|---|---|
| values | Array of strings for the enum values. |
| graphql type name | The GraphQL type name. |
field :status, Types::EnumHelpers.enum_from_array(%w[draft published archived], "StatusEnum"), null: true
Recipesβ
Sort a list query by a fixed set of columnsβ
argument :column, Types::EnumHelpers.enum_from_array(%w[created_at name], 'CourseSortColumn'), required: false
argument :direction, Types::Enums::Base::SortDirection, required: false