Dynamic aggregation

Hello, I am building a graphql api on the top of neo4j. My model looks like following:

(Client)-[:HAS_SPONSOR]->(Client)
(Client)-[:MADE]->(Order)

Now, when listing all orders for my downline (clients up to N levels below me)

I end up with the following WITH statement just before the aggregation:

WITH client, order, typeLevel

where typeLevel is a value calculated in sponsorship tree traversal (combination of level and client type).

now the problem:
I need to aggregate by any of the 3 variables in the WITH statement above:

  • client ( a node connected with Order with relationship)
  • any order property
  • typeLevel (dynamically calculated) value

and I can do that obviously by implementing at least 3 API methods (possibly more):

  • sumOfOrderValuesByClient, sumOfOrderValuessByTypeLevel and sumOfOrderValuesByProperty

I only know how to make sumOfOrderValuesByProperty dynamic using order[property] notation, but is there a way to make the other two dynamic.

For example, I am thinking about passing the aggregation operation e.g sum, count, or avg and an order property that will be aggregated (e.g value or type or id) as parameters, so it becomes:
sum(order.value) or count(order.id) or avg(order.value)

and also pass group by key: client, typeLevel or order.type (a property of the order)

So, now I would have 9 API methods for 3 types of aggregation and 3 groupBy keys. Seems horrible :slight_smile:

I do not really know how to do this in a dynamic way in a SINGLE graphql query.

I tried to create on the fly cypher query and pass it to the apoc.cypher.* but apoc operates per row, so I am not getting aggregated values :frowning:

Does anyone have an idea how to do that?

You can use apoc.cypher.run - APOC Documentation the example provided is an aggregation (count)..

Yes, I figured out that I need to wrap the whole query in apoc.cypher.run, not just the last few lines of it, since then it will be executed for each row.
All good now, thanks!