npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

zod-graphql-type

v0.1.2

Published

ZodError GraphQL type so that you can return Zod errors in your GraphQL API.

Readme

zod-graphql-type

ZodError GraphQL type so that you can return zod errors in your GraphQL API.

As an example use case, let's assume you have a GraphQL mutation that signs a user into your application. The mutation accepts a Google oAuth token to authenticate the user. GraphQL can ensure the input types on the mutation are correct. However, in your GraphQL resolver, you will want to verify the integrity of the oAuth token. Once you verify the integrity of the token, you can validate the token object with a zod schema. This package allows you to return any errors that zod throws for crystal clear error detail to your API consumer.

This package gives you 3 things:

  1. A ZodError type definition to import into your GraphQL schema for use in other types;
  2. Custom GraphQL resolvers to make the ZodError type definition work;
  3. A formatErrors zodErrorsTozodIssues function to transform ZodError (has Error objects in it so it doesn't work with GraphQL nicely) into ZodIssues (no Error object).

Getting started

Installation

npm install zod-graphql-type

Usage

import { zodTypeDef } from "zod-graphql-type";

Then add the type definition to your GraphQL type schema.

Here is a Fastify Mercurius example:

import { Fastify } from "fastify";
import mercurius from "mercurius";
import { z } from "zod";
import { zodTypeDef, scalars, zodErrorsTozodIssues } from "zod-graphql-type";

const fastify = Fastify();

const schema = `
  ${zodTypeDef},
  input ObjectToValidate {
    phraseFirstHalf: String
  }
  type Success {
    message: String!
  }
  type Error {
    message: String!
    zodIssues: [ZodIssue]
  }
  type Query {
    validatePhrase(input: ObjectToValidate): Success | Error
  }
`;

const resolvers = {
  Query: {
    validatePhrase: (_, { phrase }) => {
      const phraseSecondHalf = fetch(`https://someapi.com?phrase=${phrase}`);
      // Create a zod schema to validate the argument
      const schema = z.object({
        phrase: z.literal("world"),
      });
      try {
        schema.parse(phraseSecondHalf);
        return {
          __typename: "Success",
          message: "Object passed validation.",
        };
      } catch (error) {
        return {
          __typename: "Error",
          message: "Object failed vlaidation.",
          zodIssues: zodErrorsTozodIssues(error),
        };
      }
    },
  },
};

app.register(mercurius, {
  schema,
  resolvers,
});

app.get("/", async function (req, reply) {
  const query = '{ validatePhrase(input: {phraseFirstHalf: "world"})}';
  return reply.graphql(query);
});

app.listen({ port: 3000 });

Note that you will need to use the formatErrors function which replaces ZodError with ZodIssue[]. ZodError is of type Error and if you return a type of Error to GraphQL it will not allow you to return the ZodIssues properly.

Features

The vast majority of ZodError types are tested and working. The following are tested:

  • ZodIssueCode.invalid_type
  • ZodIssueCode.unrecognized_keys
  • ZodIssueCode.invalid_union
  • ZodIssueCode.invalid_enum_value
  • ZodIssueCode.too_big
  • ZodIssueCode.too_small
  • ZodIssueCode.invalid_type => custom error
  • ZodIssueCode.invalid_string
  • ZodIssueCode.invalid_date
  • ZodIssueCode.not_multiple_of

The following are known to not work yet (support could be added in the future):

  • ZodIssueCode.invalid_arguments an error produced from z.function().args().implement()
  • ZodIssueCode.invalid_return_type an error produced from z.function().returnType().implement()

Everything other than z.function() function schemas that use .implement() should be working properly. If you find a bug, please create an issue.

Notes

This is not yet a 1.0.0 release. Since this is under active development, versioning won't follow SEMVER until 1.0.0.

Developing

You can run the dev server with npm run start. Tests can be run with npm run test and with watch npm run test:watch. To fully check your branch run npm run package.