voger

voger

Absinthe. How to set default values for enum in input type?

Hello, I wanted to create an input object with an enum type and a default value but I can’t make it work.

My query

query GetStatuses {
  statuses {
    text
    recipient {
      nickname 
    nickname
    }
    sender {
      nickname
    }
  }
}

And my resolver function head

def list_statuses(_, %{input: filters}, %{context: %{current_user: current_user}})

I am trying to start simple with one filter and add more filters later

  • First attempt


# the types module 
 input_object :statuses_privacy do
    field :privacy, :privacy
  end

  enum :privacy do
    value :public
    value :private
  end

# in the query object
  field :statuses, list_of(:status) do
     arg :input, :statuses_privacy, default_value:  :public
     resolve &Statuses.list_statuses/3
   end

This gives

Request: POST /graphql/graphiql
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Enumerable not implemented for :public of type Atom. This protocol is implemented for the following type(s): Ecto.Adapters.SQL.Stream, Postgrex.Stream, DBConnection.Stream, DBConnection.PrepareStream, HashSet, Date.Range, MapSet, IO.Stream, Map, Function, List, HashDict, Stream, GenEvent.Stream, Range, File.Stream
  • Second attempt

# in the query object
    field :statuses, list_of(:status) do
      arg :input, :statuses_privacy, default_value:  %{privacy: :public}
      resolve &Statuses.list_statuses/3
    end

This works for a query but when graphiql tries to fetch the schema I get again

Request: POST /graphql/graphiql
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol String.Chars not implemented for %{privacy: :public} of type Map. This protocol is implemented for the following type(s): Postgrex.Query, Postgrex.Copy, Decimal, Float, Date, Integer, Version.Requirement, Time, DateTime, List, BitString, NaiveDateTime, URI, Version, Atom
        (elixir 1.10.3) lib/string/chars.ex:3: String.Chars.impl_for!/1
        (elixir 1.10.3) lib/string/chars.ex:22: String.Chars.to_string/1
        (absinthe 1.4.16) lib/absinthe/type/built_ins/introspection.ex:288: anonymous fn/2 in Absinthe.Type.BuiltIns.Introspection.__absinthe_type__/1
        (absinthe 1.4.16) lib/absinthe/resolution.ex:206: Absinthe.Resolution.call/2
  • Third attempt


# in the types module
  input_object :statuses_privacy do
    field :privacy, :privacy, default_value: :public
  end

  enum :privacy do
    value :public
    value :private
  end

# in the query object
    field :statuses, list_of(:status) do
      arg :input, :statuses_privacy
      resolve &Statuses.list_statuses/3
    end

This time the input map is not passed at all in the resolver

** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in TweetCloneWeb.Resolvers.Statuses.list_statuses/3
        (tweetclone 0.1.0) lib/tweetclone_web/resolvers/statuses.ex:16: TweetCloneWeb.Resolvers.Statuses.list_statuses(%{}, %{}, #Absinthe.Resolution<[acc: %{Absinthe.Middleware.Async => false, Absinthe.Middleware.Batch => %{input: [], output: %{}}}, adapter: Absinthe.Adapter.LanguageConventions, arguments: %{}, context: %{__absinthe_plug

How can I have an input argument as a map with default values like this:

%{input: {privacy: :public}}

and default value set to :public?

Most Liked

dtrippe

dtrippe

I was able to provide a default with (using examples from the docs here)

  enum :contact_type do
    value(:phone, as: "phone")
    value(:email, as: "email")
  end
input_object :contact_input do
  field :type, non_null(:contact_type), default_value: "phone"
  field :value, non_null(:string)
 end

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement