Skip to content

used before defined error on input Schemas #528

@lanternlogic

Description

@lanternlogic

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions