dimitarvp
Absinthe: how to rename a field in the output
Hello,
I haven’t used Absinthe in a while so this might be a dumb question.
I am looking into renaming a field inside of a returned object (in response to a mutation). Let’s imagine my mutation response returns a Comment struct that looks like so:
%{
description: "",
author: "",
inserted_at: whenever
}
Now say I have this in my mutation:
output do
field(:comment, :comment)
end
How do I modify it so it can map one of the Comment fields (description) to another name (text) and have the new field name in the response?
Marked As Solved
dimitarvp
I had to make this change in my types:
object :comment do
field :description, :string do
middleware(MapGet, :text)
end
# ... more fields
end
2
Also Liked
christopherlai
A resolver for the field should work.
field :comment, :string do
resolve fn parent, _, _ ->
{:ok, Map.get(parent, :description)}
end
end
2
BartOtten
How about using an alias?
`name` - The name of the field, usually assigned automatically by the Absinthe.Schema.Notation.field/4
Including this option will bypass the snake_case to camelCase conversion.
field :description, :string, name: :text?
1
opsb
I use this little helper
field(:ingredients_description, :string, resolve: from_parent(& &1.ingredients))
def from_parent(cb) do
fn parent, _, _ ->
{:ok, cb.(parent)}
end
end
1
Popular in Questions
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Other popular topics
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
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
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New







