What Do You Mean by Aggregations?
We use the term aggregations to encapsulate the retrieval of statistical information about the nodes, relationships, and their properties in your graph. Statistical information may be answering questions such as:
- What is the longest movie title?
- What is the latest movie?
- Who directed more than 3 movies?
- What movie has an average rating greater than 10?
- What is the average age of all the actors in a movie?
- What is the highest rating of a movie?
Prior to our 2.4.0 release, users would have to employ our Cypher Directive, and where they would have to manually write Cypher to answer such questions. Now, with our latest release, the autogenerated aggregation queries remove the need to write any custom logic at all!
How Do I Get Started?
Let’s first establish what our Graph will look like. For this we will use the ‘Movie Graph’ located on the right side of Neo4j Browser:
This can be expressed with Neo4j GraphQL Type Definitions like so:
You can visit our Documentation Getting Started Guide to learn how to use the above Type Definitions to autogenerate all the queries in this blog.
Firstly let’s start at the top. We consider ‘Top-Level’ to be the queries at the root of your GraphQL API. Those queries would be, for example; movies, people, etc. The point is it’s always considered a node from the root. In our new release, we added aggregate queries to this level. Below are some examples using those new queries plus you can learn more about top-level Aggregations here.
What is the longest movie title?
What is the latest movie?
These aggregations are those that are inside the already existing where argument. Below are some examples using those new queries plus you can learn more about Where Aggregations here.
Who directed more than 3 movies?
What movie has an average rating greater than 10?
Finally, these aggregations are across relationships. You use these aggregations nested inside a node, just like how you would select the actors of a movie, you can now also aggregate the actors of a movie. Below are some examples using Field Aggregations plus you can learn more about them here.
What is the average age of all the actors in a movie?
What is the highest rating of a movie?
Aggregating with the OGM
We also exposed the aggregation queries using the OGM. Here is an example aggregating the title fields on a movie:
Thanks for taking the time to learn more about Neo4j GraphQL aggregations. We are looking forward to hearing your feedback. Resources:
- NPM: https://www.npmjs.com/package/@neo4j/graphql
- Github: https://github.com/neo4j/graphql
- Documentation: https://neo4j.com/docs/graphql-manual/current/
- Discord: https://discord.gg/neo4j
Get Started Now