Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document more clearly how to stringify large integers #128

Closed
msonawane opened this issue Oct 2, 2021 · 3 comments · Fixed by #322
Closed

Document more clearly how to stringify large integers #128

msonawane opened this issue Oct 2, 2021 · 3 comments · Fixed by #322
Labels
documentation Improvements or additions to documentation help wanted Issues that anyone could pick up and implement if useful to them

Comments

@msonawane
Copy link

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 msonawane added the enhancement New feature or request label Oct 2, 2021
@msonawane
Copy link
Author

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
}

@csilvers
Copy link
Member

csilvers commented Oct 4, 2021

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"

@benjaminjkraft
Copy link
Collaborator

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 benjaminjkraft changed the title stringified large integers need a way to add a json string tag for int64 field Document more clearly how to stringify large integers Oct 5, 2021
@benjaminjkraft benjaminjkraft added documentation Improvements or additions to documentation help wanted Issues that anyone could pick up and implement if useful to them and removed enhancement New feature or request labels Oct 5, 2021
benjaminjkraft added a commit that referenced this issue Feb 19, 2024
We've been in need of a documentation revamp for a while; a giant FAQ
was never the greatest structure and got worse as it grew. In this
commit I reorganize the documentation. Most of it is just moving around
existing text, but I added some new documentation here and there.

The changes:
- Many of the FAQ questions have moved to several new docs, on the
  client/runtime, handling your schema, and various operation types; the
  FAQ has answers to a few of the actually most frequent questions, as
  well as a few things that didn't fit elsewhere.
- We now have a `docs/README.md` which acts as an index, so we can just
  link to `/docs`.
- I lowercased the files that don't need match a GitHub convention, so
  it's a bit less yell-y.
- I added documentation on:
  - how we version genqlient (fixes #63)
  - newer options for optional types
  - a bit more on custom scalars and integer types (fixes #128)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Issues that anyone could pick up and implement if useful to them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants