What should be the behavior of `after_action`?

The function in after_action is being called before prepare build(load: [:roles]). Is this normal behavior?
read :get_by_subject do
description "Get a user by the subject claim in a JWT"
argument :subject, :string, allow_nil?: false
get? true
prepare AshAuthentication.Preparations.FilterBySubject

prepare build(load: [:roles])

prepare after_action(fn query, records, _context ->
dbg(records)

{:ok, records}
end)
end
end
read :get_by_subject do
description "Get a user by the subject claim in a JWT"
argument :subject, :string, allow_nil?: false
get? true
prepare AshAuthentication.Preparations.FilterBySubject

prepare build(load: [:roles])

prepare after_action(fn query, records, _context ->
dbg(records)

{:ok, records}
end)
end
end
3 Replies
ZachDaniel
ZachDaniel•3mo ago
its not that its called before it, its that the query loads aren't run until the very end the preparation happens first and mofies the query but we load the data required by the query afterwards
JVMartyns
JVMartynsOP•3mo ago
What should I do if I want to operate only after all the loads?
ZachDaniel
ZachDaniel•3mo ago
There are some complexities here that make it so that I don't suggest trying to model it the way you're thinking of it its possible to ask for different loads when calling i.e your_action(..., load: [roles: Ash.Query.filter(Role, active == true)]) Thats why we model it this way your hooks should be "pure" 😄 So what you can do is:
# prepare build(load: [:roles])

prepare after_action(fn query, records, context ->
records = Ash.load!(records, [:roles], scope: context)
# do your work

{:ok, records}
end)
# prepare build(load: [:roles])

prepare after_action(fn query, records, context ->
records = Ash.load!(records, [:roles], scope: context)
# do your work

{:ok, records}
end)

Did you find this page helpful?