Skip to content

Commit

Permalink
feat(graphql): implements scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Nov 25, 2022
1 parent 119a10e commit 8f16c31
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 85 deletions.
18 changes: 18 additions & 0 deletions gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,21 @@ models:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
Time:
model:
- github.com/99designs/gqlgen/graphql.Time
Address:
model:
- okp4/nemeton-leaderboard/graphql/scalar.Address
ValoperAddress:
model:
- okp4/nemeton-leaderboard/graphql/scalar.ValoperAddress
PGPKeyID:
model:
- github.com/99designs/gqlgen/graphql.Uint64
URI:
model:
- okp4/nemeton-leaderboard/graphql/scalar.URI
Cursor:
model:
- okp4/nemeton-leaderboard/graphql/scalar.Cursor
153 changes: 87 additions & 66 deletions graphql/generated/generated.go

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions graphql/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions graphql/scalar/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package scalar

import (
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
"github.com/cosmos/cosmos-sdk/types"
)

func UnmarshalAccAddress(v interface{}) (types.AccAddress, error) {
strAddress, ok := v.(string)
if !ok {
return nil, fmt.Errorf("expect type 'string' and got type '%T'", v)
}

return types.AccAddressFromBech32(strAddress)
}

func MarshalAccAddress(addr types.AccAddress) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = w.Write(addr)
})
}

func UnmarshalValoperAddress(v interface{}) (types.ValAddress, error) {
strAddress, ok := v.(string)
if !ok {
return nil, fmt.Errorf("expect type 'string' and got type '%T'", v)
}

return types.ValAddressFromBech32(strAddress)
}

func MarshalValoperAddress(addr types.ValAddress) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = w.Write(addr)
})
}
24 changes: 24 additions & 0 deletions graphql/scalar/cursor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package scalar

import (
"fmt"
"io"

"github.com/99designs/gqlgen/graphql"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func UnmarshalCursor(v interface{}) (primitive.ObjectID, error) {
strCursor, ok := v.(string)
if !ok {
return primitive.ObjectID{}, fmt.Errorf("expect type 'string' and got type '%T'", v)
}

return primitive.ObjectIDFromHex(strCursor)
}

func MarshalCursor(c primitive.ObjectID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = w.Write([]byte(c.Hex()))
})
}
1 change: 1 addition & 0 deletions graphql/scalar/pgp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package scalar
31 changes: 31 additions & 0 deletions graphql/scalar/uri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package scalar

import (
"bytes"
"fmt"
"io"
"net/url"

"github.com/99designs/gqlgen/graphql"
)

func UnmarshalURI(v interface{}) (*url.URL, error) {
strURL, ok := v.(string)
if !ok {
return nil, fmt.Errorf("expect type 'string' and got type '%T'", v)
}

return url.Parse(strURL)
}

func MarshalURI(uri *url.URL) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
var buffer bytes.Buffer

buffer.WriteRune('"')
buffer.WriteString(uri.String())
buffer.WriteRune('"')

_, _ = buffer.WriteTo(w)
})
}
8 changes: 4 additions & 4 deletions graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ scalar Time
Represents an okp4 address as [Bech32](https://en.bitcoin.it/wiki/Bech32) format prefixed by the blockchain prefix.
e.g. `okp41jse8senm9hcvydhl8v9x47kfe5z82zmwtw8jvj`
"""
scalar Address
scalar AccAddress

"""
Represents an okp4 validator address as [Bech32](https://en.bitcoin.it/wiki/Bech32) format prefixed by the blockchain valoper prefix.
Expand All @@ -30,7 +30,7 @@ scalar PGPKeyID

"""
Represents an [Uniform Resource Identifier](https://fr.wikipedia.org/wiki/Uniform_Resource_Identifier) to permanently identify a resource.
e.g. ``
e.g. `https://okp4.network/`
"""
scalar URI

Expand Down Expand Up @@ -87,7 +87,7 @@ type Query {
cursor: Cursor
rank: Int
valoper: ValoperAddress
delegator: Address
delegator: AccAddress
discord: String
twitter: String
): Validator
Expand Down Expand Up @@ -320,7 +320,7 @@ type Validator {
"""
The address of the validator node delegator.
"""
delegator: Address!
delegator: AccAddress!

"""
The validator twitter account.
Expand Down
16 changes: 8 additions & 8 deletions graphql/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ package graphql
import (
"context"
"fmt"

"okp4/nemeton-leaderboard/app/nemeton"
"okp4/nemeton-leaderboard/graphql/generated"
"okp4/nemeton-leaderboard/graphql/model"

"github.com/cosmos/cosmos-sdk/types"
"go.mongodb.org/mongo-driver/bson/primitive"
)

// Blocks is the resolver for the blocks field.
Expand Down Expand Up @@ -48,7 +50,7 @@ func (r *queryResolver) Phases(ctx context.Context) (*model.Phases, error) {
}

// Board is the resolver for the board field.
func (r *queryResolver) Board(ctx context.Context, search *string, first *int, after *string) (*model.BoardConnection, error) {
func (r *queryResolver) Board(ctx context.Context, search *string, first *int, after *primitive.ObjectID) (*model.BoardConnection, error) {
panic(fmt.Errorf("not implemented: Board - board"))
}

Expand All @@ -58,7 +60,7 @@ func (r *queryResolver) ValidatorCount(ctx context.Context) (int, error) {
}

// Validator is the resolver for the validator field.
func (r *queryResolver) Validator(ctx context.Context, cursor *string, rank *int, valoper *string, delegator *string, discord *string, twitter *string) (*model.Validator, error) {
func (r *queryResolver) Validator(ctx context.Context, cursor *primitive.ObjectID, rank *int, valoper types.ValAddress, delegator *string, discord *string, twitter *string) (*model.Validator, error) {
panic(fmt.Errorf("not implemented: Validator - validator"))
}

Expand All @@ -71,8 +73,6 @@ func (r *Resolver) Phases() generated.PhasesResolver { return &phasesResolver{r}
// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

type (
phaseResolver struct{ *Resolver }
phasesResolver struct{ *Resolver }
queryResolver struct{ *Resolver }
)
type phaseResolver struct{ *Resolver }
type phasesResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }

0 comments on commit 8f16c31

Please sign in to comment.