Type error when NOT directly calling function (new to Convex 1.18.2)
I see in the update notes, it's not a good practice to call a function directly from another function.
However, when rectifying this (e.g. using
ctx.runQuery(myApiQuery)
vs myApiQuery()
I'm getting the stubborn "Function implicitly has return type 'any' because..." error.
Any idea why this is now upsetting the type system?
I feel like I'm missing something obvious!
And then I have this query:
12 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
yeah you're hitting https://docs.convex.dev/functions/actions#dealing-with-circular-type-inference .
it's recommended to call a helper function https://docs.convex.dev/production/best-practices/#use-helper-functions-to-write-shared-code instead of
ctx.runQuery
Best Practices | Convex Developer Hub
This is a list of best practices and common anti-patterns around using Convex.
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
the example in docs is kinda long, so here's a shorter example
Before:
After:
Thanks @Lee 📿 Really appreciate it. The docs are helpful!
I've got some refactoring to do across my code! Love it. Thanks again
ooh we have a more specific section https://docs.convex.dev/production/best-practices/#use-ctxrunquery-and-ctxrunmutation-sparingly-in-queries-and-mutations
Best Practices | Convex Developer Hub
This is a list of best practices and common anti-patterns around using Convex.
I'm combing through all these now. Code already feels a lot cleaner.
Is this legit in the case where I can't pass a query context? (I could also accept a mutation context?)
For example, from an action, I can't pass a usable context:
I don't get it, can you say more? @Tom Redman
Is this legit in the case where I can't pass a query context? (I could also accept a mutation context?)You can always pass a query context as an argument when you call a function Generally we spell it
QueryCtx
instead of GenericQueryCtx<DataModel>
but either works
Ah I see, yes if you're calling a query from an action, you can't just call the function; you need to use runQuery()
Cool, neat overloading thing!
This looks fine to me, is there a problem here?That
if (!ctx)
block doesn't look like it would work to me
inside of an action, you would use ctx.runQuery
and then
the reason convex is making you defined two functions (getCurrentUserOrThrow
and getCurrentUserOrThrowQuery
in my example), is because one of them is a separate transaction with argument validation, and the other is code running within another transaction@Lee yes you are right! And no, no issues @Tom (other than the bug Lee pointed out), just wanted to make sure this is still the blessed path. If query | mutation -> reuse the ctx, if it's an action, you gotta use ActionCtx.runQuery()
Btw you won't be able to call internalQuery within a convex function; it won't be registered and can't be called as internal.foo.bar unless it's exported at import-time
Here's the final form for posterity:
Is there a better way in Typescript to check if the ctx is either a QueryCtx or an ActionCtx? (Better than checking for the presence of 'db')
I believe
runAction
is the one method the action ctx has that the other two don't.