0

My query is find a company in the database, returns some basic information, and the financials information over the years. The result looks like:

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

Very simple to write the query:

{
  company {
    id
    name
    companyType
    financials {
      year
      netRevenue
      costOfSales
      grossProfit
      financialIncome
      financialExpenses
      resultOfOtherActivities
    }
  }
}

But my case is not so simple. I need a query just to retrieve some of the fields for each year. The result looks like:

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

Is there any way that a query can achieve such a result?

Minh Giang
  • 631
  • 9
  • 28
  • I don't think you can do exactly what you're looking for. You could add a directive (https://graphql.org/learn/queries/#directives) for the fields that you conditionally want, but then you'd have to call the query separately for the different years, passing in different booleans. – Jason Down Dec 20 '19 at 18:12
  • @JasonDown The cost of repeating the query will be expensive – Minh Giang Dec 20 '19 at 18:25
  • Yes it would. But I don't think there is a way to do what you want any other way. – Jason Down Dec 20 '19 at 18:47

1 Answers1

2

No, there's no way to write a query like that.

All the items returned in a particular list will have the same selection set. The only exception is when you're requesting a field with a union or interface type -- then you can use inline fragments to specify a selection set for each possible type.

As already suggested in the comments, the only possible workaround is to utilize aliases. Assuming your schema allows you to filter the financials field by year, you'd do something like this:

{
  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
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183