FSharp.Data.GraphQL
FSharp.Data.GraphQL copied to clipboard
Automatically encode and decode input arguments?
Looking quickly at how to accept incoming params, is there a current way that allows defining something more global. Currently that's how it's done:
let createUserArgs: InputFieldDef list =
[ Define.Input("fname", String); Define.Input("lname", String); ]
let createUser(ctx: ResolveFieldContext) =
let name = ctx.TryArg("fname")
match name with
| Some n ->
// ok...
| None ->
// failhere...
Now I was wondering if there is another way say we could have something like this which could potentially automatically encode the input params and decode them as well ? Something like this perhaps:
type CreateUserArgs = {
fname: string
lname: string
}
let createUser(ctx: ResolveFieldContext) =
let allArgsInput = ctx.getAllArgs<CreateUserArgs >()
// At this point the system would know about allArgsInput.fname and allArgsInput.lname
This seems reasonable to me.
@ivelten @TOTBWF thoughts?
Looks like could potentially be done using reflection magic and extension functions?
Yes, I think this makes a lot of sense. The user code can define and use input arguments with less code. Certainly, achievable using reflection or extensions. I am not sure about having more than one way to define and use input arguments though. If we do this, should we keep our actual approach alongside the new one?
Can I have a small example of how to use reflections, I might try to tinker something with it as an initial POC.
I tried looking online for an example of reflections, but they are really hard to find. If you could maybe give me a super small example, I will surely invest some time in this
Implemented in #436 for input objects. The same approach can be used to construct custom objects from arguments by matching names