Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@giraphql/plugin-errors

hayes66ISC2.13.0TypeScript support: included

A GiraphQL plugin for adding typed errors into your schema

giraphql, graphql, schema, typescript, error, errors, plugin

readme

Errors Plugin

A plugin for easily including error types in your GraphQL schema and hooking up error types to resolvers

Usage

Install

yarn add @giraphql/plugin-errors

Setup

Ensure that the target in your tsconfig.json is set to es6 or higher (default is es3).

Example Usage

import ErrorsPlugin from '@giraphql/plugin-errors';
const builder = new SchemaBuilder({
  plugins: [ErrorsPlugin],
  errorOptions: {
    defaultTypes: [],
  },
});

builder.objectType(Error, {
  name: 'Error',
  fields: (t) => ({
    message: t.exposeString('message'),
  }),
});

builder.queryType({
  fields: (t) => ({
    hello: t.string({
      errors: {
        types: [Error],
      },
      args: {
        name: t.arg.string({ required: false }),
      },
      resolve: (parent, { name }) => {
        if (name.slice(0, 1) !== name.slice(0, 1).toUpperCase()) {
          throw new Error('name must be capitalized');
        }

        return `hello, ${name || 'World'}`;
      },
    }),
  }),
});

The above example will produce a GraphQL schema that looks like:

type Error {
  message: String!
}

type Query {
  hello(name: String!): QueryHelloResult
}

union QueryHelloResult = Error | QueryHelloSuccess

type QueryHelloSuccess {
  data: String!
}

This field can be queried using fragments like:

query {
  hello(name: "World") {
    __typename
    ... on Error {
      message
    }
    ... on QueryHelloSuccess {
      data
    }
  }
}

This plugin works by wrapping fields that define error options in a union type. This union consists of an object type for each error type defined for the field, and a Success object type that wraps the returned data. If the fields resolver throws an instance of one of the defined errors, the errors plugin will automatically resolve to the corresponding error object type.

Builder options

  • defaultTypes: An array of Error classes to include in every field with error handling.
  • directResult: Sets the default for directResult option on fields (only affects non-list fields)

Options on Fields

  • types: An array of Error classes to catch and handle as error objects in the schema. Will be merged with defaultTypes from builder.
  • union: An options object for the union type. Can include any normal union type options, and name option for setting a custom name for the union type.
  • result: An options object for result object type. Can include any normal object type options, and name option for setting a custom name for the result type.
  • dataField: An options object for the data field on the result object. This field will be named data by default, but can be written by passsing a custom name option.
  • directResult: Boolean, can only be set to true for non-list fields. This will directly include the fields type in the union rather than creating an intermediate Result object type. This will throw at build time if the type is not an object type.
  1. Set up an Error interface
  2. Create a BaseError object type
  3. Include the Error interface in any custom Error types you define
  4. Include the BaseError type in the defaultTypes in the builder config

This pattern will allow you to consistently query your schema using a ... on Error { message } fragment since all Error classes extend that interface. If your client want's to query details of more specialized error types, they can just add a fragment for the errors it cares about. This pattern should also make it easier to make future changes without unexpected breaking changes for your clients.

The follow is a small example of this pattern:

import ErrorsPlugin from '@giraphql/plugin-errors';
const builder = new SchemaBuilder({
  plugins: [ErrorsPlugin],
  errorOptions: {
    defaultTypes: [Error],
  },
});

const ErrorInterface = builder.interfaceRef<Error>('Error').implement({
  fields: (t) => ({
    message: t.exposeString('message'),
  }),
});

builder.objectType(Error, {
  name: 'BaseError',
  isTypeOf: (obj) => obj instanceof Error,
  interfaces: [ErrorInterface],
});

class LengthError extends Error {
  minLength: number;

  constructor(minLength: number) {
    super(`string length should be at least ${minLength}`);

    this.minLength = minLength;
    this.name = 'LengthError';
  }
}

