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"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"storj.io/storj/pkg/satellite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Query is immutable graphql request
|
|
|
|
Query = "query"
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
userQuery = "user"
|
|
|
|
projectQuery = "project"
|
|
|
|
myProjectsQuery = "myProjects"
|
|
|
|
tokenQuery = "token"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rootQuery creates query for graphql populated by AccountsClient
|
|
|
|
func rootQuery(service *satellite.Service, types Types) *graphql.Object {
|
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: Query,
|
|
|
|
Fields: graphql.Fields{
|
|
|
|
userQuery: &graphql.Field{
|
2018-11-21 15:51:43 +00:00
|
|
|
Type: types.User(),
|
2018-11-14 10:50:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
id, _ := p.Args[fieldID].(string)
|
|
|
|
|
|
|
|
idBytes, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
return service.GetUser(p.Context, *idBytes)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
projectQuery: &graphql.Field{
|
|
|
|
Type: types.Project(),
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldID: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
inputID, _ := p.Args[fieldID].(string)
|
|
|
|
|
|
|
|
id, err := uuid.Parse(inputID)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
return service.GetProject(p.Context, *id)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
myProjectsQuery: &graphql.Field{
|
|
|
|
Type: graphql.NewList(types.Project()),
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return service.GetUsersProjects(p.Context)
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
tokenQuery: &graphql.Field{
|
2018-11-22 10:38:58 +00:00
|
|
|
Type: types.Token(),
|
2018-11-14 10:50:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
fieldEmail: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
fieldPassword: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
email, _ := p.Args[fieldEmail].(string)
|
|
|
|
pass, _ := p.Args[fieldPassword].(string)
|
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
token, err := service.Token(p.Context, email, pass)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:38:58 +00:00
|
|
|
return tokenWrapper{Token: token}, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|