Knowledge Graph part-3 :Building REST API over Knowledge Graph

Nikita sharma
3 min readAug 24, 2019
Photo by Clint Adair on Unsplash

In our previous post, we discussed about getting started with knowledge graph where we saw how to install neo4j in Docker and Modelling tabular data as graph where we saw how we can push the data to our graphical database.

Now that we have all the data in our graph database, we can analyse it directly in Neo4j. However a lot of time we need to access small part of the data via apps and we would need to build an API layer on the graph DB and expose only the relevant information.

In this post, we will discuss about exposing our knowledge graph via Flask API. All the data will be exposed in JSON format.

Identifying the queries

First, we will decide what we want to expose in our API. Let’s come up with few hypothetical requirements :

  • Get a particular user’s details
  • Get user’s details and all connections for a given user
  • Size of our knowledge graph : count of all the nodes in our graph

Constructing Cypher queries

Now that we have decided what data we want to expose, we will write queries to fetch the data from Neo4j.

Get a particular user’s details

Let’s find a user’s detail with user_id as 33 :

MATCH (n:Person) WHERE n.uid= 33 RETURN n as person

OR

MATCH (n:Person {uid: 33}) RETURN n as person

Get user’s details and all connections for a given user

Let’s find all the connection of user’s detail with user_id as 33 :

MATCH (n:Person {uid: 33})-[r]->(c) RETURN n as person, type(r) as relationship, c as value

Size of our knowledge graph : count of all the nodes in our graph

Let’s count all the distinct number of nodes in our graph :

--

--