ecto_enum
ecto_enum copied to clipboard
Dynamically generate graphql types
This would be a useful feature for users who typically use ecto with absinthe/graphql. Would this be useful to many? Feel free to share what you think.
Yes, please. Tired of duplicating enums for DB and for GrpahQL types. Already found some some inconsistencies.
I got tired of this also so I implemented the following for absinthe:
defmodule GraphQL.Enum do
defmacro ecto_enum(name, ecto_enum) do
values = Keyword.keys(Macro.expand(ecto_enum, __CALLER__).__enum_map__())
quote do
enum(unquote(name), values: unquote(values))
end
end
end
And then wherever you defined your types:
defmodule GraphQL.Types do
use Absinthe.Schema.Notation
import GraphQL.Enum, only: :macros
ecto_enum(:field, NameOfEnum)
end