Are there special requirements for returns :map in RPC actions?
Hi folks
I have a custom action that works perfectly in the backend (logs show correct data) but AshTypescript RPC returns empty
{}
to the frontend.
RPC Configuration:
Backend logs show correct data: %{distance_meters: 42030.1, within_range: false, ...}
and action executes successfully (no crashes). Also API call reaches backend.
But RPC response returns empty data: {"data": {}, "success": true}
Are there special requirements for returns :map in RPC actions? Or am i doing json serialization correctly?10 Replies
Wild guess, it could be because there is no type information for the keys on the map
[8:16 PM]
but I haven't looked at ash_typescript code yet.
[8:17 PM]
@Torkan will probably check in at some point.
barnabasj — 8:25 PM
both generic actions in the AshTypescript tests add constraints for the fields, so it could be that, but there also code for :map -> Record<string, any>
[8:25 PM]
But just for testing purposes you could add constraints for the :map to try if that makes it work. Like in this example
https://github.com/ash-project/ash_typescript/blob/d83b16d0919ab7c6545b4c4e850233fb30c1751b/test/support/resources/org_todo.ex#L171-L188
GitHub
ash_typescript/test/support/resources/org_todo.ex at d83b16d0919ab7...
Automatic TypeScript type generation for Ash resources and actions - ash-project/ash_typescript
[8:26 PM]
also while it shouldn't change anything putting the return type at the top, next to the action name is the more common way to do it, instead of specificying the returns in the block
GitHub
ash_typescript/test/support/resources/org_todo.ex at d83b16d0919ab7...
Automatic TypeScript type generation for Ash resources and actions - ash-project/ash_typescript
Now I responded in the wrong thread 😅
I copied it over, doesn't look that nice now, sorry about that
Hm, I'll take a look 😅
Haha no probs, always something to learn 🫂
How does the call to validateAgentLocation look in your front-end code?
or whatever it's rpc action name is set to
@Torkan we probably need to do a thing with
:map
returns and no fields where we just show everythingYeah, agreed
Adding constraints on it will definitely work though, I'd recommend doing that anyway @Zeeshan since it looks like you have a more or less fixed structure for that map, with some allow_nil? true fields
And then we'll have to add some special casing for generic actions that return untyped composite types.. The generated functions shouldn't accept
fields
, and it should just return Record<string, any>
I think just any untyped composite type
argument inputs and return values
Yes, inputs as well, although if we're lucky, those already work, I'll check soon after dealing with the returns first 😅
Ok, I've written a fix for this particular scenario now, but there are some other things that should also be addressed before it's released.. If you want to @Zeeshan you can try the main branch as a dependency in your project. When the types are regenerated that action will no longer accept the fields-arg, and the entire map will be returned in result.data
oh wow! it works 🚀 thanks for shipping muchas gracias 🫂