@anatine/graphql-codegen-zod

中文文档
0.4.1发布于 2024/03/20MITTypeScriptCommonJS近 30 天下载 4375

Its a library to parse from Graphql Schema to a Zod Schema

原始使用文档
文档目录5 节
  1. Configs
  2. Simple use
  3. Full Use
  4. Directive Schema
  5. Example

GraphQL codegen schema to Zod schema

The reason for this project is to maintain a single source of truth, between graphql and zod. This is useful for validation on forms. Inspired by codegen-graphql-yup

Configs

  • onlyWithValidation: boolean ( default false) If you want to generate an schema for all your input objects definitions with or without the directive put it in true.
  • zodSchemasMap: a map of your scalars to a zod type. This is useful for scalars such as EmailAddress that are z.string().email()

Simple use

If you only want to validate the required fields, what you can do is use the plugin in the following way

generates:
  schemas.ts:
    plugins:
      - @anatine/graphql-codegen-zod

Full Use

If you need more validations than only required fields, you have to follow this steps.

this is because in graphql instrospection we dont have access to directives

  • Add the directive in your schema.
  • In codegen.yml config use that file like schema.

Directive Schema

directive @validation(
  pattern: String
  min: Int
  max: Int
  requiredMessage: String
  typeOf: String
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

Example

result.graphql

directive @validation(
  pattern: String
  min: Int
  max: Int
  requiredMessage: String
  typeOf: String
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION
enum TestEnum {
  ENUM1
  ENUM2
}
type TestSampleType {
  id: ID!
  name: String!
  email: String
}
input TestInput {
  string: String
  stringRequired: String!
  enum: TestEnum
  enumRequired: TestEnum!
  scalar: EmailAddress
  scalarRequired: EmailAddress!
  enumArray: [TestEnum]
  enumArrayRequired: [TestEnum]!
  emailArray: [EmailAddress]
  emailArrayRequired: [EmailAddress]!
  emailRequiredArray: [EmailAddress!]
  emailRequiredArrayRequired: [EmailAddress!]!
  stringArray: [String]
  stringArrayRequired: [String]!
  stringRequiredArray: [String!]
  stringRequiredArrayRequired: [String!]!
}
input RegisterAddressInput {
  postalCode: TestInput!
  state: [String]!
  city: String!
  someNumber: Int @validation(min: 10, max: 20)
  someNumberFloat: Float @validation(min: 10.50, max: 20.50)
  someBoolean: Boolean
  ipAddress: IPAddress
  line2: String @validation(min: 10, max: 20)
}
scalar EmailAddress
scalar IPAddress

codegen.yml

schema: './apps/codegen-e2e/src/*.graphql'
generates:
  './apps/codegen-e2e/generated/output.ts':
    plugins:
      - @anatine/graphql-codegen-zod
    config:
      allowEnumStringTypes: true
      onlyWithValidation: false
      lazy: true
      zodSchemasMap:
        EmailAddress: z.string().email()
        IPAddress: z.string()
        DateTime: z.string()
        JSON: z.object()
        Date: z.string()
      zodTypesMap:
        EmailAddress: string

版本与发布记录

@anatine/graphql-codegen-zod 共发布 14 个版本,首次发布于 2022/06/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.1latest2024/03/202 年前
0.4.02022/12/133 年前
0.3.142022/07/264 年前
0.3.132022/07/264 年前
0.3.32022/07/254 年前
0.3.22022/07/254 年前

关于 @anatine/graphql-codegen-zod 的常见问题

@anatine/graphql-codegen-zod 是什么?

Its a library to parse from Graphql Schema to a Zod Schema

如何安装 @anatine/graphql-codegen-zod?

在项目目录运行 npm install @anatine/graphql-codegen-zod,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@graphql-tools/utils(^8.8.0)、graphql-code-generator(^0.18.2)、zod(^3.17.3)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@anatine/graphql-codegen-zod 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anatine/graphql-codegen-zod 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@anatine/graphql-codegen-zod 可以商用吗?

@anatine/graphql-codegen-zod 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anatine/graphql-codegen-zod 的下载量有多少?

根据 npm 官方下载统计,@anatine/graphql-codegen-zod 在 2026/10/06 – 2026/11/04 这 30 天内被下载 4,375 次,日均约 146 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anatine/graphql-codegen-zod 还在维护吗?

最新版本 0.4.1 发布于 2024/03/20(2 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。