Graphql 查询:如何创建 returns 项目之间不同字段的查询
Graphql query: how to create a query that returns different fields between items
我的查询是在数据库中查找一家公司,returns一些基本信息,以及历年的财务信息。结果如下:
{
"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
}
]
}
}
}
有什么方法可以让查询达到这样的结果吗?
不,没有办法编写这样的查询。
特定列表中返回的所有项目都将具有相同的选择集。唯一的例外是当您请求具有联合或接口类型的字段时——然后您可以使用内联片段为每种可能的类型指定一个选择集。
正如评论中已经建议的那样,唯一可能的解决方法是使用别名。假设您的架构允许您按年份过滤 financials
字段,您将执行如下操作:
{
company {
id
name
companyType
financials2007: financials(year: 2007) {
...FinancialsFields
}
financials2008: financials(year: 2008) {
...FinancialsFields
}
financials2009: financials(year: 2009) {
...FinancialsFields
}
}
}
fragment FinancialsFields on Financials {
year
netRevenue
costOfSales
grossProfit
financialIncome
financialExpenses
resultOfOtherActivities
}
我的查询是在数据库中查找一家公司,returns一些基本信息,以及历年的财务信息。结果如下:
{
"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
}
]
}
}
}
有什么方法可以让查询达到这样的结果吗?
不,没有办法编写这样的查询。
特定列表中返回的所有项目都将具有相同的选择集。唯一的例外是当您请求具有联合或接口类型的字段时——然后您可以使用内联片段为每种可能的类型指定一个选择集。
正如评论中已经建议的那样,唯一可能的解决方法是使用别名。假设您的架构允许您按年份过滤 financials
字段,您将执行如下操作:
{
company {
id
name
companyType
financials2007: financials(year: 2007) {
...FinancialsFields
}
financials2008: financials(year: 2008) {
...FinancialsFields
}
financials2009: financials(year: 2009) {
...FinancialsFields
}
}
}
fragment FinancialsFields on Financials {
year
netRevenue
costOfSales
grossProfit
financialIncome
financialExpenses
resultOfOtherActivities
}