relay-runtime
Version:
A core runtime for building GraphQL-driven applications.
59 lines (49 loc) • 2.32 kB
text/mdx
---
id: throw-on-field-error-directive
title: ' Directive'
slug: /guides/throw-on-field-error-directive/
description: Relay guide to
keywords:
- directive
- optional
- errors
---
import DocsRating from '@site/src/core/DocsRating';
:::tip
Both `` and `` only handle field errors in the
query/fragment/mutation in which they are used. They **do not** handle errors
related to fields in any spread fragments.
:::
The `` directive can be added to fragments and queries. When
this directive is used, the Relay runtime will throw an exception if a field
with a field error is encountered while reading the fragment or query, or if
Relay is missing data due to a
[graph relationship change](../debugging/why-null.mdx#graph-relationship-change).
In addition to causing the Relay runtime to throw an exception if a field error
is encountered, the `` directive also enables generation of
non-null Flow types for fields that have the `` directive in the
schema. This means that if a field has the `` directive, the
generated Flow type for that field will be non-nullable; if an error were to
occur while reading that field, the thrown exception will prevent your
application from receiving a null value. Making a previously-nullable field
non-null may make many existing `` directives unnecessary; you can use
the
[remove-unnecessary-required-directives codemod](../guides/codemods.mdx#remove-unnecessary-required-directives)
to clean these up.
To use the `` directive, add it to a fragment or query in your
Relay code. For example:
```
fragment MyFragment on User {
id
name
}
```
In this example, the `` directive is added to the MyFragment
fragment. If any of the fields in this fragment (in this case, id and name) have
a field error, the Relay runtime will throw an exception at the time the
fragment is read.
If you wish to handle a specific field error locally within your
`` fragment or query instead of having that error throw, you
can catch the error with [](./catch-directive.mdx).
**Read more about Relay's experimental support for
[Semantic Nullability](./semantic-nullability.mdx).**