punn
punn13mo ago

The right way to use makeFunctionReference

I'd like to create a function reference and store it in a array (while outside of the client), and use a client to consume the references and make the fn calls. what's the best way to do this?
4 Replies
ballingt
ballingt13mo ago
@punn can you say more about this, e.g. do you want to store a bunch of Convex function calls and make them later? like
const queries = [
[api.users.list, { friends: true }],
[api.users.list, { friends; false}],
[api.messages.list, {}]
];
const queries = [
[api.users.list, { friends: true }],
[api.users.list, { friends; false}],
[api.messages.list, {}]
];
For many cases the function references you want already exist on the api object, but if you don't have that api object for some reason (say it's a different repo and can't make the server code a dependency) then you need to create function references.
Michal Srb
Michal Srb13mo ago
Hey @punn , check out the getFunctionName and makeFunctionReference functions in the convex package. They serialize and deserialize function references to/from strings.
punn
punnOP13mo ago
Yes that's exactly what I want to do. I'm defining api.users.list in one class and consuming it in another. This results in object is not a functionReference error. However, if I define api.users.list in the consumer class, it works fine. fyi, this works across classes/files and simply listing the objects api.users.list does not.
Michal Srb
Michal Srb13mo ago
in one class and consuming it in another
I imagine there's something more going on: Some serialization, perhaps across page reloads or across the server-client boundary - and this breaks the object reference. As long as you're in the same JS execution context, the references (api.foo.bla) should work fine when passed around and stored in memory.

Did you find this page helpful?