[Answered ]-How can I create GraphQL API's with Django and Neo4j Database?

1👍

I worked with all 3 of the stacks:

  • Graphene-Django with Postgresql DB
  • Django with Neo4j & Postgresql
  • Neo4j

I can tell you actually you can have either one or the other.

Neo4j has it’s own web interface and it’s own GraphQL API Built in:

  • http://yourhost:7474/browser/
  • http://yourhost:7474/api/

For Django to work with Neo4j you can use neomodel or even better the django version of it: django-neomodel that is using neomodel under the hood.

I am actually having a separate setup and I use Django REST Framework and everything I store in Neo4j i expose it in REST endpoints instead of GraphQL.

So I would say you can either Use django-neomodel to manage/work with your data and you can simply decide if you want to:

  • use Graphene-Django to expose all of the data Neo4j included
  • use Neo4j GraphQL API

One thing though…I would still advice you to have a DEFAULT_DATABASE one of the Officially supported to handle User related data and authentication.

As a personal remark: Neo4j is a great technology but is not the fastest when it comes to transaction speed (where Postgres or other relational databases shines).

It is after all a technology running on Java and not on C++ like most of the other Databases out there…so keep that in mind.

Good luck.

0👍

I’m running a similar setup, but you’re talking about a lot of things here, it might be easier to understand if you break up the concepts.

It’s easiest to understand if you think about just running Neo4j in Python (independent of Django).

Neo4j/Python

  1. Get your Neo4j running and be able to connect to it using python (I use py2neo).

I made a video demo of making neo4j flask app starting here: https://www.youtube.com/watch?v=h8cyPIEfxQY&t=1215s

Code for this is here: https://github.com/elena/graph-fun/blob/master/notebook.ipynb

Django/Graphene

  1. Get your Django project running. Setup graphene in Django (note, wrapping your head around graphene/graphQL is a whole other thing, but do this step separately, ensure you understand how your conventional Django app can connect to graphene).

Some magic sauce here is that we use GraphiQL to serve GraphQL endpoints: https://github.com/graphql/graphiql this generally makes your life much easier.

These docs: https://www.fullstacklabs.co/blog/django-graphene-rest-graphql

Python/Graphene/Neo4j

  1. Make a module/app in your Django project for your Neo4j connection and statements. Important to note here is: graphene doesn’t care where the data comes from. Just use plain python-graphene to send whatever data to graphene schema.py

This is the flask example code (https://github.com/elena/graph-fun/blob/master/app.py), but you can just feed the Neo4j output to your GraphQL python schema.

See, python (not Django) Graphene doesn’t care where data comes from: https://docs.graphene-python.org/en/latest/quickstart/

Finally you can wrap it all up in your django urls to serve using graphiql.

So you need django-graphene to server your schema.py/urls, but this overcomplicates things.

You then need to take a step back and use naive python-graphene to serve Neo4j.

At least that’s what works for me.

Leave a comment