builder.objectType(LengthError, {
  name: 'LengthError',
  interfaces: [ErrorInterface],
  isTypeOf: (obj) => obj instanceof LengthError,
  fields: (t) => ({
    minLength: t.exposeInt('minLength'),
  }),
});

builder.queryType({
  fields: (t) => ({
    // Simple error handling just using base error class
    hello: t.string({
      errors: {},
      args: {
        name: t.arg.string({ required: true }),
      },
      resolve: (parent, { name }) => {
        if (!name.startsWith(name.slice(0, 1).toUpperCase())) {
          throw new Error('name must be capitalized');
        }

        return `hello, ${name || 'World'}`;
      },
    }),
    // Handling custom errors
    helloWithMinLength: t.string({
      errors: {
        types: [LengthError],
      },
      args: {
        name: t.arg.string({ required: true }),
      },
      resolve: (parent, { name }) => {
        if (name.length < 5) {
          throw new LengthError(5);
        }

        return `hello, ${name || 'World'}`;
      },
    }),
  }),
});

With validation plugin

To use this in combination with the validation plugin, ensure that that errors plugin is listed BEFORE the validation plugin in your plugin list.

Once your plugins are set up, you can define types for a ZodError, the same way you would for any other error type. Below is a simple example of how this can be done, but the specifics of how you structure your error types are left up to you.

// Util for flattening zod errors into something easier to represent in your Schema.
function flattenErrors(
  error: ZodFormattedError<unknown>,
  path: string[],
): { path: string[]; message: string }[] {
  // eslint-disable-next-line no-underscore-dangle
  const errors = error._errors.map((message) => ({
    path,
    message,
  }));

  Object.keys(error).forEach((key) => {
    if (key !== '_errors') {
      errors.push(
        ...flattenErrors((error as Record<string, unknown>)[key] as ZodFormattedError<unknown>, [
          ...path,
          key,
        ]),
      );
    }
  });

  return errors;
}

// A type for the individual validation issues
const ZodFieldError = builder
  .objectRef<{
    message: string;
    path: string[];
  }>('ZodFieldError')
  .implement({
    fields: (t) => ({
      message: t.exposeString('message'),
      path: t.exposeStringList('path'),
    }),
  });

// The actual error type
builder.objectType(ZodError, {
  name: 'ZodError',
  interfaces: [ErrorInterface],
  isTypeOf: (obj) => obj instanceof ZodError,
  fields: (t) => ({
    fieldErrors: t.field({
      type: [ZodFieldError],
      resolve: (err) => flattenErrors(err.format(), []),
    }),
  }),
});

builder.queryField('fieldWIthValidation', (t) =>
  t.boolean({
    errors: {
      types: [ZodError],
    },
    args: {
      string: t.arg.string({
        validate: {
          type: 'string',
          minLength: 3,
        },
      }),
    },
    resolve: () => true,
  }),
);

Example query:

query {
  validation(string: "a") {
    __typename
    ... on QueryValidationSuccess {
      data
    }
    ... on ZodError {
      fieldErrors {
        message
        path
      }
    }
  }
}

With the dataloader plugin

To use this in combination with the dataloader plugin, ensure that that errors plugin is listed BEFORE the validation plugin in your plugin list.

If a field with errors returns a loadableObject, or loadableNode the errors plugin will now catch errors thrown when loading ids returned by the resolve function.

plugin. This is because if items are nullable, the items in the list may be set to null rather that If the field is a List field, errors that occur when resolving objects from ids will not be handled by the errors plugin. This is because those errors are associated with each item in the list rather than the list field itself. In the future, the dataloader plugin may have an option to throw an error at the field level if any items can not be loaded, which would allow the error plugin to handle these types of errors.

With the prisma plugin

To use this in combination with the prisma plugin, ensure that that errors plugin is listed BEFORE the validation plugin in your plugin list. This will enable errors option to work work correctly with any field builder method from the prisma plugin.

errors can be configured for any field, but if there is an error pre-loading a relation the error will always surfaced at the field that executed the query. Because there are cases that fall back to executing queries for relation fields, these fields may still have errors if the relation was not pre-loaded. Detection of nested relations will continue to work if those relations use the errors plugin

