Skip to content

Commit

Permalink
- document semantic nullability support in resolvers
Browse files Browse the repository at this point in the history
Reviewed By: captbaritone

Differential Revision: D65289273

fbshipit-source-id: c5ba813b1d80c57a009619a3c9dbf7b5976cf701
  • Loading branch information
lynnshaoyu authored and facebook-github-bot committed Nov 7, 2024
1 parent 34948e3 commit 5600502
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
18 changes: 17 additions & 1 deletion website/docs/guides/relay-resolvers/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: errors
title: "Error Handling"
slug: /guides/relay-resolvers/errors/
description: How Relay handles errors throw by resolvers
description: How Relay handles errors thrown by resolvers
---

Just like GraphQL servers, Relay Resolvers support field-level error handling. If an individual resolver throws an error, when that field is read, Relay will log that error to the environment's user-provided `relayFieldLogger` logger, and the field will become null.
Expand Down Expand Up @@ -46,3 +46,19 @@ const environment = new Environment({
:::note
[Live Resolvers](./live-fields.md) can potentially throw errors when they are first evaluated or when their `.read()` method is called. Both types of errors will be handled identically by Relay.
:::

## Support for Semantic Nullability

Relay resolver fields can be specified as [semantically non-null](../../semantic-nullability/) just like server schema fields. Developers can add the directive `@semanticNonNull` in the docblock of a relay resolver in order to indicate that the field is non-nullable in the semantic sense, but that the client should still be prepared to handle errors.

For example:
```ts
/**
* @RelayResolver RelayExample.semantic_non_null_field: String @semanticNonNull
*/
export function semantic_non_null_field(
model: RelayExampleModel,
): string {
return model.someField ?? 'field was null, this is the default';
}
```
4 changes: 0 additions & 4 deletions website/docs/guides/relay-resolvers/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ Relay Resolvers are do have some limitations. Here we will collect a list of kno

In a full GraphQL implementation, resolvers would have access to an `info` argument. This argument is not available in Relay Resolvers today.

## All fields must be nullable

Today all resolvers must be typed as nullable in order to support coercing errors to null without having to implement null bubbling. In the future we intend Resolvers to support some version of [strict semantic nullability](https://github.com/graphql/graphql-wg/discussions/1410).

## Not all GraphQL constructs are supported

Today Relay Resolvers only support a subset of GraphQL constructs. For example, it's not currently possible to define input types, enums or interfaces using Relay Resolvers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,19 @@ const environment = new Environment({
:::note
[Live Resolvers](./live-fields.md) can potentially throw errors when they are first evaluated or when their `.read()` method is called. Both types of errors will be handled identically by Relay.
:::

## Support for Semantic Nullability

Relay resolver fields can be specified as [semantically non-null](../../semantic-nullability/) just like server schema fields. Developers can add the directive `@semanticNonNull` in the docblock of a relay resolver in order to indicate that the field is non-nullable in the semantic sense, but that the client should still be prepared to handle errors.

For example:
```ts
/**
* @RelayResolver RelayExample.semantic_non_null_field: String @semanticNonNull
*/
export function semantic_non_null_field(
model: RelayExampleModel,
): string {
return model.someField ?? 'field was null, this is the default';
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ Relay Resolvers are do have some limitations. Here we will collect a list of kno

In a full GraphQL implementation, resolvers would have access to an `info` argument. This argument is not available in Relay Resolvers today.

## All fields must be nullable

Today all resolvers must be typed as nullable in order to support coercing errors to null without having to implement null bubbling. In the future we intend Resolvers to support some version of [strict semantic nullability](https://github.com/graphql/graphql-wg/discussions/1410).

## Not all GraphQL constructs are supported

Today Relay Resolvers only support a subset of GraphQL constructs. For example, it's not currently possible to define input types, enums or interfaces using Relay Resolvers.
Expand Down

0 comments on commit 5600502

Please sign in to comment.