简体   繁体   中英

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

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. 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: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM