Repository navigation
Open
Description
I've added a small random generator query to star-wars-api example:
let randoms =
let inputs = [
Define.Input ("count", IntType)
Define.Input ("range", Nullable (Define.InputObject("RandomRange", [
Define.Input ("min", IntType)
Define.Input ("max", IntType)
])))
]
let renderRandoms (ctx:ResolveFieldContext) (r:Root) =
// printfn "Random called with count:%A and range:%A" (ctx.Arg "count") (ctx.Arg "range")
// todo: ranges are not implemented, because ctx.Arg("range") is always empty (read below)
[ for i in 1..ctx.Arg("count") -> System.Random.Shared.Next() ]
Define.Field ("random", ListOf IntType, "Render random numbers", inputs, renderRandoms)and plugged it into existing Query:
let Query =
let inputs = [ Define.Input ("id", StringType) ]
Define.Object<Root> (
name = "Query",
fields =
[ Define.Field ("hero", Nullable HumanType, "Gets human hero", inputs, fun ctx _ -> getHuman (ctx.Arg ("id")))
Define.Field ("droid", Nullable DroidType, "Gets droid", inputs, (fun ctx _ -> getDroid (ctx.Arg ("id"))))
Define.Field ("planet", Nullable PlanetType, "Gets planet", inputs, fun ctx _ -> getPlanet (ctx.Arg ("id")))
Define.Field ("characters", ListOf CharacterType, "Gets characters", (fun _ _ -> characters))
randoms // <-- here
])Now I can say
query { random(count:5) }and get
{ "documentId": -804090008,
"data": {
"random": [1773619870, 512090916, 1972640319, 301711584, 480788316]
}}Problem
When I call
query { random(count:5, range:{min:10, max:20}) }The ctx.args("range") is always passed as an empty unpopulated obj() without any type or properties,
whereas ctx.args("count") works as expected.
Metadata
Metadata
Assignees
Labels
No labels
Activity
xperiandri commentedon Apr 6, 2024
That happens because you need to declare
As
Define.InputObject()equals toDefine.InputObject<obj>().There is no functionality to deserialize into
IReadOnlyDictionary<string, obj>if no type parameter is specified.xperiandri commentedon Apr 6, 2024
There are 2 options to fix
Define.InputObject<obj>:IReadOnlyDictionary<string, obj>pkese commentedon Apr 6, 2024
@xperiandri thanks, I appreciate your help.
Defining a Range type works.
My problem however is that in my real app, I don't have a fixed input type known a compile time (available input values depend on dynamic database schema).
I suppose I could dynamically create a new type and then use reflection to call the
Define.InputObjectwith proper generic instantiation with that dynamic type (is this the right path or is there an easier workarond)?A nice option (from the library side) besides
IReadOnlyDictionary<string, obj>would be to allowDefine.InputObject<JsonElement>. Probably anything that can be expressed as an input expression should be 1:1 mappable to Json.xperiandri commentedon Apr 6, 2024
Have you tried that? Doesn’t it work?
pkese commentedon Apr 6, 2024
I've tried with
... but it's resulting in a runtime error when the app starts:
xperiandri commentedon Apr 6, 2024
What about Dictionary?
xperiandri commentedon Apr 6, 2024
Or dynamic
pkese commentedon Apr 6, 2024
Define.InputObject<System.Collections.Generic.Dictionary<string,obj>>compiles fine but at query time results inctx.Arg("range")being an empty sequence.Similarly
Define.InputObject<System.Dynamic.DynamicObject>compiles but at runtime returns a Dynamic object with no members.xperiandri commentedon Apr 6, 2024
Thanks for testing!
The input object logic is here
We can check if the type is some known dynamic type or object then switch to a dictionary logic
Add support for generic IDictionary<string,obj> query inputs - relate…
pkese commentedon Apr 6, 2024
Thanks @xperiandri, based on your hints, I managed to patch the code in #475 ... and that fixes my issue.
(ignore the link just above this comment: I somehow managed to commit too many files into the pool request, so I closed that request and made a new one).