Add new feature: nullable-result
#1637
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, andnull
is returned in place when theResult
hasErr()
.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 meetsOption
to "capture" it as a null. This makes it too easy to break the app just by not wrapping it withOption
, and wrapping all return types from resolvers that returnResult
withOption
is a very tedious, repetitive, and easy-to-miss task to do. This wouldn't have been the case at all ifResult
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 toResult<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.