Proxying actions to other actions
I've found it a useful pattern to do some transformations that then proxy the remainder of the work to another action—often the primary action of its type.
The way I'm doing that is roughly:
I've started getting a lot of the following:
Is there a preferred way to execute this pattern? It does seem like I'm doing something quite like what the message recommends.
37 Replies
So the problem is that you're returning a changeset for the wrong action
What you want is a manual action
would be one way to go about it
There is generally no need to use
Changeset.new
Tbh I just added that to check if it would silence the warning 😂
Ok, so it would be better to use a manual for this, that's fair. I do recall thinking it seemed a little suspect to return a changeset for a different action 🙂
Would this still need
Map.to_list ctx
to thread through the calling context, or is that happening somewhere else?Yeah, you would still need that kind of thing
Right on, cheers!
Might need to make a helper for doing that kind of thing
because if we ever add a key to the context that isn't a valid option to new changesets, it would break that code
Thanks for the warning, I'll start wrapping them up from now on.
Yeah, maybe do something like
Map.take(...) |> Map.to_list()
and take the keys you know aboutActually I was experimenting with several options for this at one point; one of the reasons I ended up on this was because if I do it the other way, I'm getting:
Yep, so if you add
return_notifications?: true
option, it will return them alongside the actual result
lemme check something real quick
Yeah so you can return {:ok, result, %{notifications: notifications}}
from manual create
So you'd do something like:
I'm going to fix the need for that before too long, we can automatically gather up notifications
Just need to do it 🙂 Might be a 3.0 thingNice! That would be great.
I'm still getting the same warning, though, wondering if it might be somewhere else:
Wait a sec
Thought it might be coming from the error branch, but seems ok:
🤔 Does it give you a stacktrace in the warning?
Yep
I'll tell you hwat
I'll just make it all unnecessary and make the internals gather up notifications magically 🙂
Works for me 😁
Thanks mate
can you try main?
It should handle the notifications automatically now
Yep, just a minute
Don't forget to remove your code that does
return_notifications?
Already gone
Now I'm getting the warning twice :thinkies:
(for different actions, though)
🤔
but you weren't getting it before for those actions?
The second warning? Nope.
But it looks like everything is working otherwise.
Sorry, still a bit unclear. Did you used to get warnings from those actions at all?
So was it 1 and now its 2? Or was it 0 and now its 2?
I wasn't getting notification warnings lately, since I had them as
During the thread I've switched one path over to using code interface rather than that, but before it was producing only one warning.
The two warnings it creates are for the actions I'd expect the path to take, though, so good possibility the one warning was worse.
can you lay it all out? like what actions you're talking about, how you're invoking them?
are you using
return_notifications?
anywhere?I had only added it to the one path as well, and removed it.
This is the util for threading context.
The
manual
actions are returning from the code interface, like:
Error conditions are being returned so:
The vast majority of the actions are still create
, update
, etc. and not manual
as we spoke about above.
(where possible)I just pushed up another thing to main
mix deps.update ash
try updating it and see if that helpsSure, 2 new commits?
yep
Same 2
The new warning is coming from this action:
(Which is called by the first action)
that doesn't make any sense 😆
that action isn't even doing anything
its just returning a struct
can I see how the first action is calling it?
Just a sec
I have an example in my tests that does the same thing essentially (calls a nested action from within an action) and its not showing that warning
That's the shape of it.
The
get
is unsurprisingly fine, the action call issuing a warning is the second action in the with
.
This action is being called via code interface, and warnings are coming from both Post.create
and Category.proxy
.
This action is not generating a warning
Gonna try something
No diceAre you creating a transaction manually above that?
like
Repo.transaction
?Nope, just calling that last action via code interface.
And the warning points at that
Post.create
I'll have to take a look later thenYep,
Post.create
and Category.proxy
No worries 🙂Found the problem 🙂
will fix it later tonight or tomorrow 🙂
Fixed in main 🙂