mirror of
https://github.com/ash-project/ash_graphql.git
synced 2024-09-20 13:24:14 +12:00
1.7 KiB
1.7 KiB
Enums
Automatically created enums
Enums are implemented automatically for any atom
attribute with a one_of
constraint. For example:
# On the resource of type `:ticket`
attribute :type, :atom, one_of: [:foo, :bar, :baz]
This would produce an enum called :ticket_type
/TicketType
.
Custom enums
You can implement a custom enum by first adding the enum type to your absinthe schema (more here). Then you can define a custom Ash type that refers to that absinthe enum type.
# In your absinthe schema:
enum :status do
value(:open, description: "The post is open")
value(:closed, description: "The post is closed")
end
# Your cusotm Ash Type
defmodule AshGraphql.Test.Status do
use Ash.Type
@values [:open, :closed]
@string_values Enum.map(@values, &to_string/1)
def graphql_input_type(_), do: :status
def graphql_type(_), do: :status
@impl true
def storage_type, do: :string
@impl true
def cast_input(value, _) when value in @values do
{:ok, value}
end
def cast_input(value, _) when is_binary(value) do
value = String.downcase(value)
if value in @string_values do
{:ok, String.to_existing_atom(value)}
else
:error
end
end
@impl true
def cast_stored(value, _) when value in @values do
{:ok, value}
end
def cast_stored(value, _) when value in @string_values do
{:ok, String.to_existing_atom(value)}
rescue
ArgumentError ->
:error
end
@impl true
def dump_to_native(value, _) when is_atom(value) do
{:ok, to_string(value)}
end
def dump_to_native(_, _), do: :error
end