将”Convert String! in [String!]”翻译为中文:将”String!”转换为”[String!]”。

huangapple go评论78阅读模式
英文:

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!]"
}

huangapple
  • 本文由 发表于 2021年10月20日 18:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/69644654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定