[英]Is it possible to return objectType and list of objectType in graphql union using golang as a backend
type User {
ID int
Name String
}
type Error{
Field
Message
}
var ErrorType = graphql.NewObject(graphql.ObjectConfig{
Name: "ErrorType",
Fields: graphql.Fields{
"field": &graphql.Field{Type: graphql.String},
"messages": &graphql.Field{Type: graphql.NewList(graphql.String)},
},
})
var ErrorTypeList = graphql.NewObject(graphql.ObjectConfig{
Name: "ErrorTypeList",
Fields: graphql.Fields{
"errors": &graphql.Field{Type: graphql.NewList(ErrorType)},
},
})
graphql.NewUnion(graphql.UnionConfig{
Name: "Result",
Types: []*graphql.Object{
userType, ErrorTypeList,
},
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
if _, ok := p.Value.(*User); ok {
return userType
}
return ErrorTypeList
},
})
I am trying to return a list of error along with regular userType so that I can query something like the following but I am not getting any result for ErrorTypeList我正在尝试返回错误列表以及常规 userType 以便我可以查询如下内容,但我没有得到 ErrorTypeList 的任何结果
mutation ($u: UserInput) {
userCreate(user: $u) {
... on User {
firstName
lastName
}
... on ErrorTypeList { // Getting no result here.
errors{
field
messages
}
}
}
}
If I change ErrorTypeList to ErrorType above then I get result just fine.如果我将上面的 ErrorTypeList 更改为 ErrorType,那么我得到的结果就好了。 Is there a way to send back List of ErrorType?
有没有办法发回错误类型列表? Following is my resolve
以下是我的决心
graphql.Field{
Type: UserResultType,
Args: graphql.FieldConfigArgument{
"u": &graphql.ArgumentConfig{
Type: userInputType,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
u, err := UserResolver(p) // returns user and err msg
if err != nil {
return []Error{
Error{Field: "form", Messages: []string{err}},
}, nil
}
return u, nil
},
}
Figured it out.弄清楚了。 Needed to use following struct as return in Resolve:
需要在 Resolve 中使用以下结构作为返回:
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
u, err := UserResolver(p) // returns user and err msg
if err != nil {
ret := struct {
Errors []Error
}{
[]Error{
Error{Field: "form", Messages: []string{"testing"}},
},
}
return ret,nil
}
return u, nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.