Graphql: HELP: how to pass list of objects as an argument?

Created on 22 Mar 2018  路  7Comments  路  Source: graphql-go/graphql

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

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:

var trackType = graphql.NewInputObject(graphql.InputObjectConfig{
    Name: "Track",
    Fields: graphql.InputObjectConfigFieldMap{
        "uuid": &graphql.InputObjectFieldConfig{
            Type: graphql.String,
        },
        "mac_addr": &graphql.InputObjectFieldConfig{
            Type: graphql.String,
        },
    },
})

All 7 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tobyjsullivan picture tobyjsullivan  路  5Comments

conord33 picture conord33  路  6Comments

yookoala picture yookoala  路  4Comments

michelalbers picture michelalbers  路  3Comments

titanous picture titanous  路  3Comments