Var&
Var&•14mo ago

fetch external resources while inside query

I'm trying to fetch additional data from external resource and merge it with query data to form the final data that gets sent back to client. For that I'm trying to use internalAction but seems like I can't figure out how to implement it? So I have the following questions: Am I on the right track of trying to use internalAction inside a query? if not, how can I achieve this? take this as an example. I query a table and each record has an ID that the full data of that ID is in external resource. Now my challenge is to privately fetch the data for those IDs and stitch it with query result and send back to client ( btw the query result is small number maybe < 20 records) Any help would be appreciated.
4 Replies
jamwt
jamwt•14mo ago
@Var& hi! unfortunately, you can't fetch from outside resources during the query. you can (1) use an action to fetch all those resources and save them into convex in a mutation; or (2) you can have the query return URLs or whatever is necessary for the browser to fetch those resources. but queries are pure functions that cannot integrate outside data
Var&
Var&OP•14mo ago
Thank you for getting back to me. ok I'll think about any of those solutions. I'm using convex in my expo app. and the use case is that I need to fetch user data while returning table records to the client. would it be possible if I convert the query into an action and then use internal action to accomplish this?
jamwt
jamwt•14mo ago
yes, actions can have side effects. so that would work if you want to do the merge server-side keep your query have the action use runQuery, and then have the action merge in the external data from APIs the you can invoke this action (not internalAction) from your app and use the return value
Var&
Var&OP•14mo ago
🙌

Did you find this page helpful?