In part 1 and part 2, the setting up and testing of GraphQL APIs was covered. In this article, the focus is on the querying of the graph DB, which is Neo4j in this case. For querying, the Neo4j OGM library and Spring Data’s Neo4jRepository will be used.
An OGM (Object Graph Mapper) maps nodes and relationships in the graph to objects and references in a domain model.
Much like JPA, Neo4j-OGM allows you to annotate POJOs in order to map them to nodes, relationships, and properties in the graph.
In the previous blog, we used annotations, such as NodeEntity, Relationship, RelationshipEntity, StartNode, and EndNode.
All these annotations are part of the OGM library. It supports multiple drivers – Bolt, HTTP, and embedded.
In this case, the Bolt driver has been used, as it is a fast native driver that communicates over the Bolt protocol. The default port to connect to Neo4J through the Bolt protocol is 7687. The following dependencies are required to be included in pom.xml:
This will include Neo4j OGM and its dependent libraries, as well as Spring Data capabilities, which are built on top of the Neo4J OGM library.
On the successful startup of the server, the SessionFactory object is created at the backend. Using this object, you can acquire session class objects to fire custom queries at runtime. In one of the use cases, the DB query couldn’t be determined beforehand, like a search member.
In search member, you will not be aware of all the parameters that can come in the request; it could range from 1 to 5, which makes it hard to create queries for every combination.
So the solution is to create a query at runtime dynamically and then execute it using the Session object. The code sample is as follows:
Spring Data Neo4j uses Neo4-OGM to help in the rapid and easy development of spring applications. It is very useful in querying the DB with features similar to any other ORM framework, such as Hibernate or JPA.
Two core components that are used in development are Neo4jRepository and Neo4jTransactionManager. As the name suggests, extending the Neo4jRepository comes with all kinds of CRUD, pagination, sorting, operations, etc. Neo4jTransactionManager is used to manage transactions.
The above image shows an example of the @Repository class, which is extending the Neo4jRepository. The 2 most common ways for querying the DB are shown in the example.
The first 2 methods, that is line number 14 & 16 are an example of querying through the find methods. These are very powerful, as you can find through properties or use a combination of AND and OR between the properties.
Also, you can use various keywords, such as GreaterThan, LessThan, NotNull, Like, and so on. For all supported keywords, refer to the following link:
https://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#neo4j.repositories
In the second method, the members with experience greater than the specified param are fetched. All the find methods execute Cypher queries at the backend.
The last 2 methods, that is line number 19 & 22, are a way of defining your own Cypher queries. Queries using the Cypher graph query language can be provided with the @Query annotation.
SDN supports sorting and pagination while querying. To implement this you need to provide Sort or Pageable class objects to repository methods. The following excerpt is an example:
In the above example, the member records that possess a skill with page 0 and size 10 are fetched. Also, it is sorted in descending order to list the most experienced person in some specific skill on top.
In this article, you have learned how to query Neo4j DB using 2 different libraries – Noe4j OGM and Spring Data for Neo4j (SDN). You also had a sneak peek of Session factory, session, pagination, and sorting. In the next article, we will be looking into using Kong as an API gateway for the application.
https://neo4j.com/docs/ogm-manual/current/
https://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#reference