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

If enum processed by System.Text.Json then it cannot be coerced from enum value

Open xperiandri opened this issue 5 years ago • 2 comments

Description

I have rewritten sample server to use System.Text.Json including JsonStringEnumConverter

let serializerOptions =
    let options = JsonSerializerOptions(PropertyNamingPolicy = JsonNamingPolicy.CamelCase)
    let converters = options.Converters
    converters.Add (JsonStringEnumConverter ())
    converters.Add (IsbnConverter ())
    converters.Add (JsonFSharpConverter ())
    options

But the value that comes to this code is enum instance then, but not a string.

    | Enum enumdef ->
        match input with
        | :? string as s ->
            ReflectionHelper.parseUnion enumdef.Type s
        | _ -> 
            raise (GraphQLException <| errMsg + (sprintf "Cannot coerce value of type '%O' to type Enum '%s'" (input.GetType()) enumdef.Name))

So that I get FSharp.Data.GraphQL.GraphQLException: 'Variable '$reactionKind': Cannot coerce value of type 'Frebo.Biblioteko.Domain.Reaction' to type Enum 'ReactionEnum''

Repro steps

  1. Define Enum type
  2. Use System.Text.Json with JsonStringEnumConverter
  3. Send mutation with Enum parameter

Expected behavior

Enum successfully coerced

Actual behavior

Only string values are coerced

Known workarounds

Unknown

Related information

  • FSharp.Data.GraphQL.Server 1.0.5

xperiandri avatar Apr 15 '20 22:04 xperiandri

I guess there must be a case like | o when Enum.IsDefined(enumdef.Type, o) -> o before default

xperiandri avatar Apr 15 '20 22:04 xperiandri

Fixed in https://github.com/fsprojects/FSharp.Data.GraphQL/pull/283 @johnberzy-bazinga could you have a look?

xperiandri avatar Apr 15 '20 22:04 xperiandri