genqlient icon indicating copy to clipboard operation
genqlient copied to clipboard

Document more clearly how to stringify large integers

Open msonawane opened this issue 3 years ago • 3 comments

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

msonawane avatar Oct 02 '21 07:10 msonawane

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
}

msonawane avatar Oct 02 '21 08:10 msonawane

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"

csilvers avatar Oct 04 '21 05:10 csilvers

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.

benjaminjkraft avatar Oct 05 '21 16:10 benjaminjkraft