Skip to content

Commit

Permalink
feat: make address a graphql scalar
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 10, 2022
1 parent 5de61b6 commit a4c4d20
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
13 changes: 1 addition & 12 deletions gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ autobind:
# modelgen, the others will be allowed when binding to fields. Configure them to
# your liking
models:
ID:
model:
- github.com/99designs/gqlgen/graphql.ID
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
Int:
model:
- github.com/99designs/gqlgen/graphql.Int
- github.com/99designs/gqlgen/graphql.Int64
- github.com/99designs/gqlgen/graphql.Int32
Address:
model:
- github.com/99designs/gqlgen/graphql.String
- okp4/cosmos-faucet/pkg.Address
22 changes: 20 additions & 2 deletions graph/generated/generated.go

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

4 changes: 3 additions & 1 deletion graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#
# https://gqlgen.com/getting-started/

scalar Address

input SendInput {
toAddress: String!
toAddress: Address!
}

type Mutation {
Expand Down
22 changes: 22 additions & 0 deletions pkg/scalar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pkg

import (
"errors"
"io"

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

func MarshalAddress(a string) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = w.Write([]byte(a))
})
}

func UnmarshalAddress(v interface{}) (string, error) {
value, ok := v.(string)
if !ok {
return "", errors.New("address must be a string")
}
return value, nil
}

0 comments on commit a4c4d20

Please sign in to comment.