Decode error - Nested struct

Hello

New to golang and grpahql. I’m getting a error during decode of cursor to struct.for a graphql query. any help would be great.

Thanks

Error:
-------
*cannot decode document into []model.Level1
exit status 1

Here is my graphql schema

type SalesOrder {

  Id: String

  Name: String

  Level1: [Level1]  

}

type Level1 {

  Lname: String

  Level2: [Level2]

}

type Level2 {

  Lname: String

  Level3: [Level3]

}

type Level3 {

  Lname: String

  Level3: [Level3]

}

type Query {

salesorders: [SalesOrder!]!

}

input NewSalesOrder {

  text: String!

  userId: String!

}

type Mutation {

  CreateSalesOrder(input: NewSalesOrder!): SalesOrder!

}
//document from mongo db


{"Id":"12345","Name":"Test","Level1":{"Lname":"First level","Level2":{"Lname":"Secondlevel","Level3":{"Lname":"Third level"}}},"_id":{"$oid":"5e8ef1fb9ce752aac5d18c52"}}

//code in the resolver for decoding to golang struct

ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)

    cursor, err := collection.Find(ctx, bson.D{})

    if err != nil {

        log.Fatal(err)

    }

    for cursor.Next(context.TODO()) {

        var salesOrder model.SalesOrder

        err := cursor.Decode(&salesOrder)

        

        if err != nil {

            log.Fatal(err)

        }

        salesOrderArr = append(salesOrderArr, &salesOrder)

    }

    if err := cursor.Err(); err != nil {

        log.Fatal(err)

    }

    cursor.Close(context.TODO())
//-------------
//golang struct
// Code generated by github.com/99designs/gqlgen, DO NOT EDIT.

package model

type Level1 struct {
	Lname  *string   `json:"Lname"`
	Level2 []*Level2 `json:"Level2"`
}

type Level2 struct {
	Lname  *string   `json:"Lname"`
	Level3 []*Level3 `json:"Level3"`
}

type Level3 struct {
	Lname  *string   `json:"Lname"`
	Level3 []*Level3 `json:"Level3"`
}

type NewSalesOrder struct {
	Text   string `json:"text"`
	UserID string `json:"userId"`
}

type SalesOrder struct {
	ID     *string   `json:"Id"`
	Name   *string   `json:"Name"`
	Level1 []*Level1 `json:"Level1"`
}

@rajagopalss Please try using 3 backticks (i.e. ```) instead of apostrophes and re-pasting your code so that it will be easier for us to read and get you an answer.

@skillian. thanks for pointing. Updated.

Thank you, @rajagopalss. I see the GraphQL schema, but can we see the Go struct definitions, too?

@rajagopalss, I just Googled GraphQL and see that [Level1] means that an array of Level1 objects is expected, but the JSON you provided has a single Level1 object, not an array of them. Same with the levels nested down.

@skillian. Thanks. Added the golang struct too. It is an optional array, so my understanding is it should take one or more.

@rajagopalss It should work with one (or zero) or more, but it still has to be an array. I think this will work:

{
  "Id": "12345",
  "Name": "Test",
  "Level1": [{
    "Lname": "First level",
    "Level2": [{
      "Lname": "Secondlevel",
      "Level3": [{
        "Lname": "Third level"
      }]
    }]
  }],
  "_id": {
    "$oid": "5e8ef1fb9ce752aac5d18c52"
  }
}
1 Like

@skillian
The JSON has this array [], but when this is inserted to mongodb the mongo stores this as array object. Now the object is decoded getting this error.

@skillian, i was converting xml to json and then uploaded to mongo for my testing purpose. now i used a different converter to convert xml to json. Seems like the converter which i was using earlier did not have [] added. Your earlier comment helped to identify this issue. Thanks a lot.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.