s_m
s_m•3y ago

Hi The documentation states that Code

Hi! The documentation states that "Code generation isn't required to use Convex" but running the default app without an _generated fails because of imports like import { useQuery, useMutation } from '../convex/_generated/react'. I don't like generated code, so I am trying to avoid it, and when looking inside convex/_generated/react I can see useQuery = useQueryGeneric so in theory I can do import { useQueryGeneric as useQuery, useMutationGeneric as useMutation} from "convex/react"; instead, but I'm not sure I understand why this generated code is necessary in the first place: is it just for typescript (i.e: the react.d.ts) and if so, does providing my own typings in my app achieve the same goal, or is there some additional behaviour that I'll lose from not using the generated code? Thank you.
7 Replies
s_m
s_mOP•3y ago
(I should clarify that I don't like generated code that is used as part of the app. Generated code for typings is fine, so my expectation is, if the generated code is not required for anything functional, then I'd import directly from convex/react for useMutationGeneric etc. (and provide the typings as part of the app) and have the schema-based generated code (i.e: dataModel.d.ts) generated locally as a development helper but not committed and thus not a dependency).
ballingt
ballingt•3y ago
Your understanding is right, you could import useQueryGeneric directly from convex/react and you'll get the version not typed for your backend functions. The generated code is almost exclusively for types, and to avoid manual casting of useQueryGeneric and friends. one exception is _generated/clientConfig.js, if you want to avoid importing that you'll need to do some config manually (but not much) re "does providing my own typings achieve the same goal," yes, but to build those typings I'd suggest doing exactly what the generated code does; the advantage of codegen is that it stays up to date and it automatically wired up. If you don't want to use codegen but like types, the codegen'd files are a good place to start but you'll need to manually update your types every time you add a new file in convex/
s_m
s_mOP•3y ago
Great, thank you for the insight! For now, I'll stick with declaring types manually but I'll re-assess whether that purist approach is necessary once I start fleshing out the schema. A quick follow-up on the clientConfig.js: is the expectation that I am bundling the configuration with the build rather than building config-less portable apps that can take the configuration from the environment? Are there any convex-specific considerations if I swap to environment-sourced configuration (like, does not having the config at build time break anything?)? I'm guessing it's fine for me to make that change, and config at build time it's just a consequence of it being the default next.js approach, but checking anyway 🙂
ballingt
ballingt•3y ago
You can definitely swap out client config for environment variables, config is not required at build time. convex.json contains info used for deploying/pushing convex functions to Convex servers, and that's the only info needed (plus a deploy key envvar to demonstrate permission to do this). Convex expects you have two environments in a single-person project: a dev env (a convex cloud url, a set of currently deployed functions, and a database) and a prod env (all those things again). You need to choose between these yourself if you're not using clientConfig.js.
s_m
s_mOP•3y ago
https://docs.convex.dev/using/query-and-mutation-functions#environment
Convex functions have access to fewer built-in APIs than JavaScript running in browsers or in Node.js. Nearly all JavaScript global objects are available. But web APIs like fetch and window are absent, as are Node.js APIs like process and fs.
Does a way to access the environment exist within functions? would I be expected to store it in documents?
Writing Convex Functions | Convex Developer Hub
Query and mutation functions describe server behavior.
jamwt
jamwt•3y ago
Does this help, @s_m ? https://discord.com/channels/1019350475847499849/1031791000835866626 <- Recent thread when another developer asked about environment variables in Convex functions. (1st class secrets support is still on the near-term roadmap)
ian
ian•3y ago
Also stay tuned for 1st class environment variable support for convex functions (server-side) coming out soon.

Did you find this page helpful?