How do I perform aggregations in GraphQL?

You can do a handful of simple aggregations directly on fields themselves in a GraphQL query.

The following are native GraphQL aggregation functions we can leverage directly on the field itself:

avg
count
max
min
stddev
stddev_pop
stddev_samp
sum
var_pop
var_samp
variance

Below is an example implementing avg in a GraphQL query. In the below query we take the average housing news sentiment over the past week in every MSA available:

query SENTIMENT_PAST_WEEK {
  MSA {
    MSA_NAME
    PARCL_ID
    real_estate_events_aggregate(order_by: {DATE: desc}, limit: 7, distinct_on: DATE) {
      aggregate {
        avg {
          AVG_TONE
        }
      }
    }
  }
}

However, it is important to note that you cannot perform calculations across fields with GraphQL (i.e. multiply the values of two different fields). For more complex calculations you first need to call the API and perform the desired actions in your preferred tech stack.

Here is an example recipe performing both native GraphQL aggregations and post query aggregations in a dataframe using Python: