Code-Hex / graphql-codegen-typescript-validation-schema

GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema
MIT License
314 stars 41 forks source link

Support for `lazy` #599

Open sschneider-ihre-pvs opened 6 months ago

sschneider-ihre-pvs commented 6 months ago

There is a chance that there will be some self referencing group going on which is currently not supported but is possible in Zod for example

This leads to callstack size exceeded

export function GroupSchema {
  return z.object({
    name: z.string(),
    subGroup:  GroupSchema()
  })
}

This does not

export function GroupSchema {
  return z.object({
    name: z.string(),
    subGroup: z.lazy(() => GroupSchema())
  })
}
Code-Hex commented 6 months ago

@sschneider-ihre-pvs Thanks! Yeah, I agree fix this if possible to keep returning type the same as current.

Could you send me PR for this?

elierotenberg commented 2 months ago

I am interested in providing a fix for this. I think we must do 2 things:

  1. Use z.lazy. We can try and detect recursive references (by implementing a DFS), or simply use z.lazy everywhere. There isn't really a downside to the latter.
  2. Memoize calls to the schema generating functions (using let xxxSchema: null | ZodType<xxxx> for each function should be enough) so that multiple calls returns the same reference.

I can get started by hacking my way through the generator but any help/advice would be appreciated!