-
Notifications
You must be signed in to change notification settings - Fork 73
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
Check if input object CLR property type does not match the scalar's CLR type declared in GraphQL object definition #490
base: dev
Are you sure you want to change the base?
Conversation
if result then result | ||
else | ||
if from.FullName.StartsWith OptionTypeName || from.FullName.StartsWith ValueOptionTypeName then | ||
from.GetGenericArguments().[0].IsAssignableTo ``to`` | ||
else result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we declare scalar with a validator like this
type AddressLine1 = ValidString<Phantom.Address.Line1>
module Address =
open ValidString
open Validus.Operators
let createLine1 : Validator<string, AddressLine1 voption> =
(allowEmpty ?=> (Check.String.lessThanLen 1000 <+> validateStringCharacters)) *|* ValueOption.map ValidString
let Line1Type : ScalarDefinition<string, AddressLine1 voption> = Define.ValidStringScalar<AddressLine1>("AddressLine1", createLine1, "Address line 1")
it appears that the type of scalar becomes voption<AddressLine1>
and then if we declare it Nullable
Define.Input("line1", Nullable Address.Line1Type)
it is double wrapped like option<voption<AddressLine1>>
The unwrapping itself happes on execution well but in order to perform check here I need to try unwrapping it 2 times
@valbers what do you think? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@valbers what do you think?
It looks overall good. Please just take a look at my comments.
@@ -36,7 +36,8 @@ let private normalizeOptional (outputType : Type) value = | |||
| value -> | |||
let inputType = value.GetType () | |||
if inputType.Name <> outputType.Name then | |||
let expectedOutputType = outputType.GenericTypeArguments[0] | |||
// Use only when option or voption so must not be null | |||
let expectedOutputType = outputType.GenericTypeArguments.FirstOrDefault() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're sure it will never be null
, then:
let expectedOutputType = outputType.GenericTypeArguments.FirstOrDefault() | |
let expectedOutputType = outputType.GenericTypeArguments.First() |
Otherwise, I think it's a better idea to actively throw an exception in case of Default
:
if expectedOutputType = null
then
failwith "Expected output type was null. This is a bug in FSharp.Data.GraphQL."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, my point was that the expectedOutputType
value is used only in if clauses that a relevant for Option
and ValueOption
types
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Just keep in mind that by doing this you create a logical dependency in the lines of code and someday someone might introduce a bug by not considering your initial intention with Option
and ValueOption
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no quick idea how to improve that 🙂
let result = from.IsAssignableTo ``to`` || checkCollections from ``to`` | ||
if result then result | ||
else | ||
if from.FullName.StartsWith OptionTypeName || from.FullName.StartsWith ValueOptionTypeName then | ||
let from = from.GetGenericArguments()[0] | ||
from.IsAssignableTo ``to`` || checkCollections from ``to`` | ||
else result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let result = from.IsAssignableTo ``to`` || checkCollections from ``to`` | |
if result then result | |
else | |
if from.FullName.StartsWith OptionTypeName || from.FullName.StartsWith ValueOptionTypeName then | |
let from = from.GetGenericArguments()[0] | |
from.IsAssignableTo ``to`` || checkCollections from ``to`` | |
else result | |
from.IsAssignableTo ``to`` || checkCollections from ``to`` || | |
((from.FullName.StartsWith OptionTypeName || from.FullName.StartsWith ValueOptionTypeName) && ((from.GetGenericArguments()[0]).IsAssignableTo ``to`` || checkCollections from ``to``)) |
also notice how the else result
in your code is superfluous, since result
is always false at that point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is hard to read
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, but did you also read the part:
also notice how the else result in your code is superfluous, since result is always false at that point.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep but do you see wrong in that?
tests/FSharp.Data.GraphQL.Tests/Variables and Inputs/InputScalarAndAutoFieldScalarTests.fs
Show resolved
Hide resolved
079c192
to
ef24b61
Compare
…e scalar's CLR type declared in GraphQL object definition
ef24b61
to
821acdc
Compare
No description provided.