genqlient
genqlient copied to clipboard
Document more clearly how to stringify large integers
Is your feature request related to a problem? Please describe. we should have a way to add json tags for bindings
given
bindings:
bigint:
type: int64
query GetUser($email: String!) {
users(where: {_and: {email: {_eq: $email}, deleted_at: {_is_null: true}}}) {
id
email
pwd_hash
name
}
}
generates
type GetUserUsers struct {
Id int64 `json:"-"`
Email string `json:"email"`
Pwd_hash string `json:"pwd_hash"`
Name string `json:"name"`
}
graphql server sends stringified json for bigint fields which can not be parsed.
Describe the solution you'd like If we had a way to add json tags for a binding we can add a tag `json:"id,string" to get parsed int64 back
as a workaround following function was used
func UnMarshalBigInt(b []byte, v *int64) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
num, err := strconv.ParseInt(s, 10, 64)
*v = num
return err
}
Right, I think you can just define your own marshal and unmarshal functions, and change your config to:
bindings:
bigint:
type: int64
marshaler: "github.com/your/project/path/to/module.MarshalBigInt"
unmarshaler: "github.com/your/project/path/to/module.UnMarshalBigInt"
Yep, that's the intended path! Documented further in docs/genqlient.yaml, but I'll leave this open to document it in a more obvious place.