Skip to main content

EnumHelper

  1. Create Enum from Rails Model Enum Use enum_from_model to build a GraphQL Enum based on a model's enum attribute.
  • first argument is ActiveRecord Model Name.
  • second argument is string and name of the enum on model.
  • An optional graphql_enum_name: can be provided to customize the GraphQL type name.

NOTE: GraphQL enum names are automatically generated in the format <Model Name><Titelized Enum Name>Enum but can be customized.

examples:

field :listing_type, Types::EnumHelpers.enum_from_model(Listing, 'listing_type'), null: true
field :listing_type, Types::EnumHelpers.enum_from_model(Listing, 'listing_type', graphql_enum_name: 'MyCustomName'), null: true
  1. Create Enum from Array Use enum_from_array to build a GraphQL Enum from an array of values.
  • first argument is array of string containing enum values.
  • second argument is graphql name for this.
field :status, Types::EnumHelpers.enum_from_array(%w[draft published archived], 'StatusEnum'), null: true