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

used before defined error on input Schemas

lanternlogic opened this issue ยท 8 comments

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!

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.

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.

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.

I was able to workaround this by modifying the class names, for some reason the schema generator is affected by lexical order
using function was not a good solution for us since it would make us rewrite a lot of code

@Code-Hex any fix on the horizon? ๐Ÿ˜…

@royhadad as stop gap, downgrading to version 0.10.0 worked like a charm for me.

There were two related issues in the visit invocation in topologicalSortAST that caused the problem:

  • For nodes of kind "ObjectTypeDefinition" or "InputObjectTypeDefinition", the nodes were not being unwrapped to get the underlying "NamedType" node. This was the primary cause.
  • Nodes of kind "InterfaceTypeDefinition" were not handled.

The fix ๐Ÿ‘‰ #958

The PR also updates the topsort algo implementation to handle all nodes โ€” not just sinks.

I just published the latest version https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/releases/tag/v0.17.1

Please try this version, if you have any issues feel free re-open this issue