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 (
|
2018-11-22 10:38:58 +00:00
|
|
|
"context"
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
"github.com/graphql-go/graphql"
|
2018-11-21 15:51:43 +00:00
|
|
|
|
|
|
|
"storj.io/storj/pkg/satellite"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-11-21 15:51:43 +00:00
|
|
|
userType = "user"
|
|
|
|
userInputType = "userInput"
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
fieldID = "id"
|
|
|
|
fieldEmail = "email"
|
|
|
|
fieldPassword = "password"
|
|
|
|
fieldFirstName = "firstName"
|
|
|
|
fieldLastName = "lastName"
|
2018-11-14 15:13:13 +00:00
|
|
|
fieldCreatedAt = "createdAt"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
// graphqlUser creates *graphql.Object type representation of satellite.User
|
|
|
|
func graphqlUser(service *satellite.Service, types Types) *graphql.Object {
|
2018-11-14 10:50:15 +00:00
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: userType,
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
fieldID: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldEmail: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldFirstName: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldLastName: &graphql.Field{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
2018-11-14 15:13:13 +00:00
|
|
|
fieldCreatedAt: &graphql.Field{
|
|
|
|
Type: graphql.DateTime,
|
|
|
|
},
|
2018-11-21 15:51:43 +00:00
|
|
|
companyType: &graphql.Field{
|
|
|
|
Type: types.Company(),
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2018-11-22 10:38:58 +00:00
|
|
|
user, _ := p.Source.(*satellite.User)
|
|
|
|
|
|
|
|
// if root value contains context used instead one from params
|
|
|
|
// as RootValue seems like the only way to pass additional from parent resolver
|
|
|
|
rootValue := p.Info.RootValue.(map[string]interface{})
|
|
|
|
|
|
|
|
ctx := rootValue["context"]
|
|
|
|
if ctx != nil {
|
|
|
|
return service.GetCompany(ctx.(context.Context), user.ID)
|
|
|
|
}
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
return service.GetCompany(p.Context, user.ID)
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2018-11-21 15:51:43 +00:00
|
|
|
|
|
|
|
// graphqlUserInput creates graphql.InputObject type needed to register/update satellite.User
|
|
|
|
func graphqlUserInput(types Types) *graphql.InputObject {
|
|
|
|
return graphql.NewInputObject(graphql.InputObjectConfig{
|
|
|
|
Name: userInputType,
|
|
|
|
Fields: graphql.InputObjectConfigFieldMap{
|
|
|
|
fieldEmail: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldFirstName: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldLastName: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
fieldPassword: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: graphql.String,
|
|
|
|
},
|
|
|
|
companyType: &graphql.InputObjectFieldConfig{
|
|
|
|
Type: types.CompanyInput(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserInput encapsulates satellite.UserInfo and satellite.CompanyInfo which is used in user related queries
|
|
|
|
type UserInput struct {
|
|
|
|
User satellite.UserInfo
|
|
|
|
Company satellite.CompanyInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromMapUserInfo creates UserInput from input args
|
|
|
|
func fromMapUserInfo(args map[string]interface{}) (input UserInput) {
|
|
|
|
input.User.Email, _ = args[fieldEmail].(string)
|
|
|
|
input.User.FirstName, _ = args[fieldFirstName].(string)
|
|
|
|
input.User.LastName, _ = args[fieldLastName].(string)
|
|
|
|
input.User.Password, _ = args[fieldPassword].(string)
|
|
|
|
|
|
|
|
companyArgs, ok := args[companyType].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
input.Company = fromMapCompanyInfo(companyArgs)
|
|
|
|
return
|
|
|
|
}
|