You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Best practice to mantain scalarSchemas option #655
Is ther a way to keep up to date the scalarSchemas option with my zodValidations in a shared package?
The only way that I can think of right now is copying & pasting my zodValidations and setting them in the scalarSchemas as a string but I think there has to be another way like generating the zod string reading the source code of another function (but at the same time that sounds like overengineering to me).
I created a scalars package in my monorepo so frontend & backend can share the scalars code. In this repo y also export my zod schemas
Then I did added an import line in the codegen generated files with the graphql-codegen add plugin and imported all my scalar package exports with an import alias
Also I configured the schalarsSchema entry for each custom schema that I have I call the mapToScalarSchemasMap to map the schema function reference to a string that is a function call to my custom schema.
Here is my codegen.ts
import{CuitScalarSchema,NameScalarSchema,PasswordScalarSchema,PermissionScalarSchema,UsernameScalarSchema}from;constGRAPHQL_SCHEMA_URL=process.env.NEXT_PUBLIC_APP_GRAPHQL_API_URL;constCODEGEN_FOLDER_PATH="./__generated/";constSCALARS_MODULE_IMPORT_ALIAS='scalars'constconfig: CodegenConfig={overwrite: true,schema: GRAPHQL_SCHEMA_URL,documents: ["./src/**/*.{ts,tsx}"],generates: {[pathInsideFolder(CODEGEN_FOLDER_PATH,"gql/client/")]: {preset: "client",plugins: [],presetConfig: {gqlTagName: "gql"}},[pathInsideFolder(CODEGEN_FOLDER_PATH,"gql/zod-schemas.ts")]: {plugins: ["typescript-validation-schema",{add: {// here add an import line in the `typescript-validation-schema` generated filescontent: `import * as ${SCALARS_MODULE_IMPORT_ALIAS} from '@package/scalars';`}}],config: {schema: "zod",importFrom: "./client/graphql",strictScalars: true,scalars: {CUIT: "string",Name: "number",Password: "string",Permission: "string",Username: "string",},scalarSchemas: {// here map each scalar with its schema reference to build a string calling that schema functionCUIT: mapToScalarSchemasMap(CuitScalarSchema),Name: mapToScalarSchemasMap(NameScalarSchema),Password: mapToScalarSchemasMap(PasswordScalarSchema),Permimssion: mapToScalarSchemasMap(PermissionScalarSchema),Username: mapToScalarSchemasMap(UsernameScalarSchema)},}}}};exportdefaultconfig;functionpathInsideFolder(folderPath: string,subpath: string): string{returnpath.join(folderPath,subpath);};functionmapToScalarSchemasMap(reference: ()=>any){return`${SCALARS_MODULE_IMPORT_ALIAS}.${reference.name}()`;}
so the generated file looks like this:
and it works!!
This way I can have all my scalar schema definition in one place and simply reuse them in frontend/backend
Reacted by Hirotaka Miyagi, Greg Westneat and Kei Kamikawa
Reacted by Hirotaka Miyagi
Repository owner locked and limited conversation to collaborators on Jul 13, 2024
Activity
MH4GF commentedon May 31, 2024
I would think that if the scalarSchemas were out of date, the type-check would give an error, but isn't that what you are talking about?
taprile314 commentedon May 31, 2024
No, that isn't what I was talking about in my issue.
taprile314 commentedon May 31, 2024
What I did to "resolve" this the following:
I created a
scalarspackage in my monorepo so frontend & backend can share the scalars code. In this repo y also export my zod schemasThen I did added an import line in the codegen generated files with the graphql-codegen add plugin and imported all my scalar package exports with an import alias
Also I configured the
schalarsSchemaentry for each custom schema that I have I call the mapToScalarSchemasMap to map the schema function reference to a string that is a function call to my custom schema.Here is my codegen.ts
so the generated file looks like this:
and it works!!
This way I can have all my scalar schema definition in one place and simply reuse them in frontend/backend