Errors plugin
A plugin for easily including error types in your GraphQL schema and hooking up error types to resolvers
Usage
Install
Setup
Ensure that the target in your tsconfig.json
is set to es6
or higher (default is es3
).
Example Usage
The above example will produce a GraphQL schema that looks like:
This field can be queried using fragments like:
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 fordirectResult
option on fields (only affects non-list fields)defaultResultOptions
: Sets the defaults forresult
option on fields.name
: Function to generate a custom name on the generated result types.
defaultUnionOptions
: Sets the defaults forresult
option on fields.name
: Function to generate a custom name on the generated union types.
Options on Fields
types
: An array of Error classes to catch and handle as error objects in the schema. Will be merged withdefaultTypes
from builder.union
: An options object for the union type. Can include any normal union type options, andname
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, andname
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 nameddata
by default, but can be written by passsing a customname
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.
Recommended Usage
- Set up an Error interface
- Create a BaseError object type
- Include the Error interface in any custom Error types you define
- 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:
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.
Example query:
With the dataloader plugin
To use this in combination with the dataloader plugin, ensure that that errors plugin is listed BEFORE the dataloader 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.
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 prisma plugin in your plugin list. This will enable errors
option to 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