Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used before defined error on input Schemas #528

Open
lanternlogic opened this issue Dec 13, 2023 · 3 comments
Open

used before defined error on input Schemas #528

lanternlogic opened this issue Dec 13, 2023 · 3 comments

Comments

@lanternlogic
Copy link

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
Copy link
Author

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
Copy link
Owner

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants