Skip to content

Instantly share code, notes, and snippets.

@woylie
Created November 14, 2020 10:58
Show Gist options
  • Select an option

  • Save woylie/97339b59ab996901da792cafc2662ad2 to your computer and use it in GitHub Desktop.

Select an option

Save woylie/97339b59ab996901da792cafc2662ad2 to your computer and use it in GitHub Desktop.
Absinthe enum from EctoEnum
@doc """
Defines an enum in your Absinthe schema from an enum defined with `EctoEnum`.
## Example
If you defined an enum with `EctoEnum` like this:
defmodule MyApp.Enums do
import EctoEnum
defenum Status, ["pending", "in_progress", "finished"]
end
Then you can define the same enum in your Absinthe schema like this:
defmodule CoachWeb.Types.Exercise do
use Absinthe.Schema.Notation
# change import to the module where you put the macro
import MyAppWeb.Notation
enum_from_ecto :status, MyApp.Status
end
"""
defmacro enum_from_ecto(name, module) when is_atom(name) do
name = Macro.expand(name, __CALLER__)
module = Macro.expand(module, __CALLER__)
values = Keyword.keys(module.__enum_map__())
quote do
enum(unquote(name), values: unquote(values))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment