-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
uint32 causes type mismatch #79
Comments
Not currently supported, but I'll take a look when I get a chance. In the meantime, you may need use the generated model and explicitly cast between them |
@vektah no problem, I'll see how I can do it. do you have any pointers in the code base on how to marshal and unmarshal integers? |
There are some general notes on how the marshaling works in the docs It might be enough to set the field.CastType in here somewhere for all the various int types, but I'm not 100%. Otherwise you would need to define marshalers over here for all the different types And then in the binding code check for the various int types on the field and override the field.Marshaler to use the correct type. I guess the bigger question is how should overflows be handled etc, on the wire anything above an int32 probably isn't going to be read by many clients. |
Yeah, according to the spec The spec though is conservative to support languages that don't have unsigned integers or 64 bit integers (like Javascript). I would probably add the possibility to use them being very clear to the developer that if they don't control the client they might incur in overflow issues because the target language doesn't handle integers that big. |
Thinking about this some more, you probably should just define a custom scalar marshshaler for eg: scalar Uint
type Model {
interval: Uint!
} package mytypes
import (
"fmt"
"io"
"strconv"
)
func MarshalUInt(i uint64) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, fmt.Sprintf("%d",i))
})
}
func UnmarshalUint(v interface{}) (uint64, error) {
switch v := v.(type) {
case int:
return v.(uint64), nil
default:
return 0, fmt.Errorf("%T is not an int", v)
}
} I think that cast from int to uint64 is safe on 64 bit systems (it will wrap, but the negative will unwrap?...), but its worth testing some large uints make it through serialisation correctly. types.json {
"Uint": "mytypes.Uint"
} |
It's working! Thank you! This is how I've done it for now: scalar UInt32
type Model {
interval: UInt32!
} import (
"fmt"
"io"
graphql "github.com/vektah/gqlgen/graphql"
)
func MarshalUInt32(i uint32) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
io.WriteString(w, fmt.Sprintf("%d", i))
})
}
func UnmarshalUInt32(v interface{}) (uint32, error) {
switch v := v.(type) {
case int:
return uint32(v), nil
default:
return 0, fmt.Errorf("%T is not an uint32", v)
}
} It was super easy to hookup resolvers to the domain logic and the query tool builtin is very handy! Thank you! |
I had issue with json.numbers so I added more cases.
|
Expected Behaviour
I would expect that having a model defined as:
and a schema
and running:
gqlgen -typemap types.json -schema schema.graphql type mismatch on Model.interval, expected int got uint32
would not result in that error.
Actual Behavior
It seems that the generator doesn't support unsigned integers but maybe I'm missing something.
Minimal graphql.schema and models to reproduce
See above
The text was updated successfully, but these errors were encountered: