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"
|
2019-07-02 15:36:54 +01:00
|
|
|
"storj.io/storj/satellite/rewards"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Query is immutable graphql request
|
|
|
|
Query = "query"
|
2019-01-24 16:26:36 +00:00
|
|
|
// ProjectQuery is a query name for project
|
|
|
|
ProjectQuery = "project"
|
|
|
|
// MyProjectsQuery is a query name for projects related to account
|
|
|
|
MyProjectsQuery = "myProjects"
|
2019-07-02 15:36:54 +01:00
|
|
|
// ActiveRewardQuery is a query name for current active reward offer
|
|
|
|
ActiveRewardQuery = "activeReward"
|
2019-06-20 14:09:50 +01:00
|
|
|
// CreditUsageQuery is a query name for credit usage related to an user
|
|
|
|
CreditUsageQuery = "creditUsage"
|
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
|
|
|
},
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
},
|
2019-07-02 15:36:54 +01:00
|
|
|
ActiveRewardQuery: &graphql.Field{
|
|
|
|
Type: types.reward,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
FieldType: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.Int),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
rewardType, _ := p.Args[FieldType].(int)
|
|
|
|
|
2019-09-04 16:02:39 +01:00
|
|
|
offer, err := service.GetCurrentRewardByType(p.Context, rewards.OfferType(rewardType))
|
|
|
|
if err != nil {
|
2020-01-20 13:02:44 +00:00
|
|
|
return nil, err
|
2019-09-04 16:02:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return offer, nil
|
2019-07-02 15:36:54 +01:00
|
|
|
},
|
|
|
|
},
|
2019-06-20 14:09:50 +01:00
|
|
|
CreditUsageQuery: &graphql.Field{
|
|
|
|
Type: types.creditUsage,
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-09-04 16:02:39 +01:00
|
|
|
usage, err := service.GetUserCreditUsage(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 usage, nil
|
2019-06-20 14:09:50 +01:00
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|