Skip to content
a simple way to create a graph service on node.js
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Graphsvc makes it super easy to create an API for consuming and modifying your graph data. It runs on node.js and extends the express framework. It uses neo4j as a datasource.


$ npm install graphsvc

You will also need to run an instance of neo4j.


Imagine that we want to build a simple service that lets us create, update, or delete instances of two entity types: users and places. Additionally, we want the ability to create, update, or delete connections between these instances.

Graphsvc allows us to configure such a service with just a few lines of code. Here's what it would look like:

graphsvc = require('graphsvc');

var app = graphsvc('');
   .endpoint('user.destinations', 'place.visitors', 'visited');


If we run this code on node.js, then our service will be listening on port 8080.

Simple Configuration

Let's go through the configuration above step by step:

graphsvc = require('graphsvc');

This imports the graphsvc module that we installed via npm.

var app = graphsvc("");

This creates an instance of graphsvc that points to our neo4j REST API endpoint. If we're running locally, this would be 'http://localhost:7474' by default.


This configures the user endpoint. A new endpoint: http://myservice/users will be created (note the automatic pluralization).

We can POST request to this endpoint to create new users. We can read or update existing users by sending GET requests to: http://myservice/users/[user_id]


This configures the place endpoint, which works exactly the same as the user endpoint described above.

app.endpoint('user.destinations', 'place.visitors', 'visited');

This configures a connection between users and places. The first parameter, user.destinations, creates the endpoint http://myservice/users/[user_id]/destinations. We can read the list of places that a user has visited by sending a GET request to this endpoint. We can connect the user to a new or existing place by sending a POST request to this endpoint.

The second parameter, place.visitors, creates the endpoint http://myservice/places/[place_id]/visitors. This works exactly the same as the user.destinations endpoint, only in reverse. Sending a GET request to it will let us read a list of users who have visited a given place, and sending a POST request to it lets us connect new or existing users to this place.

The third parameter, visited, is the name of the neo4j relationship type for this connection. It describes the relationship, as in [user] visited [place]. For instance, Mike visited Boston.


This configures our service to listen on port 8080. Note that because graphsvc is an extension of express, all of the functions provided by express (such as listen) will be available from the app object.

Controlling Requests and Responses

If we need to add additional logic to our service, we can use the before and after functions to intercept requests or responses for specific entities or connections.

Here are a few examples:

app.before('create', 'place', function(data, request, next) {
    if( == 'Newfoundland') {
        throw new Error('That place is too cold');
    } else {

This rule would prevent any place named Newfoundland from being created, and would respond with an error.

app.after('read', 'user', function(data, request, next) {
    if( == 'Mike') {

graphsvc works by letting you configure service endpoints. Each endpoint represents either an entity or a connection between entities.

Entity Endpoints

Here's a simple service with 3 entity endpoints

graphsvc = require('graphsvc');
var app = graphsvc("");



This will automatically configure these endpoints (assuming our service is running on


We can use these endpoints to create, update, delete, or get entities. We can create a new user

$ curl -X POST '' 
       -d '{"name":"Mike", "state":"Massachusetts"}' 
       -H 'Content-Type: application/json'

The server will respond with the key of the newly created user. Assuming the key was 12345, we can get the user

$ curl -X GET ''

We can also modify the user

$ curl -X POST '' 
       -d '{"state":"Connecticut"}' 
       -H 'Content-Type: application/json'

or delete the user

$ curl -X DELETE ''

Connection Endpoints

Here's the same service as above, with some connection endpoints added

graphsvc = require('graphsvc');
var app = graphsvc("");

   .endpoint("user.locations", "place.dwellers", "lives_in")
   .endpoint("user.items", "thing.owners", "owns");


This will configure 2 endpoints for the lives_in connection


and 2 endpoints for the owns connection


Assuming that user 12345 and place 6789 exist, we can now connect them

$ curl -X POST '' 
       -d '{"id":"6789"}' 
       -H 'Content-Type: application/json'

We can now get all places that user 12345 lives in. The server would respond to this with place 6789, and any other connected places

$ curl -X GET ''
Something went wrong with that request. Please try again.