graphql-typeorm-error-parser
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

graphql-typeorm-error-parser

A utility for parsing graphql errors into form field errors

Motivation

Writing validations in Typeorm with class-validator is very easy. When it came to writing the front-end, it felt silly to me to have to re-write my validations, especially custom validations.

The main problem I faced was that the validation errors come back deeply nested within the GraphqlError. I found myself wishing for the ease of Ruby on Rails, where I could simply get the errors for each field of a form.

This utility aims to parse those deeply nested GraphQLErrors into something useful for doing field level validation in the UI.

Usage

Installation

yarn

yarn add graphql-typeorm-error-parser

npm

npm i graphql-typeorm-error-parser

Example

The getFieldErrors method is intended to be used with the catch of a try/catch block. Your GraphQL backend should simply throw any errors from class-validator, the typeorm query failing, or other errors.

import { getFieldErrors } from "graphql-typeorm-error-parser";
 
try {
  await useRegisterMutation({
    variables: {
      email: "joe@email.com",
      password: "weak",
    },
  });
} catch (gqlError) {
  const errors = getFieldErrors(gqlError, {
    fieldErrorValues: {
      as: "string",
      format: "sentence-case",
    },
  });
 
  console.log(errors);
  /**
   * { password: "Password must be at least 6 characters, Password must include a special character" }
   */
}

Roadmap

  • Ability to parse validation errors from class-validator
  • Ability to parse query errors from postgres
  • Add basic options for the format errors are returned in

Package Sidebar

Install

npm i graphql-typeorm-error-parser

Weekly Downloads

14

Version

1.1.1

License

MIT

Unpacked Size

8.57 kB

Total Files

9

Last publish

Collaborators

  • yis4yimmy