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

99 lines
2.6 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"
"storj.io/storj/satellite/rewards"
)
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"
// ActiveRewardQuery is a query name for current active reward offer
ActiveRewardQuery = "activeReward"
// CreditUsageQuery is a query name for credit usage related to an user
CreditUsageQuery = "creditUsage"
)
// 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
},
},
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
},
},
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)
offer, err := service.GetCurrentRewardByType(p.Context, rewards.OfferType(rewardType))
if err != nil {
return nil, err
}
return offer, nil
},
},
CreditUsageQuery: &graphql.Field{
Type: types.creditUsage,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
usage, err := service.GetUserCreditUsage(p.Context)
if err != nil {
return nil, err
}
return usage, nil
},
},
},
})
}