Server Side - carsonmcdonald/Python-Flask-neo4j-Heroku-Example GitHub Wiki

Server Side

The server can be broken down into a few different parts: basics, bookmarks, simple queries and complex queries.

Basics

The Flask framework is used to serve and route requests, there is plenty of information about setting it up on Heroku in their Python getting started guide.

py2neo is used to access the neo4j database. It doesn't handle URLs with a username/password in them so there is some code when doing setup that extracts that from the NEO4J_REST_URL environment variable before creating a connection.

Bookmarks

Bookmarks can be added, deleted and modified to have a certain weight. Requests for each type of action come in as a POST in JSON format. The following examples express all the valid versions of JSON that will be accepted:

{'type':'user', 'userId':1234, 'action':'add'}
{'type':'post', 'postId':9000, 'action':'add'}
{'type':'tag', 'tagName':'school', 'action':'add'}
{'type':'tag', 'tagName':'school', 'action':'weight', 'weight':2}

The action value can be add, delete or weight.

The code for bookmarks is an example of CRUD operations with neo4j. Note that when you add a new node or relationship you will want to update any index associated with the information unless you have auto-indexing turned on.

Simple Queries

Simple queries generate user, post or tag data based on a userId, postId or tagName respectivly. The query uses one of the three indicies created for the given types to find the reference node. The reference node is then used to find both incoming and outgoing node relationships using the py2neo get_related_nodes method. The resulting data is converted to JSON format and has any bookmark relationships filtered out before it is returned.

Complex Queries

Complex queries use the cypher query language to produce more complex results. All complex queries are stored in the complexqueries.cfg file and are read in on startup. A query from the configuration file is executed based on the input query name and query value. The query value is used to replace the {val} value found in each query in the configuration file.

In the simplest case the query is executed with the py2neo execute_and_output_as_json call and is sent directly back to the client in JSON format. There is also an option to post process query data using the configuration file's exec option. With the exec option a block of Python code will recieve the response data to transform before being converted to JSON and going to the client.

The following is an example of a query from the configuration file that finds a user based on the user index then matches that user to their answers and then to a user who asked that question:

[user.answered]
query: start user=node:Users(userId="{val}")
       match user -[:posted]-> (post) -[:question]-> (question) -[:posted_by]-> (asker)
       return user.userId as answererId, post.postId as answerId, asker.userId as questionerId, question.postId as questionId