Skip to main content

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.

ArgumentDescription
modelThe ActiveRecord model.
enum nameThe 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.

ArgumentDescription
valuesArray of strings for the enum values.
graphql type nameThe 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