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
Popular in Questions
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
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
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
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
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
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
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
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
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
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
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
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
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
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
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New







