0

我正在使用GraphQL 的 Go 实现

您将如何配置突变,以便它可以接收超过 1 级的参数?例如,这是我想传递给突变 CreateUser 的参数列表:

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

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

(请注意,我不想使用名字姓氏,而是使用名称对象)

到目前为止,这是我的(不成功的)尝试:

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),
                },
            },
        })),
    },
}

显然,第一个最后一个子字段没有被识别,因为这是我在运行这个突变时得到的:

{
  "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
        }
      ]
    }
  ]
}

这甚至可能吗?

编辑:请参阅解决方案已接受答案中的评论。

4

1 回答 1

1

这是我第一次使用 Go,但我会尝试传达我认为的问题所在。

首先让我们谈谈你想要的结构。我将在这里使用 SDL:

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

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

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

好的,现在我们知道我们需要两种输入类型,让我们开始吧!

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),
        },
    },
})

现在剩下的就是将突变字段添加到您的突变对象类型中。

于 2018-09-10T20:21:35.720 回答