2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-15 12:00:08 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleql
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
2018-12-13 20:01:43 +00:00
|
|
|
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-04-10 20:16:10 +01:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-11 15:50:01 +01:00
|
|
|
// Query is immutable graphql request.
|
2018-11-14 10:50:15 +00:00
|
|
|
Query = "query"
|
2020-08-11 15:50:01 +01:00
|
|
|
// ProjectQuery is a query name for project.
|
2019-01-24 16:26:36 +00:00
|
|
|
ProjectQuery = "project"
|
2021-01-21 18:19:37 +00:00
|
|
|
// OwnedProjectsQuery is a query name for projects owned by an account.
|
|
|
|
OwnedProjectsQuery = "ownedProjects"
|
2020-08-11 15:50:01 +01:00
|
|
|
// MyProjectsQuery is a query name for projects related to account.
|
2019-01-24 16:26:36 +00:00
|
|
|
MyProjectsQuery = "myProjects"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// rootQuery creates query for graphql populated by AccountsClient.
|
2019-04-10 20:16:10 +01:00
|
|
|
func rootQuery(service *console.Service, mailService *mailservice.Service, types *TypeCreator) *graphql.Object {
|
2018-11-14 10:50:15 +00:00
|
|
|
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,
|
2018-11-26 10:47:23 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.ArgumentConfig{
|
2018-11-26 10:47:23 +00:00
|
|
|
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)
|
2018-11-26 10:47:23 +00:00
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
id, err := uuid.FromString(inputID)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
project, err := service.GetProject(p.Context, id)
|
2019-09-04 16:02:39 +01:00
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-09-04 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return project, nil
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-21 18:19:37 +00:00
|
|
|
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),
|
2018-11-26 10:47:23 +00:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-09-04 16:02:39 +01:00
|
|
|
projects, err := service.GetUsersProjects(p.Context)
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-09-04 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return projects, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|