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

Automatically encode and decode input arguments?

Open klanthier opened this issue 5 years ago • 5 comments

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      

klanthier avatar Jul 05 '19 15:07 klanthier

This seems reasonable to me.

@ivelten @TOTBWF thoughts?

johnberzy-bazinga avatar Jul 10 '19 00:07 johnberzy-bazinga

Looks like could potentially be done using reflection magic and extension functions?

Swoorup avatar Jul 11 '19 06:07 Swoorup

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?

ivelten avatar Jul 12 '19 19:07 ivelten

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

klanthier avatar Jul 15 '19 19:07 klanthier

Implemented in #436 for input objects. The same approach can be used to construct custom objects from arguments by matching names

xperiandri avatar Nov 05 '23 23:11 xperiandri