Skip to content

Commit

Permalink
fix: allow checkpoint to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Jan 24, 2023
1 parent ed18007 commit 32c7abc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
6 changes: 3 additions & 3 deletions db/deals.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type FilterOptions struct {
Checkpoint *string
IsOffline *bool
TransferType *string
VerifiedDeal *bool
IsVerified *bool
}

func (d *DealsDB) newDealDef(deal *types.ProviderDealState) *dealAccessor {
Expand Down Expand Up @@ -323,9 +323,9 @@ func withSearchFilter(filter FilterOptions) (string, []interface{}) {
whereArgs = append(whereArgs, *filter.TransferType)
}

if filter.VerifiedDeal != nil {
if filter.IsVerified != nil {
statements = append(statements, "VerifiedDeal = ?")
whereArgs = append(whereArgs, *filter.VerifiedDeal)
whereArgs = append(whereArgs, *filter.IsVerified)
}

if len(statements) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions db/deals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func ToFilterOptions(filters map[string]interface{}) *FilterOptions {
if ok {
filter.TransferType = &tt
}
vd, ok := filters["VerifiedDeal"].(bool)
vd, ok := filters["IsVerified"].(bool)
if ok {
filter.VerifiedDeal = &vd
filter.IsVerified = &vd
}

return filter
Expand Down
15 changes: 9 additions & 6 deletions gql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type filterArgs struct {
Checkpoint gqltypes.Checkpoint
IsOffline graphql.NullBool
TransferType graphql.NullString
VerifiedDeal graphql.NullBool
IsVerified graphql.NullBool
}

type dealsArgs struct {
Expand Down Expand Up @@ -143,11 +143,14 @@ func (r *resolver) Deals(ctx context.Context, args dealsArgs) (*dealListResolver
query = *args.Query.Value
}

filter := &db.FilterOptions{
Checkpoint: &args.Filter.Checkpoint.String,
IsOffline: args.Filter.IsOffline.Value,
TransferType: args.Filter.TransferType.Value,
VerifiedDeal: args.Filter.VerifiedDeal.Value,
var filter *db.FilterOptions
if args.Filter != nil {
filter = &db.FilterOptions{
Checkpoint: args.Filter.Checkpoint.Value,
IsOffline: args.Filter.IsOffline.Value,
TransferType: args.Filter.TransferType.Value,
IsVerified: args.Filter.IsVerified.Value,
}
}

deals, count, more, err := r.dealList(ctx, query, filter, args.Cursor, offset, limit)
Expand Down
4 changes: 2 additions & 2 deletions gql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ input StorageAskUpdate {
}

input DealFilter {
Checkpoint: Checkpoint!
Checkpoint: Checkpoint
IsOffline: Boolean
TransferType: String
VerifiedDeal: Boolean
IsVerified: Boolean
}

type RootQuery {
Expand Down
33 changes: 25 additions & 8 deletions gql/types/checkpoint.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package types

import (
"encoding/json"
"fmt"

"github.com/filecoin-project/boost/storagemarket/types/dealcheckpoints"
)

type Checkpoint struct {
String string
Value *string
Set bool
}

// ImplementsGraphQLType maps this custom Go type
Expand All @@ -20,17 +22,32 @@ func (Checkpoint) ImplementsGraphQLType(name string) bool {
//
// This function will be called whenever you use the
// Checkpoint scalar as an input
func (n *Checkpoint) UnmarshalGraphQL(input interface{}) error {
cp, err := dealcheckpoints.FromString(input.(string))
n.String = cp.String()
return err
func (cp *Checkpoint) UnmarshalGraphQL(input interface{}) error {
cp.Set = true

if input == nil {
return nil
}

switch v := input.(type) {
case string:
_, err := dealcheckpoints.FromString(input.(string))
if err != nil {
return fmt.Errorf("invalid Checkpoint value: %T", v)
}
cp.Value = &v
return nil
default:
return fmt.Errorf("wrong type for Checkpoint: %T", v)
}
}

// MarshalJSON is a custom marshaler for Checkpoint
//
// This function will be called whenever you
// query for fields that use the Checkpoint type
func (n Checkpoint) MarshalJSON() ([]byte, error) {
json := fmt.Sprintf(`{"__typename": "Checkpoint", "n": "%s"}`, n.String)
return []byte(json), nil
func (cp Checkpoint) MarshalJSON() ([]byte, error) {
return json.Marshal(*cp.Value)
}

func (cp *Checkpoint) Nullable() {}

0 comments on commit 32c7abc

Please sign in to comment.