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

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

used before defined error on input Schemas #528

Open lanternlogic opened 9 months ago

lanternlogic commented 9 months ago

I am having a strange issue where generated input Schema generates Zod objects in the wrong order, resulting in a "is used before being assigned" error.

Server Setup package.json:

Node Version: 18
"zod": "^3.22.4"
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"@graphql-codegen/typescript-resolvers": "^4.0.1",
"@parcel/watcher": "^2.1.0",
"@types/node": "^20.10.4",
"graphql-codegen-typescript-validation-schema": "^0.12.1",

codegen.yml:

schema: ./src/schema.graphql
generates:
  ./src/__generatedTypes__.ts:
    plugins:
      - typescript
      - typescript-resolvers
      - typescript-validation-schema
    config:
      # Ensure compatibility with Apollo Server typedefs
      useIndexSignature: true
      contextType: ./Context#Context
      mappers:
        Project: shared#Project as ProjectModel
      validationSchemaExportType: const
      strictScalars: true
      scalars:
        ID: string
      schema: zod
      directives:
        constraint:
          maxLength: max
          pattern: ["regex", "/$1/"]

...with relevant part of my GraphQL schema:

input ProvisionParam {
  Key: String!
  Value: String!
}

input TagUpdateInput {
  arns: [String!]!
  project: String
  account: String
  tags: [ProvisionParam!]!
}

Given the above, the generated code appears as such:

export const TagUpdateInputSchema: z.ZodObject<Properties<TagUpdateInput>> = z.object({
    account: z.string().nullish(),
    arns: z.array(z.string()),
    project: z.string().nullish(),
    tags: z.array(ProvisionParamSchema)
});

export const ProvisionParamSchema: z.ZodObject<Properties<ProvisionParam>> = z.object({
    Key: z.string(),
    Value: z.string()
}); 

and of course, because ProvisionParamSchema is referenced before its defined, an error is thrown.

Is there a reason for why this is happening? It seems to only be happening after updating graphql-codegen-typescript-validation-schema to keep up-to-date with me updating @graphql-codegen package.

Any help would be greatly appreciated!

lanternlogic commented 9 months ago

Update: It seems that 0.11.0 has a bug in it where generated Schemas can be out of order, as mentioned above. I was able to get it working simply by downgrading to version 0.10.0.

Code-Hex commented 2 months ago

Sorry, I omitted to reply. Does this problem still occur? If it does occur, it would be appreciated if you could provide a minimum reproduction, e.g. using codesandbox.

ivan-silva commented 4 days ago

Hi @Code-Hex, I confirm in version 1.16.0 the problem is still present. Switching to validationSchemaExportType: 'function' solves the problem but with const the problem persists. I try to give you a repro.