storj/satellite/console/consoleweb/consoleql/query.go

96 lines
2.5 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleql
import (
"github.com/graphql-go/graphql"
"github.com/skyrings/skyring-common/tools/uuid"
"storj.io/storj/satellite/console"
)
const (
// Query is immutable graphql request
Query = "query"
2019-01-24 16:26:36 +00:00
// UserQuery is a query name for user
UserQuery = "user"
// ProjectQuery is a query name for project
ProjectQuery = "project"
// MyProjectsQuery is a query name for projects related to account
MyProjectsQuery = "myProjects"
// TokenQuery is a query name for token
TokenQuery = "token"
)
// rootQuery creates query for graphql populated by AccountsClient
2019-04-04 15:56:20 +01:00
func rootQuery(service *console.Service, types *TypeCreator) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: Query,
Fields: graphql.Fields{
2019-01-24 16:26:36 +00:00
UserQuery: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: types.user,
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.ArgumentConfig{
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
id, err := uuidIDAuthFallback(p, FieldID)
if err != nil {
return nil, err
}
return service.GetUser(p.Context, *id)
},
},
2019-01-24 16:26:36 +00:00
ProjectQuery: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: types.project,
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldID: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
inputID, _ := p.Args[FieldID].(string)
id, err := uuid.Parse(inputID)
if err != nil {
return nil, err
}
return service.GetProject(p.Context, *id)
},
},
2019-01-24 16:26:36 +00:00
MyProjectsQuery: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: graphql.NewList(types.project),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return service.GetUsersProjects(p.Context)
},
},
2019-01-24 16:26:36 +00:00
TokenQuery: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: types.token,
Args: graphql.FieldConfigArgument{
2019-01-24 16:26:36 +00:00
FieldEmail: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
2019-01-24 16:26:36 +00:00
FieldPassword: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
2019-01-24 16:26:36 +00:00
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)
if err != nil {
return nil, err
}
2018-11-22 10:38:58 +00:00
return tokenWrapper{Token: token}, nil
},
},
},
})
}