-
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
using sqlboiler, null.string fields in structs, field resolvers created #646
Comments
You will need to implement a custom scalar to We could potentially be better at detecting that your type mapping wont work at runtime and not generate invalid code. |
@mathewbyrne oh, thanks a lot for the hint. I have read but I am too inexperienced to be able to create one myself. How can I create a custom scalar for Do you have a nullable string scalar already? |
@mathewbyrne I have made progress, but something is still not entirely clear to me. As soon I understand and solve the problem I can write a small Wiki for newbies like me. I wrote this: package graphql
import (
"github.com/99designs/gqlgen/graphql"
"github.com/volatiletech/null"
"io"
"strconv"
)
func MarshalMyNullString(ns null.String) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
if !ns.Valid {
_, err := w.Write([]byte("null")) // Is this correct?
DieIfErr(err)
}
_, err := io.WriteString(w, strconv.Quote(ns.String))
DieIfErr(err)
})
}
func UnmarshalMyNullString(v interface{}) (null.String, error) {
// What to do here?
// return v.(null.String), nil
} TWO QUESTIONS:
Maybe I don't understand the point entirely here, but I'm close, @vektah. Thanks a lot and please appreciate my commitment. |
Probably for most cases, but there are some fairly sublte differences between strconv.Quote and valid json strings. Would probably recommend just casing and calling the default string implementation. func MarshalMyNullString(ns null.String) graphql.Marshaler {
if !ns.Valid {
// this is also important, so we can detect if this scalar is used in a not null context and return an appropriate error
return graphql.Null
}
return graphql.MarshalString(ns.String)
}
Yeah, you should implement both. Scalars can be used in input types, arguments and directives which all require unmarshal. Its just the reverse operation, at its simplest: func UnmarshalMyNullString(v interface{}) (null.String, error) {
if v == nil {
return null.String{Valid: false}, nil
}
// again you can delegate to the default implementation to save yourself some work.
s, err := graphql.UnmarshalString(v)
return null.String{Value: s}, err
}
By default the first implementation for a model will be chosen when generating models or argument signatures, but for any model you declare yourself (even inputs) you can use null.String in the code and gqlgen will try to find the right marshal. |
Hopefully @vektah's response solves your issue. I'm going to close this out. |
@mathewbyrne yes, thanks. Amazing, gqlgen is a stellar project! :) @vektah, you mean |
@frederikhors How did you workaround this? |
I just let gqlgen generate pure models and I'm generating a custom convert plugin for it which generates a convert file. It's work in progress, and I work on this when I have some spare time |
I have this table:
SQLBoiler is generating this entity:
My
schema.graphql
:My
gqlgen.yml
:Expected Behaviour
I thought
gqlgen
must generate just:Actual Behavior
gqlgen
now generates also:and in logging during generation:
If I use this in
gqlgen.yml
:it works (doesn't generate Username and Img resolvers) but something is wrong in
gqlgen_generated.go
at runtime:Where am I doing wrong?
The text was updated successfully, but these errors were encountered: