Skip to content

Commit 3bf509e

Browse files
xperiandriViktor Tochonov
authored andcommitted
Implemented ObjectListFilter parsing from variable
1 parent b169b71 commit 3bf509e

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/FSharp.Data.GraphQL.Server.Middleware/SchemaDefinitions.fs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
namespace FSharp.Data.GraphQL.Server.Middleware
22

33
open System
4+
open System.Collections.Generic
5+
open System.Collections.Immutable
6+
open System.Text.Json
47
open FSharp.Data.GraphQL
58
open FSharp.Data.GraphQL.Types
69
open FSharp.Data.GraphQL.Ast
710
open FSharp.Data.GraphQL.Errors
811

9-
1012
/// Contains customized schema definitions for extensibility features.
1113
[<AutoOpen>]
1214
module SchemaDefinitions =
@@ -117,6 +119,18 @@ module SchemaDefinitions =
117119
// | :? ObjectListFilter as x -> Ok x
118120
// | _ -> Error [{ new IGQLError with member _.Message = $"Cannot coerce ObjectListFilter output. '%s{x.GetType().FullName}' is not 'ObjectListFilter'" }]
119121

122+
// TODO: Move to shared and make public
123+
let rec private jsonElementToInputValue (element : JsonElement) =
124+
match element.ValueKind with
125+
| JsonValueKind.Null -> NullValue
126+
| JsonValueKind.True -> BooleanValue true
127+
| JsonValueKind.False -> BooleanValue false
128+
| JsonValueKind.String -> StringValue (element.GetString ())
129+
| JsonValueKind.Number -> FloatValue (element.GetDouble ())
130+
| JsonValueKind.Array -> ListValue (element.EnumerateArray () |> Seq.map jsonElementToInputValue |> List.ofSeq)
131+
| JsonValueKind.Object -> ObjectValue (element.EnumerateObject () |> Seq.map (fun p -> p.Name, jsonElementToInputValue p.Value) |> Map.ofSeq)
132+
| _ -> raise (NotSupportedException "Unsupported JSON element type")
133+
120134
/// Defines an object list filter for use as an argument for filter list of object fields.
121135
let ObjectListFilter : ScalarDefinition<ObjectListFilter> =
122136
{ Name = "ObjectListFilter"
@@ -126,5 +140,5 @@ module SchemaDefinitions =
126140
CoerceInput =
127141
(function
128142
| InlineConstant c -> coerceObjectListFilterInput c
129-
| Variable _ -> raise <| NotSupportedException "List filter cannot be a variable") // TODO: Investigate
143+
| Variable json -> json |> jsonElementToInputValue |> coerceObjectListFilterInput)
130144
CoerceOutput = coerceObjectListFilterValue }

0 commit comments

Comments
 (0)