2018-11-15 12:00:08 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
package satelliteql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
2018-11-26 10:47:23 +00:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/satellite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Mutation is graphql request that modifies data
|
|
|
|
Mutation = "mutation"
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
createUserMutation = "createUser"
|
|
|
|
createProjectMutation = "createProject"
|
|
|
|
deleteProjectMutation = "deleteProject"
|
|
|
|
updateProjectMutation = "updateProject"
|
2018-11-21 15:51:43 +00:00
|
|
|
|
|
|
|
input = "input"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rootMutation creates mutation for graphql populated by AccountsClient
|
|
|
|
func rootMutation(service *satellite.Service, types Types) *graphql.Object {
|
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: Mutation,
|
|
|
|
Fields: graphql.Fields{
|
2018-11-21 15:51:43 +00:00
|
|
|
createUserMutation: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
2018-11-14 10:50:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2018-11-21 15:51:43 +00:00
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.UserInput()),
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-21 15:51:43 +00:00
|
|
|
// creates user and company from input params and returns userID if succeed
|
2018-11-14 10:50:15 +00:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-11-21 15:51:43 +00:00
|
|
|
var userInput = fromMapUserInfo(p.Args[input].(map[string]interface{}))
|
2018-11-14 10:50:15 +00:00
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
user, err := service.CreateUser(
|
2018-11-14 10:50:15 +00:00
|
|
|
p.Context,
|
2018-11-21 15:51:43 +00:00
|
|
|
userInput.User,
|
|
|
|
userInput.Company,
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-11-21 15:51:43 +00:00
|
|
|
return "", err
|
2018-11-14 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
return user.ID.String(), nil
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-11-26 10:47:23 +00:00
|
|
|
// creates project from input params
|
|
|
|
createProjectMutation: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.ProjectInput()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
var projectInput = fromMapProjectInfo(p.Args[input].(map[string]interface{}))
|
|
|
|
|
|
|
|
return service.CreateProject(p.Context, projectInput)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// deletes project by id, taken from input params
|
|
|
|
deleteProjectMutation: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
inputID := p.Args[fieldID].(string)
|
|
|
|
projectID, err := uuid.Parse(inputID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, service.DeleteProject(p.Context, *projectID)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// updates project
|
|
|
|
updateProjectMutation: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
input: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(types.ProjectInput()),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
var projectInput = fromMapProjectInfo(p.Args[input].(map[string]interface{}))
|
|
|
|
|
|
|
|
inputID := p.Args[fieldID].(string)
|
|
|
|
projectID, err := uuid.Parse(inputID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service.UpdateProject(p.Context, *projectID, projectInput)
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|