Using Dgraph as your database
Tags: database, graphql
Published: 2022-01-16 04:27:09

The link to the original article with images -> https://easyontheweb.com/using-dgraph-as-your-database/

For the past couple of months I’ve been working on a project which has been using Dgraph as the database and I’ve been fairly happy with Dgraph, hence, I thought of writing this article on how anyone can start off by using Dgraph as the database of choice for their next project.

We will discuss the pre-requisites for Dgraph, for what projects is it most suited for and also the pros and cons of using Dgraph in this article but before all that we should first know what Dgraph is and equally importantly, what graph databases are in general.

Graph Databases and Dgraph
A graph database stores nodes and relationships instead of tables, or documents. Data is stored just like you might sketch ideas on a whiteboard. Your data is stored without restricting it to a pre-defined model, allowing a very flexible way of thinking about and using it.

Neo4j docs
I think the way the Neo4j docs explain is the simplest way that graph databases can be explained.

Most developers are familiar with the Table based relational databases like MySQL, PostgreSQL or the document based databases like MongoDB. Our minds have been trained to think of storing data in those table-based or document-based paradigms but what graph databases bring in is a completely different way to think how data is stored in the databases – in graphs.

We can think of graph databases as data being modeled like we would on a white board.


If you would like to get deeper into graph database knowledge I’d highly recommend you go through the Neo4j What are graph databases? series.

Dgraph is a graph database just like MySQL is a table based relational database and hence it is vital that we have a grasp of what graph databases are before working with Dgraph.

Where to use graph databases?
There are some clear advantages that graph databases provide over conventional database solutions like :-

Navigate deep hierarchies
Find hidden connections between distant items, and
Discover inter-relationships between items.
You would most likely want to use Graph databases if your project comes under the following categories –

Fraud Detection
360 Customer Views
Recommendation Engines
Network/Operations Mapping
AI Knowledge Graphs
Social Networks
Supply Chain Mapping
Why Dgraph
Okay, so you’ve decided to use a graph database for your next project seeing the advantages we saw above. Then what’s so special about Dgraph as a database? Why not some other graph database?

There were two main reasons for choosing Dgraph over other graph databases for us: –

1. It gives the ability to query the database in either its own specific domain language called DQL or the much popular GraphQL. The ability to being able to use GraphQL for queries was very important.

2. You do not have to create endpoints or resolvers for Dgraph if you give it a proper schema.

Now the second point is a bit confusing right? What does it mean? It means that the Dgraph creates a structure for resolvers ( handlers for CRUD endpoints etc.) on it’s own if you just provide it with a schema file. Think of it like you just structure the tables in MySQL and it readies the CRUD endpoints for you on it’s own, without having to write any additional code.

I’ve to be honest and say that sometimes, there has been a need to write additional code because logic can be very custom even for CRUD queries but because DGraph does provide an automatic structure in place, this makes it very easy to start off with it.

It’s not just cakes and roses with Dgraph though, there are certain aspects of it which particularly frustrate me, most of these are a result of it being a relatively new database. Not being able to query in the reverse edge sometimes, having to use @cascade in places where I do not want to etc are some of the problems the database still has but I’m sure going forward would be solved.

In my opinion, if you do plan on going over Dgraph then the great tutorial A Tour Of Dgraph is a must for you to learn the know hows.

My personal experience with DGraph
I would say my personal experience of using Dgraph as a database has been fairly positive. The positives certainly outweigh the negatives but I still have not made any statistical analysis of choosing it over a MongoDB or MySQL.

Even though I do believe a couple of years down the line Graph databases, in general, would be much mature and widely used, I still believe they are just a single step behind being used for enterprise-level projects. Nevertheless, I think it is a great choice for personal or small projects and certainly a technology to dip your hands in.

For other web development articles please check the Blog section of the website and if you would like to join me and some other web developers in a Facebook group, here is the link.