Subobject is not parsed graphql with golang

76 Views Asked by At

The object itself inside the object is parsed with an empty field.

input:

{
  Products(search: {limit: 1, filters: {product_id: {gte: 5}}}) {
    data {
      product_id
      product_name
      sales_history{
        total_check
      }
    }
  }
}

output:

{
  "data": {
    "Products": {
      "data": [
        {
          "product_id": 35,
          "product_name": "testpr",
          "sales_history": {}
        }
      ]
    }
  }
}

Product type:

gql.ProductType = graphql.NewObject(graphql.ObjectConfig{
        Name: "Product",
        Fields: graphql.Fields{
            "product_id": &graphql.Field{
                Type: graphql.Int,
            },
            "product_name": &graphql.Field{
                Type: graphql.String,
            },
            "sales_history": &graphql.Field{
                Type: gql.SalesHistoryType,
            },
        },
    })

SalesHistory type:

gql.SalesHistoryType = graphql.NewObject(graphql.ObjectConfig{
    Name: "sales_history",
    Fields: graphql.Fields{
        "total_check": &graphql.Field{
            Type: graphql.Float,
        },
    },
})

In Resolve returning map in interface:

map[data:[map[product_id:35 product_name:testpr sales_history:map[total_check:671.20]]]]

I create the map"sales_history" myself, otherwise opposite the field sales_history - null

1

There are 1 best solutions below

0
On

The problem was in the packaging of the final map.

It was wrong:

tmp := make(map[string]interface{}, 0)
tmp["total_check"] = v["total_check"]
v["sales_history"] = tmp

*Some fields are hidden

That`s right:

v["sales_history"] = make(map[string]interface{}, 0)
v["sales_history"].(map[string]interface{})["total_check"] = v["total_check"]