Last active
November 14, 2024 16:19
-
-
Save woylie/d83ceb0e063b904b74fd72c7d8630a19 to your computer and use it in GitHub Desktop.
Absinthe enums from Ecto enums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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