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
Add BindFields function tool #197
Conversation
util.go
Outdated
|
||
func getGraphList(tipe reflect.Type) *List { | ||
switch tipe { | ||
case reflect.TypeOf([]int{}): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of all the reflect
usage here, you've already got a reflect.Type
, you can use it. like here case tipe.Kind() == reflect.Slice && tipe.Elem().Kind() == reflect.Int
and other tests case tipe.Kind() == reflect.Slice && tipe.Elem().Kind() == reflect.Int8
, etc...
I'm still not a fan of auto-binding nested structs, which is exactly the opposite of how I (and all the demos I've ever seen of how to use/configure GraphQL recommend). The point is to make sub records searchable in their own right. For example, you should be able to bind recursively. If you have a |
if you mean nested structs by type Person struct{
Alive bool `json:"alive"`
Age int `json:"age"`
Weight float64 `json:"weight"`
Name string `json:"name"`
Address Address `json:"address"`
// and so on ....
} in that struct the sub record is address, so yes you can query |
Waiting |
@zeucxb I updated the PR and hope it can be accepted to merge, if it's not then feel free to just copy util.go and place it in your package |
1 similar comment
Thanks for reviewing @bbuck, agree on that, but it's good enough as it is FMHO
This util function is very awesome!, @ahmadmuzakki29 thanks a lot for taking the time. I believe this is the initial function for more useful always optional time-savers functions, let's improve the recursive part and other improvements on new PR's!, thanks! 🚢 |
Sure thanks! |
I got the error "reflect: call of reflect.Value.NumField on ptr Value" and here is my struct, can anyone help? type Post struct {
ID string `json:"id"`
UID string `json:"uid"`
Cid string `json:"cid"`
ShortId string `json:"shortId"`
Title string `json:"title"`
Summary string `json:"summary"`
Content string `json:"content"`
Gallery []string `json:"gallery"`
Like int32 `json:"like"`
Comment int32 `json:"comment"`
Share int32 `json:"share"`
View int32 `json:"view"`
Tags []string `json:"tags"`
IsShow bool `json:"isShow"`
IsActive bool `json:"isActive"`
IsTop bool `json:"isTop"`
CreatedAt rpcbase.Time `json:"createdAt"`
UpdatedAt rpcbase.Time `json:"updatedAt"`
} |
@axetroy How are you calling the tool? Also it might be better to create your own issue for this, as this issue is related to merging the tool in, not bugs and/or issues arising from using it. |
@axetroy I suspect rpcbase.Time causing it. please create the fields manually for CreatedAt and UpdatedAt, and set it like this
also I'm considering to return the string representation of struct instead of creating an object of it. to remove this kind of problem. |
Same problem with a struct having custom fields |
I've been working with graphql-go lately and realize I'm pretty lazy to create object type for every struct I had. So I made a binder/wrapper function to easily bind all fields in my struct into graphql object.
So if you have this struct
you can create person type with
in file
util_test.go
I made an example with Person struct that looked like this:because this function will scan through empty struct so this function cannot accept recursive type. that would make a stack-overflow error. And one more utility function which is
BindArgs
that can bind the args depending on provided params.