graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.86k stars 838 forks source link

Array of Input / Mutation #640

Open ibadaboom opened 2 years ago

ibadaboom commented 2 years ago

Hello,

I got a question. I tried since hours to achieve to following:

Let's assume, we got an Object called User.

input User {
    id       int
    name string
}

What I want to get is the possibility. to bind "User" to an array like [User] as an input and also have User available as Object, Comming from node.js that was possible. But how this will work with your lib? Is this even possible? I Can only add 1 Input-Type of this name, resolving either as list or as object, but not both, because the name "UserInput" was already registered.

Like this: (This is just how I would do it in node.js)

mutation saveUser has inputObject of type User returning Bool
saveUser(input: User): Boolean
and mutation saveUsers has inputObject of type []User returning Bool
saveUsers(input: [User]): Boolean

Some go-code:

var UserIdInput = graphql.NewInputObject(graphql.InputObjectConfig{
    Name: "UserInput",
    Fields: graphql.InputObjectConfigFieldMap{
        "id": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.Int),
        },
        "name": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
    },
})

var USERINPUT = map[string]graphql.Input{
    "UserInput": UserInput,
    // I know I can do: graphql.newList(UserInput), but I want both be possible for that inputtype
}
bhoriuchi commented 2 years ago

I think the only way you can achieve this with this package is to use a custom scalar type for UserInput and use the serialize/parsevalue methods to perform the appropriate validation