英文:
Go - Graphql : Convert String! in [String!]
问题
我正在尝试使用这个客户端在Go中查询wikiJS Graphql API,并且在类型转换方面遇到了一些问题(可能是因为我在Go和Graphql方面的技能不足)。
我有以下结构体类型:
var query struct{
Pages struct{
List struct{
id graphql.Int
}`graphql:"list(tags: $tags)"`
}
}
variables := map[string]interface{}{
"tags": graphql.String(tag),
}
其中tag是一个普通的字符串,当我发送请求时,出现了以下错误:
"GraphQLError: Variable "$tags" of type "String!" used in position expecting type "[String!]"."
所以我的问题是,如何将String!
转换为[String!]
?
英文:
I'm trying to query wikiJS Graphql API in go using this client and I have a little problem of type conversion (maybe because of my lack of skills in go and graphql).
I have this struct type :
var query struct{
Pages struct{
List struct{
id graphql.Int
}`graphql:"list(tags: $tags)"`
}
}
variables := map[string]interface{}{
"tags": graphql.String(tag),
}
where tag is a normal string and when I send the request i have the following error :
"GraphQLError: Variable "$tags" of type "String!" used in position expecting type "[String!]".",
So My question is, how to convert a String!
into a [String!]
?
答案1
得分: 1
[String!]
是一个可选的字符串数组(非可选)。你可以使用字符串切片来表示一个数组,使用指向字符串切片的指针来表示一个可选数组。
x := []graphql.String{graphql.String(tag)} // 这适用于 "[String!]!"
variables := map[string]interface{}{
"tags": &x, // &x 适用于 "[String!]"
}
英文:
[String!]
is an optional array of (non-optional) strings. You may use a slice of strings for an array, and a pointer to a slice of string for an optional array.
x := []graphql.String{graphql.String(tag)} // This would be good for "[String!]!"
variables := map[string]interface{}{
"tags": &x, // &x is good for "[String!]"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论