question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

enums don't seem to use the schema when they are properties in Zod

See original GitHub issue

The below types are generated.

/** The publications tags sort criteria */
export const TagSortCriteria = {
  Alphabetical: 'ALPHABETICAL',
  MostPopular: 'MOST_POPULAR',
} as const;

export type TagSortCriteria = typeof TagSortCriteria[keyof typeof TagSortCriteria];

export const TagSortCriteriaSchema = z.nativeEnum(TagSortCriteria);

export type AllPublicationsTagsRequest = {
  cursor: InputMaybe<Scalars['Cursor']>;
  limit: InputMaybe<Scalars['LimitScalar']>;
  sort: TagSortCriteria;
  /** The App Id */
  source: InputMaybe<Scalars['Sources']>;
};

However it seems to generate AllPublicationsTagsRequest with the wrong schema. I would expect it to use TagSortCriteriaSchema as per your examples. I’m not sure if i have a wrong config.

export type AllPublicationsTagsRequest = {
  cursor: InputMaybe<Scalars['Cursor']>;
  limit: InputMaybe<Scalars['LimitScalar']>;
  sort: TagSortCriteria;
  /** The App Id */
  source: InputMaybe<Scalars['Sources']>;
};

this is my config

const config: CodegenConfig = {
  schema: 'https://api-sandbox-mumbai.lens.dev/',
  documents: ['src/**/*.ts', '!src/generated/**/*'],
  ignoreNoDocuments: true, // for better experience with the watcher
  generates: {
    './src/generated/gql/gql.ts': {
      // preset: [],
      plugins: [
        'typescript',
        'typescript-operations',
        'typescript-graphql-request',
        'typescript-react-query',
        'typescript-validation-schema',
        {
          add: {
            content: '/* eslint-disable */',
          },
        },
        {
          add: {
            content: 'import { ethers } from "ethers"',
          },
        },
        {
          add: {
            content: ' ',
          },
        },
      ],
      config: {
        enumsAsConst: true,
        maybeValue: 'T | undefined | null',
        nonOptionalTypename: true,
        useTypenameImports: true,
        avoidOptionals: true,
        schema: 'zod',
        //strictScalars: true,
        scalars: scalarDefinition,
        enumAsTypes: true,
      },
    },
  },
};

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ShravanSundercommented, Nov 19, 2022

@Code-Hex here is a reproduction: https://github.com/ShravanSunder/issue-graphql-zodgen please run

yarn install
yarn graphql-codgen

you’ll see the generated code in the generated folder zod.ts

1reaction
Code-Hexcommented, Nov 14, 2022

@ShravanSunder Thanks for creating the issue!

Could you share any information how to reproduce the problem. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read properties of undefined (reading '_parse') #1193
Hey, I've come across this issue in my project while composing schemas from imported zod objects/enums.Here is the code: CountryCode.ts ...
Read more >
Creating a zod enum from an object - typescript - Stack Overflow
I want to define it only once using the properties object, without the VALUES array, and use it to create a zod object...
Read more >
Untitled
This issue is resolved in zod@1.10.2 and later. --> # What is Zod Zod is a TypeScript-first schema declaration and validation library. I'm...
Read more >
zod-to-json-schema - npm
Summary · Supports all relevant schema types, basic string, number and array length validations and string patterns. · Resolves recursive and ...
Read more >
Safety Considerations When Using Enums in Avro Schemas
Avro is much loved because of its schema compatibility features. However, it turns out that use of the enum type can erode the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found