FSharp.Data.GraphQL icon indicating copy to clipboard operation
FSharp.Data.GraphQL copied to clipboard

int value could be valid float value

Open evilz opened this issue 4 years ago • 2 comments

Description

For a data of type float when value extracted from response is int the code fail to run

Repro steps

  • create schema with double or float
  • respond with integer value

Expected behavior

value should be casted

Actual behavior

Fail with exception

Known workarounds

evilz avatar Jul 21 '20 14:07 evilz

Can you be more specific? I'm not sure I get the error here. Do you mean you're trying to return an integer from a resolver that is typed to be a Float?

jberzy avatar Jul 21 '20 22:07 jberzy

In schema you got something like this :

"fields": [
            {
              "name": "amount",
              "description": "",
              "args": [],
              "type": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "SCALAR",
                  "name": "Float",
                  "ofType": null
                }
              },
              "isDeprecated": false,
              "deprecationReason": null
            },

But the server (not one done in fsharp, from someone else) is responding with

{ "Amount": 100 , .....} 

In RecordBase diconnary it will be deserialize as INT but the type that we expect is FLOAT. My ugly work around is that

let flt (dico:IDictionary<string,obj>) (key:string) =
    let value = dico.[key]
    let s = value.ToString()
    let r = TryParser.parseDouble s |> Option.defaultValue 0.0
    r

let fltOpt (dico:IDictionary<string,obj>) (key:string) =
    let value = dico.[key]
    let s = value.ToString()
    let r = TryParser.parseDouble s
    r

evilz avatar Jul 22 '20 08:07 evilz

Fixed, use latest preview packages from the GitHub feed

xperiandri avatar Nov 05 '23 23:11 xperiandri