Gqlgen: Support for int64 datatype

Created on 8 Nov 2019  路  3Comments  路  Source: 99designs/gqlgen

What happened?

Currently, int parameters defined in schema are mapped to 32-bit version. If the number is > 2^32 then it will be truncated.

What did you expect?

Can we map Int datatype to int64 by default?
If it is not feasible, any workaround to achieve this?

Minimal graphql.schema and models to reproduce

versions

  • gqlgen version?
  • go version?
  • dep or go modules?
stale

Most helpful comment

scalar Int64 # graphql.schema

```
//If the type referenced in .gqlgen.yml is a function that returns a marshaller we can use it to encode and decode
// onto any existing go type.
func MarshalInt64(t int64) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.FormatInt(t, 10))
})
}

// Unmarshal{Typename} is only required if the scalar appears as an input. The raw values have already been decoded
// from json into int/float64/bool/nil/map[string]interface/[]interface
func UnmarshalInt64(v interface{}) (int64, error) {
if res, ok := v.(json.Number); ok {
return res.Int64()
}
if res, ok := v.(string); ok {
return json.Number(res).Int64()
}
if res, ok := v.(int64); ok {
return res, nil
}
if res, ok := v.(*int64); ok {
return *res, nil
}
return 0, fmt.Errorf("could not convert %v of type %T to Int64", v, v)
} ```

Int64: model: github.com/firm/project/internal/handlers/graphql.Int64 # gqlgen.yml

All 3 comments

sure, you can map your owner type into graphql.

scalar Int64 # graphql.schema

```
//If the type referenced in .gqlgen.yml is a function that returns a marshaller we can use it to encode and decode
// onto any existing go type.
func MarshalInt64(t int64) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.FormatInt(t, 10))
})
}

// Unmarshal{Typename} is only required if the scalar appears as an input. The raw values have already been decoded
// from json into int/float64/bool/nil/map[string]interface/[]interface
func UnmarshalInt64(v interface{}) (int64, error) {
if res, ok := v.(json.Number); ok {
return res.Int64()
}
if res, ok := v.(string); ok {
return json.Number(res).Int64()
}
if res, ok := v.(int64); ok {
return res, nil
}
if res, ok := v.(*int64); ok {
return *res, nil
}
return 0, fmt.Errorf("could not convert %v of type %T to Int64", v, v)
} ```

Int64: model: github.com/firm/project/internal/handlers/graphql.Int64 # gqlgen.yml

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings