0

I'm using the Go implemenatation of GraphQL.

How would you configure a mutation so that it can receive arguments with more than 1 level? For exemple, here is the list of arguments I would like to pass to a mutation CreateUser:

mutation createUser($user: CreateUser!) {
  createUser(input: $user)
}

{
  "user": {
    "name": {
      "first": "John",
      "last": "Doe"
    },
    "email": "john@doe.com"
  }
}

(Notice that I dont want to use firstname and lastname but a name object instead)

And this is my (unsuccessful) attempt so far:

var CreateUserInput = graphql.FieldConfigArgument{
    "input": &graphql.ArgumentConfig{
        Description: "Input for creating a new user",
        Type: graphql.NewNonNull(graphql.NewInputObject(graphql.InputObjectConfig{
            Name: "CreateUser",
            Fields: graphql.InputObjectConfigFieldMap{
                "name": &graphql.InputObjectFieldConfig{
                    Type: graphql.NewNonNull(graphql.NewInputObject(graphql.InputObjectConfig{
                        Fields: graphql.InputObjectConfigFieldMap{
                            "first": &graphql.InputObjectFieldConfig{
                                Type: graphql.NewNonNull(graphql.String),
                            },
                            "last": &graphql.InputObjectFieldConfig{
                                Type: graphql.NewNonNull(graphql.String),
                            },
                        },
                    })),
                },
                "email": &graphql.InputObjectFieldConfig{
                    Type: graphql.NewNonNull(graphql.String),
                },
            },
        })),
    },
}

Apparently the subfields first and last are not recognized as this is what I get when I run this mutation:

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$user\" got invalid value {\"email\":\"john@doe.com\",\"name\":{\"first\":\"john\",\"last\":\"doe\"}}.\nIn field \"name\": In field \"first\": Unknown field.\nIn field \"name\": In field \"last\": Unknown field.",
      "locations": [
        {
          "line": 1,
          "column": 21
        }
      ]
    }
  ]
}

Is this even possible?

EDIT: See comments in the accepted answer for the solution.

SebScoFr
  • 881
  • 1
  • 9
  • 24

1 Answers1

1

This are my first ever lines of Go but I will try to convey what I think the problem is.

First lets talk about the structure you want to be going for. I will use SDL here:

type Mutation {
  createUser(user: CreateUser!): Boolean! # Maybe return user type here?
}

input CreateUser {
  name: CreateUserName!
  email: String!
}

input CreateUserName {
  first: String!
  last: String!
}

Okay now that we know that we need two input types lets get started!

var CreateUserName = graphql.NewInputObject(graphql.InputObjectConfig{
    Name: "CreateUserName",
    Fields: graphql.InputObjectConfigFieldMap{
        "first": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
        "last": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
    },
})

var CreateUser = graphql.NewInputObject(graphql.InputObjectConfig{
    Name: "CreateUser",
    Fields: graphql.InputObjectConfigFieldMap{
        "name": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(CreateUserName),
        },
        "email": &graphql.InputObjectFieldConfig{
            Type: graphql.NewNonNull(graphql.String),
        },
    },
})

Now all that should be left is adding the mutation field to your mutation object type.

Herku
  • 7,198
  • 27
  • 36
  • If you look closely at my code, this is exactly what I have already. I haven’t created two separate input type but just one with everything in it but ultimately it’s the same thing. Also I haven’t mentioned it but I have obviously added the input object to the mutation type. – SebScoFr Sep 10 '18 at 20:34
  • You are missing for example the name property in the nested input type. Not sure of that is relevant. – Herku Sep 10 '18 at 20:36
  • I've just tested it and yes, I just needed to add the name property! Thanks :) I just wish the error message could have been more explicit haha – SebScoFr Sep 10 '18 at 20:46
  • 1
    I would suggest to group type definitions in the way I did so that those things are easier to catch :) – Herku Sep 10 '18 at 20:47