dimitarvp

dimitarvp

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?

Showing Posts 1 to 7

dimitarvp

dimitarvp OP

I had to make this change in my types:

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

  # ... more fields
end
christopherlai

christopherlai

A resolver for the field should work.

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

dimitarvp OP

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

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?

christopherlai

christopherlai

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.

mattbaker

mattbaker

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

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews