Create graphql query for getting user credit usage (#2255)

* set up creditUsage query

* rename function name to follow convention

* remove unused code

* add coipyright

* fix linting
This commit is contained in:
Yingrong Zhao 2019-06-20 09:09:50 -04:00 committed by GitHub
parent e5fd0287e4
commit 1c2067a090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 0 deletions

View File

@ -24,6 +24,8 @@ const (
ProjectQuery = "project"
// MyProjectsQuery is a query name for projects related to account
MyProjectsQuery = "myProjects"
// CreditUsageQuery is a query name for credit usage related to an user
CreditUsageQuery = "creditUsage"
// TokenQuery is a query name for token
TokenQuery = "token"
// ForgotPasswordQuery is a query name for password recovery request
@ -81,6 +83,12 @@ func rootQuery(service *console.Service, mailService *mailservice.Service, types
return service.GetUsersProjects(p.Context)
},
},
CreditUsageQuery: &graphql.Field{
Type: types.creditUsage,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return service.GetUserCreditUsage(p.Context)
},
},
TokenQuery: &graphql.Field{
Type: types.token,
Args: graphql.FieldConfigArgument{

View File

@ -19,6 +19,7 @@ type TypeCreator struct {
token *graphql.Object
user *graphql.Object
creditUsage *graphql.Object
project *graphql.Object
projectUsage *graphql.Object
bucketUsage *graphql.Object
@ -56,6 +57,11 @@ func (c *TypeCreator) Create(log *zap.Logger, service *console.Service, mailServ
return err
}
c.creditUsage = graphqlCreditUsage()
if err := c.creditUsage.Error(); err != nil {
return err
}
c.projectUsage = graphqlProjectUsage()
if err := c.projectUsage.Error(); err != nil {
return err

View File

@ -0,0 +1,36 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleql
import (
"github.com/graphql-go/graphql"
)
const (
// CreditUsageType is a graphql type for user credit
CreditUsageType = "creditUsage"
// FieldAvailableCredit is a field name for available credit
FieldAvailableCredit = "availableCredit"
// FieldUsedCredit is a field name for used credit
FieldUsedCredit = "usedCredit"
// FieldReferred is a field name for total referred number
FieldReferred = "referred"
)
func graphqlCreditUsage() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: CreditUsageType,
Fields: graphql.Fields{
FieldAvailableCredit: &graphql.Field{
Type: graphql.Int,
},
FieldUsedCredit: &graphql.Field{
Type: graphql.Int,
},
FieldReferred: &graphql.Field{
Type: graphql.Int,
},
},
})
}

View File

@ -95,3 +95,40 @@ export async function fetchBucketUsages(projectID: string, before: Date, cursor:
return result;
}
export async function fetchCreditUsage(): Promise<RequestResponse<CreditUsage>> {
let result: RequestResponse<CreditUsage> = {
errorMessage: '',
isSuccess: false,
data: {
referred: 0,
usedCredits: 0,
availableCredits: 0,
}
};
let response: any = await apollo.query(
{
query: gql(`
query {
creditUsage {
referred,
usedCredit,
availableCredit,
}
}`
),
fetchPolicy: 'no-cache',
errorPolicy: 'all',
}
);
if (response.errors) {
result.errorMessage = response.errors[0].message;
} else {
result.isSuccess = true;
result.data = response.data.creditUsage;
}
return result;
}

View File

@ -38,3 +38,9 @@ declare type BucketUsageCursor = {
limit: number,
page: number
};
declare type CreditUsage = {
referred: number,
usedCredits: number,
availableCredits: number,
};