DEV Community

Discussion on: Explain GraphQL like I'm five.

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.