ecto_enum icon indicating copy to clipboard operation
ecto_enum copied to clipboard

Dynamically generate graphql types

Open gjaldon opened this issue 5 years ago • 2 comments

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.

gjaldon avatar Jun 27 '19 13:06 gjaldon

Yes, please. Tired of duplicating enums for DB and for GrpahQL types. Already found some some inconsistencies.

cospin avatar Feb 17 '20 08:02 cospin

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

coop avatar May 18 '20 04:05 coop