Created
November 14, 2020 10:58
-
-
Save woylie/97339b59ab996901da792cafc2662ad2 to your computer and use it in GitHub Desktop.
Absinthe enum from EctoEnum
This file contains hidden or 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 """ | |
| 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