changelog

Change Log

2.13.0

Minor Changes

  • 9307635a: Migrate build process to use turborepo

2.12.3

Patch Changes

  • 2b08f852: Fix syntax highlighting in docs and update npm README.md files"

2.12.2

Patch Changes

  • c6aa732: graphql@15 type compatibility fix

2.12.1

Patch Changes

  • c85dc33: Add types entry in package.json

2.12.0

Minor Changes

  • aeef5e5: Update dependencies

2.11.0

Minor Changes

  • 9107f29: Update dependencies (includes graphql 16)

2.10.0

Minor Changes

  • 17db3bd: Make type refs extendable by plugins

2.9.0

Minor Changes

  • 73e947b: Add directResult option to remove extra result type wrapper in error unions

2.8.2

Patch Changes

  • c976bfe: Update dependencies

2.8.1

Patch Changes

  • 4150f92: Fixed esm transformer for path-imports from dependencies

2.8.0

Minor Changes

  • dc87e68: update esm build process so extensions are added during build rather than in source

2.7.2

Patch Changes

  • b4b8381: Updrade deps (typescript 4.4)

2.7.1

Patch Changes

  • f04be64: Update dependencies

2.7.0

Minor Changes

  • ea4d456: Add interoperability between prisma and errors plugins

2.6.0

Minor Changes

  • 4f9b886: Add integration between error and dataloader plugins to that errors from dataloaders can be handled via errors plugin

2.5.0

Minor Changes

  • a4c87cf: Use ".js" extensions everywhere and add module and exports to package.json to better support ems in node

2.4.2

Patch Changes

  • f13208c: bump to fix latest tag

2.4.1

Patch Changes

  • 9ab8fbc: re-release previous version due to build-process issue

2.4.0

Minor Changes

  • 3dd3ff14: Updated dev dependencies, switched to pnpm, and added changesets for releases

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

2.3.1 - 2021-08-03

Note: Version bump only for package @giraphql/plugin-errors

2.3.1-alpha.0 - 2021-08-02

Note: Version bump only for package @giraphql/plugin-errors

2.3.0 - 2021-07-30

🚀 Updates

Note: Version bump only for package @giraphql/plugin-errors

2.2.2 - 2021-07-23

Note: Version bump only for package @giraphql/plugin-errors

2.2.2-alpha.0 - 2021-07-17

Note: Version bump only for package @giraphql/plugin-errors

2.2.1 - 2021-07-10

Note: Version bump only for package @giraphql/plugin-errors

2.2.0 - 2021-07-04

Note: Version bump only for package @giraphql/plugin-errors

2.2.0-alpha.0 - 2021-07-04

🚀 Updates

  • add early warning for undefined refs to simplify debugging of circular import issues (095b68b)

📦 Dependencies

Note: Version bump only for package @giraphql/plugin-errors

2.1.2 - 2021-07-02

🐞 Fixes

  • only create error types once (60fddd8)

📘 Docs

  • add es6 target requirement to error plugin docs (a218973)

Note: Version bump only for package @giraphql/plugin-errors

2.1.1 - 2021-06-30

📘 Docs

  • update docs to include links to error plugin (46db92d)

Note: Version bump only for package @giraphql/plugin-errors

2.1.0 - 2021-06-28

🐞 Fixes

  • type default empty objects as never to ensure compatibility with plugins that add required options (e457c02)

Note: Version bump only for package @giraphql/plugin-errors

2.1.0-alpha.1 - 2021-06-28

🚀 Updates

  • make error options optional only when options can be empty objects (6791bcb)
  • update docs and deno (4f131b0)

🐞 Fixes

  • fix typos in tests and docs (3b81ba2)

Note: Version bump only for package @giraphql/plugin-errors

2.1.0-alpha.0 - 2021-06-28

🚀 Updates

Note: Version bump only for package @giraphql/plugin-errors