dimitarvp

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

dimitarvp

I had to make this change in my types:

object :comment do
  field :description, :string do
    middleware(MapGet, :text)
  end

  # ... more fields
end

Also Liked

christopherlai

christopherlai

A resolver for the field should work.

field :comment, :string do
  resolve fn parent, _, _ ->
    {:ok, Map.get(parent, :description)}
  end
end
BartOtten

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?

opsb

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

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
Fl4m3Ph03n1x
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
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
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement