Mordsith
Mordsith3mo ago

How to get the schema definitions

I need help with getting the full paths to all functions from a single api variable. I'm building a tool; with this tool, I want to pass my convex generated api variable. Within the tool, I would like to access all the properties it has. I went deep in the codebase and realized this is inferred from typescript. The actual api is a Proxy object. How do I get all the routes from a single api variable on the client side? On the server side, I can parse the api.d.ts file and do what I want. On the client, I do not have direct access to the file, is there a workaround?
12 Replies
Convex Bot
Convex Bot3mo ago
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!
Mordsith
MordsithOP3mo ago
Code Spelunking: Uncovering Convex's API Generation Secrets
As the newest member of the Convex team I am keen to find out how the magic works. Join me as I go spelunking through the codebase and uncover how the...
djbalin
djbalin3mo ago
Hm yeah doing something like Object.entries(api) just returns an empty array. I guess maybe this Proxy approach is used to make it impossible to enumerate all the api endpoints at runtime for security purposes? I guess you could build some kind of script or helper function in your convex folder to produce this array of endpoints based on the folder structure. AI would probably be really good at this, or maybe you can even find the function that Convex uses to do this to create the api object?
Mordsith
MordsithOP3mo ago
Yeah. I thought about that; since convex already does this, trying to redo this may not be ideal but it is a way out. I wish there was something to hook into this build within Convex and grab the schema. I'll go with the naive approach for now, read the api.d.ts file on the server and create a manifest JSON file @ballingt Would you mind adding your 2 cents to this? My approach is as follows: - Add a program that generates a manifest in the generated folder. The manifest is a pure array mapping function names and to their full reference.
{
‘getUserQuery: convex/routesfunctions/getUswrQuery
}
{
‘getUserQuery: convex/routesfunctions/getUswrQuery
}
The program listen to changes to the api.d.ts file, scans the file and generate the function mappings in the manifest file anytime there is a change to the api.d.ts. It scans all the imported files in api.d.ts, searches for functions that are exported that contains query, internalQuery, mutstion etc only; it ignores pure functions. (Using regex) - This would also require you run the convex dev sxript concurrently with the program’s manifest generator. convex dev & mytool-generate-manifest
The problm I have with this is that I feel like I’ll be rebuilding what is already being built but badly within convex; all the file watching etc. Is there a simpler and more efficient way out? Probably a way to hook into convex doCodegen outputs in realtime?
ballingt
ballingt3mo ago
Have you seen OpenAPI spec generation? https://docs.convex.dev/client/open-api or probably more useful, https://stack.convex.dev/multiple-repos https://docs.convex.dev/production/multiple-repos These all don't use TypeScript, they get a list of functions from the last push to dev/prod
Mordsith
MordsithOP3mo ago
Thank you @Tom The OpenAPI spec generation wouldn't work for my use-case, it's local and don't need to call HTTP endpoint; but the convex-helpers solution might be what I need. That is close to what I need
Mordsith
MordsithOP3mo ago
@Tom The api is a proxy, in my case I need a pure object and not the types; but with this, I do not have to manually traverse to get the function names, I can scan this file and generate mappings from it.
No description
Mordsith
MordsithOP3mo ago
My goal is to have a list like this on the client side like we have on the convex dashboard
No description
ballingt
ballingt3mo ago
Doesn't this openapi generation give you the names and the types?
it's local and don't need to call HTTP endpoint
your solution is local, and can't call an http endpoint? Just in case you haven't seen it, npx convex function-spec does this but isn't local; but you could run a backend locally and use the command
Mordsith
MordsithOP3mo ago
Hi @ballingt Thank you. The function-spec is exactly what I needed. Appreciate your help so far. I'm good to go. 🙏
Mordsith
MordsithOP3mo ago
Hi @ballingt , one more thing. I want to run npx convex function-spec after Convex functions ready, is there a way to hook / listen to this event from another library? The Goal: I want to regenerate the function spec only when there is a change to the convex directory (assuming something in the spec would also change)
No description
Mordsith
MordsithOP2mo ago
Hi @ballingt In case you missed this. I still need help if you come across this One approach is to listen to the stderr or stdout data events from the terminal. I'm going with that approach for now

Did you find this page helpful?