Skip to content

Add maxDepth Option to Handle Circular References in Validation Schema Generation #1039

Description

@lewisblackburn

Description:
I'm encountering issues with circular references in my GraphQL schema when using the graphql-codegen-typescript-validation-schema plugin. The generated validation schemas (e.g., for Zod) result in infinite recursion or stack overflow errors due to self-referencing types.

For example, consider the following schema:

type Movie {
  id: ID!
  title: String!
  alternativeTitles: [MovieAlternativeTitle!]!
}

type MovieAlternativeTitle {
  id: ID!
  title: String!
  movie: Movie!
}

In this case, Movie references MovieAlternativeTitle, which in turn references Movie, creating a circular dependency.

To mitigate this, I propose adding a maxDepth configuration option to the plugin. This option would allow users to specify the maximum depth for nested validation schema generation, preventing infinite recursion by limiting how deeply nested schemas are generated.

Proposed Configuration:

generates:
  path/to/validation.ts:
    plugins:
      - typescript-validation-schema
    config:
      schema: zod
      maxDepth: 1

Implementing this feature would enhance the plugin's robustness, especially for schemas with complex relationships, and align it with similar options available in other plugins like typescript-mock-data.

Thank you for considering this enhancement!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions