Skip to content

Instantly share code, notes, and snippets.

@woylie
Last active November 14, 2024 16:19
Show Gist options
  • Save woylie/d83ceb0e063b904b74fd72c7d8630a19 to your computer and use it in GitHub Desktop.
Save woylie/d83ceb0e063b904b74fd72c7d8630a19 to your computer and use it in GitHub Desktop.
Absinthe enums from Ecto enums
@doc """
Takes a list of Ecto.Enum fields and converts them into Absinthe enum
types.
## Usage
The list consists of tuples with the name of the GraphQL enum, the Ecto schema
module and the schema field using `Ecto.Enum`.
use Absinthe.Schema.Notation
import ModuleThatDefinesMacro
absinthe_enums_from_ecto_enums([
{:type_name, MyApp.SomeSchema, :some_field}
])
"""
defmacro absinthe_enums_from_ecto_enums(enums) when is_list(enums) do
for {_, _, [name, module, field]} <- enums do
module = Macro.expand(module, __CALLER__)
values = Ecto.Enum.values(module, field)
quote do
enum(unquote(name), values: unquote(values))
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment