基本上我想做的是将一个字符串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)
},
},
和方法listTest
func listTest(testing[]string) (*QueryMessage, error) {
fmt.Println(testing)
return &QueryMessage{
QueryBody: "nothing to do here",
}, nil
}
但是,当我在INSOMNIA中执行请求时,响应为:
{
"data": {
"listTest": null
},
"errors": [
{
"message": "interface conversion: interface {} is []interface {}, not []string",
"locations": []
}
]
}
请求是这样的:
mutation{
listTest(listNew: ["aa","bb","vv"]){
querybody
}
}
谁能告诉我如何在Go Server中接收字符串列表。谢谢!:)
更新 当我调用fmt.Println(p.Args [“ listTest”])
结果是:[aa bb vv]
解决了
按照投票答案的指示,脚本现在可以完成工作。这是最终结果:
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)
},
在控制台中,我得到了这个:
aa
bb
vv
相关分类