【发布时间】:2020-04-13 04:32:59
【问题描述】:
我的查询是在数据库中找到一家公司,返回一些基本信息,以及多年来的财务信息。结果如下:
{
"data": {
"company": {
"id": 1,
"name": "test company",
"companyType": "NH",
"financials": [
{
"year": 2018,
"netRevenue": 0,
"costOfSales": 0,
"grossProfit": 0,
"financialIncome": 0,
"financialExpenses": 0,
"resultOfOtherActivities": 0
},
{
"year": 2017,
"netRevenue": 0,
"costOfSales": 0,
"grossProfit": 0,
"financialIncome": 0,
"financialExpenses": 0,
"resultOfOtherActivities": 0
},
{
"year": 2016,
"netRevenue": 0,
"costOfSales": 0,
"grossProfit": 0,
"financialIncome": 0,
"financialExpenses": 0,
"resultOfOtherActivities": 0
}
]
}
}
}
编写查询非常简单:
{
company {
id
name
companyType
financials {
year
netRevenue
costOfSales
grossProfit
financialIncome
financialExpenses
resultOfOtherActivities
}
}
}
但我的情况并非如此简单。我需要一个查询来检索每年的一些字段。结果如下:
{
"data": {
"company": {
"id": 1,
"name": "test company",
"companyType": "NH",
"financials": [
{
"year": 2018,
"netRevenue": 0
},
{
"year": 2017,
"grossProfit": 0,
"financialIncome": 0,
"financialExpenses": 0
},
{
"year": 2016,
"resultOfOtherActivities": 0
}
]
}
}
}
有没有什么方法可以让查询达到这样的结果?
【问题讨论】:
-
我不认为你可以做你正在寻找的东西。您可以为有条件需要的字段添加指令 (graphql.org/learn/queries/#directives),但随后您必须针对不同年份分别调用查询,并传入不同的布尔值。
-
@JasonDown 重复查询的成本会很高
-
是的。但我认为没有其他方法可以做你想做的事。
标签: c# asp.net-core graphql hotchocolate