Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does GraphQL negate the need for Graph Databases

Most of the reasons for using a graph database seem to be that relational databases are slow when making graph like queries.

However, if I am using GraphQL with a data loader, all my queries are flattened and combined using the data loader, so you end up making simpler SELECT * FROM X type queries instead of doing any heavy joins. I might even be using a No-SQL database which is usually pretty fast at these kinds of flat queries.

If this is the case, is there a use case for Graph databases anymore when combined with GraphQL? Neo4j seems to be promoting GraphQL. I'd like to understand the advantages if any.

like image 633
Muhammad Rehan Saeed Avatar asked May 02 '18 12:05

Muhammad Rehan Saeed


People also ask

Does GraphQL need graph database?

For many developers building standard web applications that rely on transactions and a constrained data model, a relational database may be a better choice. Regardless, GraphQL is data source agnostic; that means that you could use anything you like as a data source, including a graph database.

Does GraphQL use graph data structure?

GraphQL (Graph Query Language) is an open source data Query and Manipulation Language built on using the Graph Data Structure. GraphQL can be used to Query from an API and/or Database.

What does GraphQL have to do with graphs?

With GraphQL, you model your business domain as a graph by defining a schema; within your schema, you define different types of nodes and how they connect/relate to one another. On the client, this creates a pattern similar to Object-Oriented Programming: types that reference other types.

When should you not use a graph database?

2. When you Need Key/Value Storage. Very often, databases are used to lookup information stored in key/value pairs. When you have a known key and need to retrieve the data associated with it, a graph database is not particularly useful.


2 Answers

GraphQL doesn't negate the need for graph databases at all, the connection is very powerful and makes GraphQL more performant and powerful.

You mentioned:

However, if I am using GraphQL with a data loader, all my queries are flattened and combined using the data loader, so you end up making simpler SELECT * FROM X type queries instead of doing any heavy joins.

This is a curious point, because if you do a lot of SELECT * FROM X and the data is connected by a graph loader, you're still doing the joins, you're just doing them in software outside of the database, at another layer, by another means. If even that software layer isn't joining anything, then what you gain by not doing joins in the database you're losing by executing many queries against the database, plus the overhead of the additional layer. Look into the performance profile of sequencing a series of those individual "easy selects". By not doing those joins, you may have lost 30 years value of computer science research...rather than letting the RDMBS optimize the query execution path, the software layer above it is forcing a particular path by choosing which selects to execute in which order, at which time.

It stands to reason that if you don't have to go through any layer of formalism transformation (relational -> graph) you're going to be in a better position. Because that formalism translation is a cost you must pay every time, every query, no exceptions. This is sort of equivalent to the obvious observation that XML databases are going to be better at executing XPath expressions than relational databases that have some XPath abstraction on top. The computer science of this is straightforward; purpose-built data structures for the task typically outperform generic data structures adapted to a new task.

I recommend Jim Webber's article on the motivations for a native graph database if you want to go deeper on why the storage format and query processing approach matters.

What if it's not a native graph database? If you have a graph abstraction on top of an RDBMS, and then you use GraphQL to do graph queries against that, then you've shifted where and how the graph traversal happens, but you still can't get around the fact that the underlying data structure (tables) isn't optimized for that, and you're incurring extra overhead in translation.

So for all of these reasons, a native graph database + GraphQL is going to be the most performant option, and as a result I'd conclude that GraphQL doesn't make graph databases unnecessary, it's the opposite, it shows where they shine.

They're like chocolate and peanut butter. Both great, but really fantastic together. :)

like image 153
FrobberOfBits Avatar answered Sep 23 '22 20:09

FrobberOfBits


Yes GraphQL allows you to make some kind of graph queries, you can start from one entity, and then explore its neighborhood, and so on.

But, if you need performances in graph queries, you need to have a native graph database.

With GraphQL you give a lot of power to the end-user. He can make a deep GraphQL query.

If you have an SQL database, you will have two choices:

  • to compute a big SQL query with a lot of joins (really bad idea)
  • make a lot of SQL queries to retrieve the neighborhood of the neighborhood, ...

If you have a native graph database, it will be just one query with good performance! It's a graph traversal, and native graph database are made for this.

Moreover, if you use GraphQL, you consider your data model as a graph. So to store it as graph seems obvious and gives you less headache :)

I recommend you to read this post: The Motivation for Native Graph Databases


Answer for Graph Loader

With Graph loader you will do a lot of small queries (it's the second choice on my above answer) but wait no, ... there is a cache record.

Graph loaders just do batch and cache.

For comparaison:

  • you need to add another library and implement the logic (more code)
  • you need to manage the cache. There is a lot of documentation about this topic. (more memory and complexity)
  • due to SELECT * in loaders, you will always get more data than needed Example: I only want the id and name of a user not his email, birthday, ... (less performant)
  • ...
like image 29
logisima Avatar answered Sep 22 '22 20:09

logisima