@vantreeseba/graphql-zod-codegen
v0.1.0
Published
GraphQL Code Generator plugin that emits Zod validation schemas from your typed documents.
Readme
@vantreeseba/graphql-zod-codegen
A graphql-codegen plugin that writes Zod schemas for every named operation: one for its variables, with nested input objects resolved, and one for its result.
The generated file imports a helper from the runtime package, @vantreeseba/graphql-zod, so a project installs both.
Install
npm install @vantreeseba/graphql-zod graphql zod
npm install --save-dev @vantreeseba/graphql-zod-codegen @graphql-codegen/cliQuick start
Create these files in your project:
# schema.graphql
type User { id: ID! name: String! age: Int }
type Query { user(id: ID!): User }# src/get-user.graphql
query GetUser($id: ID!) { user(id: $id) { id name age } }// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: 'schema.graphql',
documents: 'src/**/*.graphql',
generates: {
'src/__generated__/zod-schemas.ts': { plugins: ['@vantreeseba/graphql-zod-codegen'] },
},
};
export default config;// src/quick-start.mjs
import { GetUserQueryResultSchema } from './__generated__/zod-schemas.ts';
console.log(GetUserQueryResultSchema.parse({ user: { id: '1', name: 'Ada', age: '36' } }));
// { user: { id: '1', name: 'Ada', age: 36 } }Then generate the schemas and use one. Node 22.18 or later runs the generated TypeScript as it is:
npx graphql-codegen --config codegen.ts
node src/quick-start.mjsDocumentation
- Install and configure: setup and the
scalarsoption - Generated output: names, inputs, results, lists, and what is not resolved
- Common mistakes
The installed package has these pages too, with an index of every export in its llms.txt.
Roadmap
Planned work is in TODO.md.
