[英]Golang Mutation with a list as a param variable (GRAPHQL)
Basically what im trying to do is send a list of string ex: ["aa","bb","vv"] into a graphql Mutation field, currently this is my Mutation Schema 基本上,我想做的就是将一个字符串ex列表: [“ aa”,“ bb”,“ vv”]发送到graphql Mutation字段中,当前这是我的Mutation Schema
"listTest": &graphql.Field{
Type: QueryMessageType,
Args: graphql.FieldConfigArgument{
"listNew": &graphql.ArgumentConfig{
Description: "Example List of Json String",
Type: graphql.NewList(graphql.NewNonNull(graphql.String)),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
list := p.Args["listTest"].([]string)
return listTest(list)
},
},
and the Method listTest 和方法listTest
func listTest(testing[]string) (*QueryMessage, error) {
fmt.Println(testing)
return &QueryMessage{
QueryBody: "nothing to do here",
}, nil
}
However when i do the request in INSOMNIA the response is: 但是,当我在INSOMNIA中执行请求时,响应为:
{
"data": {
"listTest": null
},
"errors": [
{
"message": "interface conversion: interface {} is []interface {}, not []string",
"locations": []
}
]
}
and the request is this: 请求是这样的:
mutation{
listTest(listNew: ["aa","bb","vv"]){
querybody
}
}
can anyone tell me how to receive a List of String in my Go Server. 谁能告诉我如何在Go Server中接收字符串列表。 Thanks!
谢谢! :)
:)
UPDATE When i call a fmt.Println(p.Args["listTest"]) 更新当我调用fmt.Println(p.Args [“ listTest”])
the result is: [aa bb vv] 结果是: [aa bb vv]
SOLVED 解决了
Following the instructions of the voted answer, the script now do his job. 按照投票答案的指示,脚本现在可以完成工作。 This is the final result:
这是最终结果:
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
var groupIDs []string
for _, gid := range p.Args["list"].([]interface{}) {
groupIDs = append(groupIDs, gid.(string))
}
for _, final := range groupIDs {
fmt.Println(final)
}
return listTest(groupIDs)
},
and in the console i got this: 在控制台中,我得到了:
aa
bb
vv
Your problem, according to the error message, is with this line: 根据错误消息,您的问题在于以下行:
list := p.Args["listTest"].([]string)
p.Args["listTest"]
is returning []interface{}
. p.Args["listTest"]
返回[]interface{}
。
interface{}
can store any other type. interface{}
可以存储任何其他类型。 If you are familiar with java it's a little bit like Object
. 如果您熟悉Java,则有点像
Object
。
The problem here is that you have your field from p.Args["listTest"]
and you are trying to type assert it to []string
. 这里的问题是您有
p.Args["listTest"]
字段,并且正在尝试将其断言为[]string
。 This would work if the value stored in args were interface{}
(any). 如果存储在args中的值为
interface{}
(任意),则此方法有效。 But it's not, p.Args
(according to the error) holds []interface{}
. 但是不是,
p.Args
(根据错误)保存[]interface{}
。 This is a slice of interface values, where each of those can be anything (rather than a single interface value holding a slice of strings.) 这是接口值的一部分,其中的每个值都可以是任何值(而不是包含一个字符串切片的单个接口值)。
Instead try ranging over that list of interfaces, and type asserting each value. 而是尝试遍历该接口列表,然后键入断言每个值。
var strs []string
for _, v := range p.Args["list"].([]interface{}) {
strs = append(strs, v.(string))
}
Or investigate another way to set up the graphQL types so that you get the values back in a more useable way. 或研究另一种设置graphQL类型的方法,以便以更有用的方式取回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.