{"id":15192431,"url":"https://github.com/kylerush/zod-graphql-type","last_synced_at":"2026-02-02T09:07:10.944Z","repository":{"id":65493668,"uuid":"591419945","full_name":"kylerush/zod-graphql-type","owner":"kylerush","description":"ZodError GraphQL type so that you can return zod errors in your GraphQL API.","archived":false,"fork":false,"pushed_at":"2023-03-09T22:25:16.000Z","size":116,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-12T08:47:05.201Z","etag":null,"topics":["error-handling","error-messages","graphql","graphql-api","graphql-server","zod"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kylerush.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-20T18:01:32.000Z","updated_at":"2023-01-25T23:01:00.000Z","dependencies_parsed_at":"2024-11-12T11:02:47.544Z","dependency_job_id":"a7df02d6-fdf5-43c3-9d54-9f0f9a9dbf8f","html_url":"https://github.com/kylerush/zod-graphql-type","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"83fde247159da3ee13b01ddf6cca1d8f7d766002"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylerush%2Fzod-graphql-type","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylerush%2Fzod-graphql-type/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylerush%2Fzod-graphql-type/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylerush%2Fzod-graphql-type/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kylerush","download_url":"https://codeload.github.com/kylerush/zod-graphql-type/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241180878,"owners_count":19923307,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["error-handling","error-messages","graphql","graphql-api","graphql-server","zod"],"created_at":"2024-09-27T21:23:26.675Z","updated_at":"2026-02-02T09:07:10.895Z","avatar_url":"https://github.com/kylerush.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# zod-graphql-type\n\n`ZodError` GraphQL type so that you can return [`zod`](https://github.com/colinhacks/zod) errors in your GraphQL API.\n\nAs 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.\n\nThis package gives you 3 things:\n\n1. A ZodError type definition to import into your GraphQL schema for use in other types;\n2. Custom GraphQL resolvers to make the ZodError type definition work;\n3. 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).\n\n## Getting started\n\n### Installation\n\n```sh\nnpm install zod-graphql-type\n```\n\n### Usage\n\n```js\nimport { zodTypeDef } from \"zod-graphql-type\";\n```\n\nThen add the type definition to your GraphQL type schema.\n\nHere is a Fastify Mercurius example:\n\n```js\nimport { Fastify } from \"fastify\";\nimport mercurius from \"mercurius\";\nimport { z } from \"zod\";\nimport { zodTypeDef, scalars, zodErrorsTozodIssues } from \"zod-graphql-type\";\n\nconst fastify = Fastify();\n\nconst schema = `\n  ${zodTypeDef},\n  input ObjectToValidate {\n    phraseFirstHalf: String\n  }\n  type Success {\n    message: String!\n  }\n  type Error {\n    message: String!\n    zodIssues: [ZodIssue]\n  }\n  type Query {\n    validatePhrase(input: ObjectToValidate): Success | Error\n  }\n`;\n\nconst resolvers = {\n  Query: {\n    validatePhrase: (_, { phrase }) =\u003e {\n      const phraseSecondHalf = fetch(`https://someapi.com?phrase=${phrase}`);\n      // Create a zod schema to validate the argument\n      const schema = z.object({\n        phrase: z.literal(\"world\"),\n      });\n      try {\n        schema.parse(phraseSecondHalf);\n        return {\n          __typename: \"Success\",\n          message: \"Object passed validation.\",\n        };\n      } catch (error) {\n        return {\n          __typename: \"Error\",\n          message: \"Object failed vlaidation.\",\n          zodIssues: zodErrorsTozodIssues(error),\n        };\n      }\n    },\n  },\n};\n\napp.register(mercurius, {\n  schema,\n  resolvers,\n});\n\napp.get(\"/\", async function (req, reply) {\n  const query = '{ validatePhrase(input: {phraseFirstHalf: \"world\"})}';\n  return reply.graphql(query);\n});\n\napp.listen({ port: 3000 });\n```\n\nNote 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.\n\n## Features\n\nThe vast majority of ZodError types are tested and working. The following are tested:\n\n- `ZodIssueCode.invalid_type`\n- `ZodIssueCode.unrecognized_keys`\n- `ZodIssueCode.invalid_union`\n- `ZodIssueCode.invalid_enum_value`\n- `ZodIssueCode.too_big`\n- `ZodIssueCode.too_small`\n- `ZodIssueCode.invalid_type =\u003e custom error`\n- `ZodIssueCode.invalid_string`\n- `ZodIssueCode.invalid_date`\n- `ZodIssueCode.not_multiple_of`\n\nThe following are known to not work yet (support could be added in the future):\n\n- `ZodIssueCode.invalid_arguments` an error produced from `z.function().args().implement()`\n- `ZodIssueCode.invalid_return_type` an error produced from `z.function().returnType().implement()`\n\nEverything other than `z.function()` function schemas that use `.implement()` should be working properly. If you find a bug, please create an issue.\n\n## Notes\n\nThis is not yet a 1.0.0 release. Since this is under active development, versioning won't follow SEMVER until 1.0.0.\n\n## Developing\n\nYou 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`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylerush%2Fzod-graphql-type","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkylerush%2Fzod-graphql-type","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylerush%2Fzod-graphql-type/lists"}