8000 Add new feature: `nullable-result` by XiNiHa · Pull Request #1637 · async-graphql/async-graphql · GitHub
[go: up one dir, main page]

Skip to content

Add new feature: nullable-result #1637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

XiNiHa
Copy link
Contributor
@XiNiHa XiNiHa commented Nov 27, 2024

Stacked on #1639


This PR adds a new Cargo feature, nullable-result.

What does it do?

When the feature is enabled, all fields that return Result become nullable, and null is returned in place when the Result has Err().

Why do this?

Null bubbling is a nightmare in most cases, which makes nullable-by-default schema one of the best practices for designing GraphQL schemas. However, the existing behavior of async-graphql doesn't match with it: error bubbles until it meets Option to "capture" it as a null. This makes it too easy to break the app just by not wrapping it with Option, and wrapping all return types from resolvers that return Result with Option is a very tedious, repetitive, and easy-to-miss task to do. This wouldn't have been the case at all if Result got resolved into a nullable type.

If we also look at the ecosystem, Caliban, a GraphQL server framework for Scala, also infers types of fields from the return type of resolvers and emits nullable types if resolvers return a throwable type like ZIO[R, Throwable, A] (somewhat similar to Result<A, Throwable>.) (ref)

While I personally believe that this should be the default for all cases, I guarded it with a Cargo feature for now to keep backward compatibility. I hope we can enable this as default at some point.

@XiNiHa XiNiHa force-pushed the feat/optional-result branch 4 times, most recently from b6ad0b1 to db1aea4 Compare November 27, 2024 10:38
@XiNiHa XiNiHa closed this Nov 27, 2024
@XiNiHa XiNiHa deleted the feat/optional-result branch November 27, 2024 11:01
@XiNiHa XiNiHa restored the feat/optional-result branch November 27, 2024 11:01
@XiNiHa XiNiHa reopened this Nov 27, 2024
@XiNiHa XiNiHa force-pushed the feat/optional-result branch from db1aea4 to 12d2010 Compare December 3, 2024 07:39
@XiNiHa XiNiHa changed the title Add new feature: optional-result Add new feature: nullable-result Dec 3, 2024
@XiNiHa XiNiHa force-pushed the feat/optional-result branch from 12d2010 to 3373f1f Compare December 4, 2024 09:54
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
0