简体   繁体   English

子对象未使用Golang解析graphql

[英]Subobject is not parsed graphql with golang

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: SalesHistory类型:

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]]]] 地图[数据:[地图[产品ID:35产品名称:testpr销售历史:地图[总计检查:671.20]]]]]

I create the map"sales_history" myself, otherwise opposite the field sales_history - null 我自己创建地图“ sales_history”,否则在字段sales_history对面-null

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM