djbalinD
Convex Community2y ago
4 replies
djbalin

[Module: server/TS]: Generic query pipelining

Hello, I am trying to build a custom react hook that contains a "pipeline" of Convex functions. The pipeline is simple: Query A returns an array of Id<Table>, and Query B accepts an array of Id<Table> and enriches them (basically performing joins with other tables):
() => getDocumentIds --> (Ids) => enrichDocuments(Ids)

I want to make this hook generic such that it can be used with any compatible getter/enrichment function pair, e.g. both getVideoIds --> enrichVideos and getChannelIds --> enrichChannels. Therefore, the return type of Query A must be compatible with the parameter type of Query B. I have taken inspiration from the implementation of
useQuery
and have actually made it work in practice, but not without ignoring a TS error - not very peace of mind-y.
I found that specifying Args in the FunctionReference of QueryB approximated the solution, but I had to manually specify the correct object key (e.g. "videoIds"). Is there a way to use some placeholder as such?: { PLACEHOLDER: FunctionReturnType<BaseQuery> }
I have attached screenshots of my code until now, the TS error, and usage in the front-end (which works).

I know this post may be confusing, and that it may not be clear what I am actually asking about or trying to achieve, and I apologize for that. I am a bit out of my depth TS-wise, but I hope someone understands what I'm trying to do and has some ideas! Thank you very much.

TL;DR

How can I write a generic custom hook which takes as parameters QueryA, QueryA['_args'] as well asQueryB in order to invoke in a generic way convex.query(query: QueryB, ...args: FunctionReturnType<QueryA> ? A simplifying concern is that I know as the "human" that any QueryB I want to pass to this hook will only have one argument of the same type as the return value of QueryA (an array of document Ids)
image.png
image.png
image.png
Was this page helpful?