Absinthe: how to rename a field in the output

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)

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?

1 Like

I had to make this change in my types:

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

  # ... more fields
1 Like

A resolver for the field should work.

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

I think you should replace :comment with :text to match my case. Thanks for the alternative method, bookmarking it. :slight_smile:

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 Like

This won’t work because the name option is used for snakecase/camelcase renames. resolve needs to be used if the underlying key is different than the one exposed via GQL.


I like your idea, I wouldn’t have thought to do it that way @dimitarvp