-
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
Deserialize variables in Values.fs #327
Comments
Or actually, deserialization can happen in |
I recently hit this issue in a nasty way, so would appreciate more type-safety in this area. Some thoughts... I'm not sure if Anyway, I was thinking what an implementation might look like. Taking the idea from type Decoder<'t> = JsonElement -> Result<'t, string> The library could provide common decoders like To apply this concept, the user would first define an F# type for their input: type FooInput =
{
Bar : int
Qux : string list
Baz : string option
} Then a corresponding decoder: let fooInputDecoder : Decoder<FooInput> =
(fun el ->
result {
let! bar = el |> Decode.property "bar" Decode.int
let! qux = el |> Decode.property "qux" (Decode.list Decode.string)
let! baz = el |> Decode.property "baz" (Decode.optional Decode.string)
return
{
Bar = bar
Qux = qux
Baz = baz
}
}) (Note that using reflection we can provide a default decoder for arbitrary types) Then the decoder would be passed to the GraphQL type definition: let fooInputObjectDef =
Define.InputObject<FooInput>
(
"FooInput",
[
Define.Input ("bar", Types.SchemaDefinitions.Int)
Define.Input ("qux", Types.SchemaDefinitions.ListOf Types.SchemaDefinitions.String)
Define.Input ("baz", Types.SchemaDefinitions.Nullable Types.SchemaDefinitions.String)
],
fooInputDecoder
) I think it would be quite easy for Any thoughts? |
I had a go here: #350 |
Implementation of "decoders" idea here #351 |
Implemented in #418 |
Description
I propose to move variables deserialization into
FSharp.Data.GraphQL/src/FSharp.Data.GraphQL.Server/Values.fs
Line 105 in b072222
Map<string, obj>
whereobj
can be eitherSystem.Text.Json.JsonElement
or anotherMap<string, obj>
.Because in that function we have all the required info to correctly get value from JSON.
This way a scalar coerce function will always receive
System.Text.Json.JsonElement
and for objects options with converters can be applied.@jberzy what do you think?
Alternatives
Define an interface that allows to convert arbitrary JSON toke (i.e. JsonElemen to JToken) to a requested value type and pass it.
The text was updated successfully, but these errors were encountered: