-
Notifications
You must be signed in to change notification settings - Fork 441
Strip InputValue from from_input for GraphQL scalars
#1327
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
Improvement of existing features or bugfix
k::api
Related to API (application interface)
k::refactor
Refactoring, technical debt elimination and other improvements of existing code base
semver::breaking
Breaking change in terms of SemVer
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.
Synopsis
At the moment, defining a GraphQL scalar requires a
from_input()function, which acceptsInputValueas its argument:However, the only viable option to parse a
GraphQLScalaris only fromInputValue::Scalarvariant. Machinery never passes to this function anInputValue::Enumor other variants. Thus, all theGraphQLScalardeclarations useInputValue::as_string_value()or similar methods, which inside use theInputValue::as_scalar_value()method which tries to unwrap anInputValue::Scalar.It's quite redundant to deal with the whole
InputValueinfrom_input()functions, while the only viable and used variant is alwaysInputValue::Scalar.Solution
Instead, it makes sense to pass into the
from_input()the already unwrappedInputValue::ScalarasScalarValue, so the user code could deal with it directly without a need to involve anyInputValuefunctionality.And yet, we can take a step further and provide an input/output type polymorphism for the
from_input()function:This is achieved by revamping the
ScalarValuetrait and introducing input type polymorphism via helperTryScalarValueToconversion trait. We cannot use justTryFromorTryIntotrait for this purpose, because the conversions are made from a reference, and thus, we cannot specify the required conversions as super-trait bounds for theScalarValuetrait (which we need to make them implied in macro expansions or wheneverScalarValueis used).The only downside, that Rust cannot infer types if the generic is specified directly, and thus, when we need to use a generic in
from_input()function, we should use the transparentScalarwrapper to aid the inference:Into-
Resultoutput type polymorphism is achieved by::juniper::macros::helper::ToResultCallautoref-based specialization for the provided function.Checklist