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
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.11.0 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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









