我正在尝试将对象作为参数传递给查询(而不是标量)。从文档看来,这应该是可能的,但我无法弄清楚如何使它工作。
我正在使用graphql-go,这是测试架构:
var fileDocumentType = graphql.NewObject(graphql.ObjectConfig{
Name: "FileDocument",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if fileDoc, ok := p.Source.(data_format.FileDocument); ok {
return fileDoc.Id, nil
}
return "", nil
},
},
"tags": &graphql.Field{
Type: graphql.NewList(tagsDataType),
Args: graphql.FieldConfigArgument{
"tags": &graphql.ArgumentConfig{
Type: tagsInputType,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
fmt.Println(p.Source)
fmt.Println(p.Args)
if fileDoc, ok := p.Source.(data_format.FileDocument); ok {
return fileDoc.Tags, nil
}
return nil, nil
},
},
},
})
我试图使用的输入类型(我已尝试过InputObject和标准对象)
var tagsInputType = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "tagsInput",
Fields: graphql.Fields{
"keyt": &graphql.Field{
Type: graphql.String,
},
"valuet": &graphql.Field{
Type: graphql.String,
},
},
})
这是我用来测试的graphql查询:
{
list(location:"blah",rule:"blah")
{
id,tags(tags:{keyt:"test",valuet:"test"})
{
key,
value
},
{
datacentre,
handlerData
{
key,
value
}
}
}
}
我收到以下错误:
wrong result, unexpected errors: [Argument "tags" has invalid value {keyt: "test", valuet: "test"}.
In field "keyt": Unknown field.
In field "valuet": Unknown field.]
问题是,当我将类型更改为字符串时,它工作正常。如何将对象用作输入arg?
谢谢!
答案 0 :(得分:16)
有同样的问题。这是我通过graphql-go源找到的内容。
Fields
的{{1}}必须为InputObject
或InputObjectConfigFieldMap
,才能使pkg正常工作。
所以InputObjectConfigFieldMapThunk
看起来像这样:
InputObject
修改Hello World示例以获取var inputType = graphql.NewInputObject(
graphql.InputObjectConfig{
Name: "MyInputType",
Fields: graphql.InputObjectConfigFieldMap{
"key": &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
},
},
)
:
Input Object