Call scalar coerceUserInput before coerceOutput#993
Draft
filosganga wants to merge 1 commit intosangria-graphql:mainfrom
Draft
Call scalar coerceUserInput before coerceOutput#993filosganga wants to merge 1 commit intosangria-graphql:mainfrom
filosganga wants to merge 1 commit intosangria-graphql:mainfrom
Conversation
e779fcd to
f17e172
Compare
filosganga
commented
Apr 14, 2023
Comment on lines
+1408
to
+1412
| val corcedInput = scalar | ||
| .coerceUserInput(value) | ||
| // Do we need a specific exepction here? | ||
| .fold(e => throw new ValidationError(Vector(e), exceptionHandler), identity) | ||
| val coerced = scalar.coerceOutput(corcedInput, marshaller.capabilities) |
Contributor
Author
There was a problem hiding this comment.
Throwing an exception here because the whole block is protected by try/catch
yanns
reviewed
Apr 15, 2023
| "UUID", | ||
| coerceOutput = (v, _) => v.toString, | ||
| coerceUserInput = { | ||
| case uuid: UUID => Right(uuid) |
Contributor
There was a problem hiding this comment.
I try to understand:
coerceUserInput is now called twice:
- the first time to parse the user input as
Stringinto aUUID - the second time to confirm that a
UUIDis a valid value
?
Contributor
Author
There was a problem hiding this comment.
No it is not called, twice, but I have spent a bit of time looking at Apollo implementation, and I think, I would just need to change the scalars rather than Sangria.
In Apollo the scalar are represented by 3 functions:
serializeit is the same ascoerceOutput, it coerces the scalar to a JSON representationparseLiteralit is the same ascoerceInputthat is bit misleading, it basically coerces and hardcoded value into the scalar typeparseValueit is the same ascoerceUserInputthat basically coerces the values coming from the users via not hardcoded variables.
When you have an untyped underlying model, like JSON, the serialize function should take care also of parsing the type from the underlying model.
I will investigate more about that and maybe drop this PR in favour of a documentation one.
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
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.
This PR investigates a solution for #991
The scalar are always input coerced before coerced for output. This also mean that for any
scalarType: ScalarType[T],scalarType.coerceUserInputmust accept anyTand return it as-is (It is already the case for all the builtin Scalars).The coercion happens in two places, the default field resolver, and the FutureResolver, that is basically the base resolver. I could not find a better place to do it, probably something in between the input unmarshaller and the resolver would be the best place. Unfortunately, the unmarshaller does not receive any information about the ScalarType, so it can only leverage the underlying model.