Skip to content

Instantly share code, notes, and snippets.

@rodrwan
Last active August 26, 2018 22:56
Show Gist options
  • Save rodrwan/c7edb3324422eb535257374ad67c06cb to your computer and use it in GitHub Desktop.
Save rodrwan/c7edb3324422eb535257374ad67c06cb to your computer and use it in GitHub Desktop.
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