Returns type

I have a actions likes this elixir

update :update_user, :update_user do
read_action :get_by_token
identity false
end
....

update :update_user do
change fn changeset, struct ->
changeset
|> Ash.Changeset.change_attribute(
:confirmed_at,
Dates.now()
)
|> Ash.Changeset.change_attribute(
:status,
:confirmed
)
|> Ash.Changeset.after_action(fn changeset, customer ->
#generate token
{:ok, %{user: user, token: token}}
end)
end

update :update_user, :update_user do
read_action :get_by_token
identity false
end
....

update :update_user do
change fn changeset, struct ->
changeset
|> Ash.Changeset.change_attribute(
:confirmed_at,
Dates.now()
)
|> Ash.Changeset.change_attribute(
:status,
:confirmed
)
|> Ash.Changeset.after_action(fn changeset, customer ->
#generate token
{:ok, %{user: user, token: token}}
end)
end
I want to override the default return type which defaults to the resource . and instead return a map type with fields user of type user resource and token of type string.
12 Replies
barnabasj
barnabasj2y ago
you could probably put it into the metadata to expose it along the resource https://ash-hq.org/docs/dsl/ash-resource#actions-update-metadata, or use a generic action https://ash-hq.org/docs/guides/ash/latest/topics/actions#generic-actions
Ash HQ
Ash.Resource
View the documentation for Ash.Resource on Ash HQ.
Ash HQ
Guide: Actions
Read the "Actions" guide on Ash HQ
barnabasj
barnabasj2y ago
Metadata is probably the best fit here IMO
edwinofdawn
edwinofdawnOP2y ago
Oh okay, I still don't how this will look like , will the API still return a user type ? If so how can the token be accessed?
barnabasj
barnabasj2y ago
It will be the user type with an extra metadata key in the action you declare the type of the metadata metadata :token, :string, allow_nil?: false in the after action you would do
Ash.Changeset.after_action(fn changeset, resource ->
#generate token
{:ok, Ash.Resource.put_metadata(resource, :token, token)}
end)
Ash.Changeset.after_action(fn changeset, resource ->
#generate token
{:ok, Ash.Resource.put_metadata(resource, :token, token)}
end)
the api will return
%User{
metadata: %{
token: <your-token>
},
other_user_attributes: ""
...
}
%User{
metadata: %{
token: <your-token>
},
other_user_attributes: ""
...
}
edwinofdawn
edwinofdawnOP2y ago
this does not work as this is a graphql API since the return type is a User the fields I query need to exist on the user.
barnabasj
barnabasj2y ago
it generates a different type for the graphql api i'm already using this with graphql
barnabasj
barnabasj2y ago
hmm, might be only implemented for reads https://ash-hq.org/docs/dsl/ash-resource#graphql-queries-get
Ash HQ
Ash.Resource
View the documentation for Ash.Resource on Ash HQ.
barnabasj
barnabasj2y ago
there are options for metadata fields No i just checked I definetly have mutations using this.
barnabasj
barnabasj2y ago
No description
barnabasj
barnabasj2y ago
No description
barnabasj
barnabasj2y ago
it is part of the result type not the user, my bad
edwinofdawn
edwinofdawnOP2y ago
this worked thanks @barnabasj

Did you find this page helpful?