You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Following the guide here I am trying to implement a custom scalar type BigNumber which maps to *big.Int in the standard library. This results in compiler errors in the generated code.
validation failed: packages.Load: /Users/alex/programming/gomod/gqlgen-todos/graph/generated/generated.go:1909:9: cannot use scalars.UnmarshalBigNumber(v) (value of type *big.Int) as big.Int value in return statement
/Users/alex/programming/gomod/gqlgen-todos/graph/generated/generated.go:1913:34: cannot use v (variable of type big.Int) as *big.Int value in argument to scalars.MarshalBigNumber
In my real application, we need to use *big.Int and cannot use big.Int. All functions and methods in the math/big package expect *big.Int so if my models use big.Int it makes it extremely hard to work with.
What did you expect?
Since I have defined custom marshaler and unmarshaler functions, I should be able to map BigNumber to *big.Int types in my models.
The models are actually being generated correctly. Here's what model/models_gen.go looks like:
typeTodostruct {
Number*big.Int`json:"number"`
}
The problem just lies in the generated code in graph/generated/generated.go. It looks like this can be resolved by changing the generated code so that it uses the correct types when calling scalars.UnmarshalBigNumber and scalars.MarshalBigNumber. I would suggest something like this:
package scalars
import (
"fmt""io""math/big""strconv""github.com/99designs/gqlgen/graphql"
)
// UnmarshalBigNumber converts scalar value of type BigNumber into a *big.Int.funcUnmarshalBigNumber(vinterface{}) (*big.Int, error) {
s, ok:=v.(string)
if!ok {
returnnil, fmt.Errorf("BigNumber must be a numerical string")
}
bigInt:=new(big.Int)
_, ok=bigInt.SetString(s, 10)
if!ok {
returnnil, fmt.Errorf("invalid BigNumber value: %q", s)
}
returnbigInt, nil
}
// MarshalBigNumber converts a *big.Int into a scalar value of type BigNumber.funcMarshalBigNumber(number*big.Int) graphql.Marshaler {
returngraphql.WriterFunc(func(w io.Writer) {
quotedString:=strconv.Quote(number.String())
_, _=w.Write([]byte(quotedString))
})
}
versions
gqlgen version? v0.11.3
go version? go version go1.14.3 darwin/amd64
dep or go modules? go modules
The text was updated successfully, but these errors were encountered:
albrow
changed the title
Cannot use custom scalars for third party pointer types
Cannot map custom scalars to third party pointer types
Jul 22, 2020
Depends on what you mean by "find a solution". IMO gqlgen should not produce code that doesn't compile, and what I shared should be considered a bug.
For my specific application I was able to work around this bug by defining my own BigNumber type and writing the MarshalGQL and UnmarshalGQL methods. This creates unnecessary complication because we have to convert between the BigNumber type and *big.Int in order to do any actual math or business logic.
What happened?
Following the guide here I am trying to implement a custom scalar type
BigNumber
which maps to*big.Int
in the standard library. This results in compiler errors in the generated code.Here is the problematic code:
In my real application, we need to use
*big.Int
and cannot usebig.Int
. All functions and methods in themath/big
package expect*big.Int
so if my models usebig.Int
it makes it extremely hard to work with.What did you expect?
Since I have defined custom marshaler and unmarshaler functions, I should be able to map
BigNumber
to*big.Int
types in my models.The models are actually being generated correctly. Here's what model/models_gen.go looks like:
The problem just lies in the generated code in graph/generated/generated.go. It looks like this can be resolved by changing the generated code so that it uses the correct types when calling
scalars.UnmarshalBigNumber
andscalars.MarshalBigNumber
. I would suggest something like this:Minimal graphql.schema and models to reproduce
See https://github.com/albrow/gqlgen-todos.
Schema:
Marshal and unmarshal functions:
versions
gqlgen version
?v0.11.3
go version
?go version go1.14.3 darwin/amd64
The text was updated successfully, but these errors were encountered: