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…
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…
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)
Ah… this is a v nice solution - thanks!!
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 https://hexdocs.pm/absinthe/Absinthe.Type.Field.html 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
.
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.