Absinthe: How to get enum values from query?

I am trying to learn the absinthe library and I am stuck how to get the enum values from a query.

I have this type definition

defmodule Qssite.Type.Gender do
    use Absinthe.Schema.Notation

  enum :gender do
	 value :ma
	 value :fe
	 value :co
	end
   end

and in my schema in the query I have

   field :genders, list_of(:gender) do
      # what can I put in the resolver?
      # resolve &MySite.Resolver.Gender.all/2 
    end

I would like to do a query like

{
 genders
}

and get back an array. I am reading the documentation but I can’t find an obvious way to do it.

remove list_of and pass in the :gender artifact directly

    field :genders, :gender do
          # what can I put in the resolver?
          # resolve &MySite.Resolver.Gender.all/2 
        end

I just had this question myself. This is the solution I came up with:

field :genders, list_of(:gender) do
	resolve fn _, _ ->
		%{values: values} = Absinthe.Schema.lookup_type(MyApp.Schema, :gender)
        {:ok, Map.keys(values)}
	end
end

You can also directly query GraphQL with

query {
	__type(name:"EnumName") {
    name
    enumValues {
      name
    } 
  }
}
2 Likes