I have a type trackType:
var trackType = graphql.NewObject(graphql.ObjectConfig{
Name: "Track",
Fields: graphql.Fields{
"uuid": &graphql.Field{
Type: graphql.String,
},
"mac_addr": &graphql.Field{
Type: graphql.String,
},
},
})
schema field to create several tracks in one query:
func GraphQLCreateTracks() *graphql.Field{
return &graphql.Field{
Type: graphql.NewList(trackType),
Description: "Create multiple tracks",
Args: graphql.FieldConfigArgument{
"tracks": &graphql.ArgumentConfig{
Type: graphql.NewList(trackType),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
tracks := params.Args["tracks"]
fmt.Println("this is tracks: ", tracks)
return nil, nil
},
}
}
and I am trying to pass a query like this:
query=mutation{createTracks(tracks:[{uuid:"1",mac_addr:"mac_addr1"},{uuid:"2",mac_addr:"mac_addr2"}]){uuid,mac_addr}}
when I print the tracks argument, it shows:
map[tracks:[<nil> <nil>]]
So, I guess there is a problem with deserialization. I might be doing something totally wrong. I am new both to Go and Graphql, so apologies if this is a very basic question. I am not even sure that's the best way to save several objects in one query.
I already spent several hours trying to figure this out. Please, someone help
@khier996 I've had success deserializing complex data structures by using Input Objects inside of graphql.NewList().
Something along the lines of:
var trackType = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "Track",
Fields: graphql.InputObjectConfigFieldMap{
"uuid": &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
"mac_addr": &graphql.InputObjectFieldConfig{
Type: graphql.String,
},
},
})
@NathanBurkett thank you! yes, i figured out I had to use input objects a day later :)
Forgot to close the issue.
@khier996 I have same issue. Do you know how to make the tracks variable in looping? Can you give me an example?
Thanks
@GarryLaly i am not exactly sure what you mean. In my case changing type from NewObject to NewInputObject solved the problem. can you explain your problem in more detail?
@khier996 in this block code
tracks := params.Args["tracks"]
fmt.Println("this is tracks: ", tracks)
I want to print with looping each value object by trackType. In example we have input data
tracks: [
{
uuid: "test1",
mac_addr: "test1",
},
{
uuid: "test2",
mac_addr: "test2",
}
]
and we want access like this tracks[0].mac_addr
Can you give example source code looping the array object trackType in golang?
tracks[0] is not an Track object. so you need to convert it. i used "github.com/mitchellh/mapstructure" library for that:
for index, _ := range tracks {
var track models.Track
convertTrack(tracks[index], &track)
fmt.Println(track.MacAddr)
}
func convertTrack(from interface{}, to *models.Track) {
if trackMap, ok := from.(map[string]interface {}); ok {
config := &mapstructure.DecoderConfig{ TagName: "json", Result: &to, }
decoder, _ := mapstructure.NewDecoder(config)
decoder.Decode(trackMap)
}
}
@khier996 Solved. Thank you very much.
Most helpful comment
@khier996 I've had success deserializing complex data structures by using Input Objects inside of
graphql.NewList().Something along the lines of: