Skip to content

[POC] Enable experimental semantic nullability in Grats and Relay#1

Open
captbaritone wants to merge 3 commits intomainfrom
semantic-nullability
Open

[POC] Enable experimental semantic nullability in Grats and Relay#1
captbaritone wants to merge 3 commits intomainfrom
semantic-nullability

Conversation

@captbaritone
Copy link
Copy Markdown
Owner

@captbaritone captbaritone commented Feb 5, 2024

PR demonstrating enabling experimental support for semantic nullability in both Grats and Relay and seeing the semantic nullability of a resolver field represented in the generated types.

This depends upon a few things:

  1. Grats includes the @semanticNonNull on fields which will only be null in the case of error
  2. Relay's experimental error handling capability throws from within a component when a field error is encountered
  3. Relay's experimental support for @semanticNonNull generates non-nullable TypeScript types for @semanticNonNull fields

Together these features demonstrate the value proposition of semantic nullability:

This allows client code to make an informed decsion about the expected nullability of a field, avoiding tedious null checks of values that are never null in practice, while also ensuring expected nulls are appropriately handled.

-- https://grats.capt.dev/docs/guides/strict-semantic-nullability/#what-is-strict-semantic-nullability

Note

Parcel includes an errors overlay which catches React errors and shows an overlay in dev mode with a stack trace/component stack. You must press esc or click the "x" to see how the error case would actually render in production.

Screenshot 2024-02-05 at 2 44 14 PM

@captbaritone captbaritone changed the title [POC] Enable experimental nullability [POC] Enable experimental semantic nullability in Grats and Relay Feb 5, 2024
import { Fragment, ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type AppChildComponent$data = {
readonly alwaysThrows: string;
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this field is typed as non-nullable!

}

/** @gqlField */
export function alwaysThrows(_: Query): string {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this resolver is typed as non-nullable

export type AppQuery$variables = Record<PropertyKey, never>;
export type AppQuery$data = {
readonly greeting: string | null | undefined;
readonly greeting: string;
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field is also typed as non-nullable

fragment AppChildComponent on Query {
# When this errors, the component will throw and
# the error will be caught by the ErrorBoundary
alwaysThrows
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this field errors, it is contained by the surrounding ErrorBoundary on line 32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant