Neo4j GraphQL Aggregations


Recently we released Neo4j GraphQL 2.4.0 that contained some really cool features, but the coolest, in my opinion, is the autogenerated aggregation queries.

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:

  1. What is the longest movie title?
  2. What is the latest movie?
  3. Who directed more than 3 movies?
  4. What movie has an average rating greater than 10?
  5. What is the average age of all the actors in a movie?
  6. 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.

Top-Level Aggregations

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?

GraphQL Query:

Response:

What is the latest movie?

GraphQL Query:

Response:

Where Aggregations

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?

GraphQL Query:

Response:

What movie has an average rating greater than 10?

GraphQL Query:

Response:

Field Aggregations

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?

GraphQL Query:

Response:

What is the highest rating of a movie?

GraphQL Query:

Response:

Aggregating with the OGM

We also exposed the aggregation queries using the OGM. Here is an example aggregating the title fields on a movie:

That’s It.

Thanks for taking the time to learn more about Neo4j GraphQL aggregations. We are looking forward to hearing your feedback. Resources:

  1. NPM: https://www.npmjs.com/package/@neo4j/graphql
  2. Github: https://github.com/neo4j/graphql
  3. Documentation: https://neo4j.com/docs/graphql-manual/current/
  4. Discord: https://discord.gg/neo4j

Get started the Neo4j GraphQL Library for building API-driven, intelligent applications faster.

Get Started Now

Neo4j GraphQL Aggregations was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.