DEV Community

Discussion on: Explain GraphQL like I'm five.

Collapse
 
angieg0nzalez profile image
Angelica Gonzalez

Oooh, nice!

Anyone know why it has Graph in its name? This whole time I thought it was something to do with data viz!

Collapse
 
defman profile image
Sergey Kislyakov

I don't know about the Graph part too (and Wikipedia doesn't know about that as well). I think the idea is that your queries kinda visualizes the data you're asking for. I mean it's not that hard to understand what I'm asking for in this query:

query {
  books(author:"George von Devto") {
    title
    pages
    comments(rating:">=4") {
      name
      text
    }
  }
}
Collapse
 
mplis profile image
Mike Plis

I remember someone briefly explaining this in some conference talk, but I can't seem to find it. It has to do with how GraphQL allows you to think about and traverse your data and the relationships between them as if you're traversing nodes and edges in a graph.

Collapse
 
johnpaulada profile image
John Paul Ada

I think it's because you can think of each entry as a node, which has edges that connects to other nodes.

Collapse
 
karn profile image
Karn Saheb • Edited

It has Graph in its name because the data is structured as a Graph Data Structure, as opposed to the Linked List (i use this term very loosely) style REST APIs. The key difference is that having "nodes", as is the case for GraphQL, allows you to coalesce data from multiple "nodes" while with conventional REST API is more linear in its data retrieval.

The cool thing about nodes is that you can reference yourself (i.e the same node); it can be recursive.

Let's say you have a user object, which contains a list of friends, denoted by their UUID. We are particularly interested in the names of your friends, so what we can do is this

{
  user(uuid: "8f7ab4") {
    name
    friends {
      uuid
      name
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

What ends up happening is that in a single request we go from one user (given by a user object), get their friends list, and for each friend (also given by a user object) fetch their UUID and name. This is all made possible by how the data is structured. With a REST API, you'd have to query for each of the users by UUID, perhaps by using user/<friend-id>, returned from a user/8f7ab4/friends API call. Even more important is that the REST API call will return tons of information that you dont really care about, effectively costing you more for bandwidth and also resulting in high latency in completing the actual query you're trying to execute.

Thread Thread
 
angieg0nzalez profile image
Angelica Gonzalez

Thank you! That was great.