2

对象内部的对象本身被解析为一个空字段。

输入:

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

输出:

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

产品类别:

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,
            },
        },
    })

销售历史类型:

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

在 Resolve 在界面中返回地图:

地图[数据:[地图[product_id:35 product_name:testpr sales_history:map[total_check:671.20]]]]

我自己创建地图“sales_history”,否则与字段 sales_history - null

4

1 回答 1

0

问题在于最终地图的包装。

那是错的:

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

*有些字段是隐藏的

没错:

v["sales_history"] = make(map[string]interface{}, 0)
v["sales_history"].(map[string]interface{})["total_check"] = v["total_check"]
于 2019-03-18T13:46:09.510 回答