登录
首页 >  Golang >  Go问答

变异参数对待对象

来源:stackoverflow

时间:2024-03-22 12:46:27 213浏览 收藏

Golang小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《变异参数对待对象》带大家来了解一下##content_title##,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!


问题内容

我正在使用 graphql 的 go 实现。

如何配置一个突变,以便它可以接收超过 1 级的参数? 例如,以下是我想要传递给突变 createuser 的参数列表:

mutation createuser($user: createuser!) {
  createuser(input: $user)
}

{
  "user": {
    "name": {
      "first": "john",
      "last": "doe"
    },
    "email": "[email protected]"
  }
}

(请注意,我不想使用 firstnamelastname 而是使用 name 对象)

这是我迄今为止的(不成功的)尝试:

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

显然子字段firstlast未被识别,因为这是我运行此突变时得到的结果:

{
  "data": null,
  "errors": [
    {
      "message": "Variable \"$user\" got invalid value {\"email\":\"[email protected]\",\"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
        }
      ]
    }
  ]
}

这可能吗?

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


解决方案


这是我第一次写 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),
        },
    },
})

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

终于介绍完啦!小伙伴们,这篇关于《变异参数对待对象》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>