Last active
August 26, 2018 22:56
-
-
Save rodrwan/c7edb3324422eb535257374ad67c06cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package queries | |
import ( | |
"errors" | |
"github.com/rodrwan/gateway" | |
"github.com/rodrwan/gateway/graph" | |
"github.com/rodrwan/gateway/graph/types" | |
"github.com/graphql-go/graphql" | |
) | |
// UserGraph fill graphql Field with data from postgres service. | |
func UserGraph(ctx *graph.Context) *graphql.Field { | |
return &graphql.Field{ | |
Type: types.User, | |
Description: "Get user by email", | |
Args: graphql.FieldConfigArgument{ | |
"email": &graphql.ArgumentConfig{ | |
Type: graphql.String, | |
Description: "return user information by email", | |
}, | |
}, | |
Resolve: func(params graphql.ResolveParams) (interface{}, error) { | |
email, ok := params.Args["email"].(string) | |
if !ok { | |
return nil, errors.New("Bad params") | |
} | |
opts := gateway.SetUserQueryOptions(&gateway.UserQueryOptions{ | |
Email: email, | |
}) | |
u, err := ctx.UserService.Get(opts) | |
if err != nil { | |
return nil, err | |
} | |
return u, nil | |
}, | |
} | |
} | |
// RootQuery expose UserQuery | |
func RootQuery(ctx *graph.Context) *graphql.Object { | |
return graphql.NewObject(graphql.ObjectConfig{ | |
Name: "UserQuery", | |
Fields: graphql.Fields{ | |
"user": UserGraph(ctx), | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment