mmport80

mmport80

Absinthe Translation Field Names to DB Column Names

Is there a neat way to convert,

field(:profilename, non_null(:string))

to :name for example in the map sent to the resolver, in order to match backend concerns - e.g. the corresponding DB column?

I suspect not, but no harm in trying…

Marked As Solved

marisradu

marisradu

The easiest solution that seemed to work is to add a :name to the field definition:

field(:profilename, non_null(:string), name: "name")

It’s not very clear from the docs but you can check out the field docs Absinthe.Type.Field — absinthe v1.7.6 where it states:

:name - The name of the field, usually assigned automatically by the Absinthe.Schema.Notation.field/1.

Now the exposed field will be name while the one getting into resolvers will be profilename acting like a true alias.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Sure! Just use a resolver:

field :profile_name, non_null(:string), resolve: fn parent, _, _ ->
  {:ok, Map.get(parent, :name)}
end

If this is a common pattern you can create a little helper function:

def key(k) do
  fn parent, _, _ -> {:ok, Map.get(parent, k)} end
end

field :profile_name, non_null(:string), resolve: key(:name)
marisradu

marisradu

I noticed that you’re using this for aliasing the database columns. In case you’re using Ecto is even easier to do this by letting Ecto take care of the column alias in the field definition by using the source argument like this:

field(:name, :string, source: :profilename)

where :profilename is the column in the database while the :name is the one exposed by Ecto to Absinthe.

Where Next?

Popular in Questions Top

srinivasu
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
Tee
can someone please explain to me how Enum.reduce works with maps
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
srinivasu
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
polypush135
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
freewebwithme
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
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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 Postg...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

We're in Beta

About us Mission Statement