Skip to content

Instantly share code, notes, and snippets.

@koblas
Created August 3, 2018 14:33
Show Gist options
  • Save koblas/e4c860fb9ac725f03316d18fe6d41790 to your computer and use it in GitHub Desktop.
Save koblas/e4c860fb9ac725f03316d18fe6d41790 to your computer and use it in GitHub Desktop.
User Update
package nresolver
import (
"context"
"github.com/graphql-go/graphql"
"github.com/koblas/projectx/server-go/pkg/contextkey"
"github.com/koblas/projectx/server-go/pkg/model"
"github.com/koblas/projectx/server-go/pkg/service"
)
func init() {
type userUpdateResponse struct {
User *model.User
ValidationErrors []*ValidationError `json:"validationErrors"`
}
userUpdateResponseQL := graphql.NewObject(graphql.ObjectConfig{
Name: "UserUpdateResponse",
Description: "User Update Response",
Fields: graphql.Fields{
"user": &graphql.Field{
Type: userObjectQL,
Description: "Updated user if no errors",
},
"validationErrors": &graphql.Field{
Type: graphql.NewList(validationErrorQL),
Description: "Field validation errors",
},
},
})
userUpdateInputQL := graphql.NewInputObject(graphql.InputObjectConfig{
Name: "UserUpdateInput",
Description: "User Update Parameters",
Fields: graphql.InputObjectConfigFieldMap{
"email": &graphql.InputObjectFieldConfig{
Type: graphql.String,
Description: "Email address if changed",
},
"name": &graphql.InputObjectFieldConfig{
Type: graphql.String,
Description: "Name if changed",
},
"password": &graphql.InputObjectFieldConfig{
Type: graphql.String,
Description: "existing password if changed",
},
"newpass": &graphql.InputObjectFieldConfig{
Type: graphql.String,
Description: "new password -- password required to change",
},
},
})
mutationConfig.AddFieldConfig("userUpdate", &graphql.Field{
Type: userUpdateResponseQL,
Args: graphql.FieldConfigArgument{
"input": &graphql.ArgumentConfig{
Type: &graphql.NonNull{
OfType: userUpdateInputQL,
},
},
},
Resolve: wrapIsAuth(wrapGraphQL(func(ctx context.Context, input struct {
Input struct {
Name *string `valid:"length(1|100)~Must not be empty"`
Email *string `valid:"email~Invalid email address"`
Password *string
Newpass *string `valid:"length(6|80)~Must be a minimum of 6 characters"`
}
}, validationErrors []*ValidationError) (interface{}, error) {
userService := service.UserServiceFromContext(ctx)
// most of the sausage factory is removed here...
user, err := userService.UpdateUser(ctx, user, service.UpdateUserInput{
Name: data.Name,
Email: data.Email,
Password: data.Newpass,
})
if err != nil {
return nil, err
}
return userUpdateResponse{User: user}, nil
})),
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment