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

80 lines
2.1 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"
"storj.io/common/uuid"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/mailservice"
)
const (
// Query is immutable graphql request.
Query = "query"
// ProjectQuery is a query name for project.
2019-01-24 16:26:36 +00:00
ProjectQuery = "project"
// OwnedProjectsQuery is a query name for projects owned by an account.
OwnedProjectsQuery = "ownedProjects"
// MyProjectsQuery is a query name for projects related to account.
2019-01-24 16:26:36 +00:00
MyProjectsQuery = "myProjects"
)
// rootQuery creates query for graphql populated by AccountsClient.
func rootQuery(service *console.Service, mailService *mailservice.Service, types *TypeCreator) *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: Query,
Fields: graphql.Fields{
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.FromString(inputID)
if err != nil {
return nil, err
}
project, err := service.GetProject(p.Context, id)
if err != nil {
return nil, err
}
return project, nil
},
},
OwnedProjectsQuery: &graphql.Field{
Type: types.projectsPage,
Args: graphql.FieldConfigArgument{
CursorArg: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(types.projectsCursor),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
cursor := fromMapProjectsCursor(p.Args[CursorArg].(map[string]interface{}))
page, err := service.GetUsersOwnedProjectsPage(p.Context, cursor)
return page, err
},
},
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) {
projects, err := service.GetUsersProjects(p.Context)
if err != nil {
return nil, err
}
return projects, nil
},
},
},
})
}