[英]Passing nested JSON as variable in Machinebox GraphQL mutation using golang
Hi there Golang experts,您好 Golang 专家,
I am using the Machinebox "github.com/machinebox/graphql" library in golang as client for my GraphQL server.我正在使用 golang 中的 Machinebox“github.com/machinebox/graphql”库作为我的 GraphQL 服务器的客户端。
Mutations with single layer JSON variables work just fine单层 JSON 变量的突变工作得很好
I am, however, at a loss as to how to pass a nested JSON as a variable但是,我不知道如何将嵌套的 JSON 作为变量传递
With a single layer JSON I simply create a map[string]string
type and pass into the Var method.对于单层 JSON,我只需创建一个
map[string]string
类型并传递给 Var 方法。 This in turn populates my graphql $data
variable这反过来填充了我的 graphql
$data
变量
The machinebox (graphql.Request).Var method takes an empty interface{}
as value so the map[string]string
works fine. machinebox (graphql.Request).Var 方法采用空
interface{}
作为值,因此map[string]string
可以正常工作。 But embedded json simply throws an error.但是嵌入式 json 只会引发错误。
code:代码:
func Mutate(data map[string]string, mutation string) interface{} {
client := GQLClient()
graphqlRequest := graphql.NewRequest(mutation)
graphqlRequest.Var("data", data)
var graphqlResponse interface{}
if err := client.Run(context.Background(), graphqlRequest, &graphqlResponse); err != nil {
panic(err)
}
return graphqlResponse
}
Mutation:突变:
mutation createWfLog($data: WfLogCreateInput)
{
createWfLog (data: $data){
taskGUID {
id
status
notes
}
event
log
createdBy
}
}
data variable shape:数据变量形状:
{
"data": {
"event": "Task Create",
"taskGUID": {
"connect": {"id": "606f46cdbbe767001a3b4707"}
},
"log": "my log and information",
"createdBy": "calvin cheng"
}
}
As mentioned, the embedded json (value of taskGUID) presents the problem.如前所述,嵌入式 json(taskGUID 的值)存在问题。 If value was simple string type, it's not an issue.
如果 value 是简单的字符串类型,那不是问题。
Any help appreciated任何帮助表示赞赏
Calvin卡尔文
I have figured it out... and it is a case of my noobness with Golang.我已经弄清楚了……这是我对 Golang 的菜鸟的一个例子。
I didn't need to do all this conversion of data or any such crazy type conversions.我不需要进行所有这些数据转换或任何此类疯狂的类型转换。 For some reason I got in my head everything HAD to be a map for the machinebox Var(key, value) to work
出于某种原因,我想到所有东西都必须是 map 机器箱 Var(key, value) 才能工作
thanks to xarantolus 's referenced site I was able to construct a proper strut.感谢xarantolus的引用站点,我能够构建一个合适的支柱。 I populated the strut with my variable data (which was a nested json) and the mutation ran perfectly!
我用我的变量数据(这是一个嵌套的 json)填充了支柱,突变运行得很好!
thanks!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.