Minh Giang
Minh Giang

Reputation: 727

Graphql query: how to create a query that returns different fields between items

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?

Upvotes: 0

Views: 670

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84867

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
}

Upvotes: 2

Related Questions