DEV Community

Megan Sullivan
Megan Sullivan

Posted on • Originally published at meganesulli.com

[Sketchnote] Why Use GraphQL?

Introduction

Since I started working at Gatsby, I've been learning more about GraphQL. One resource that has been really helpful is How To GraphQL.

I made a sketchnote based on my current understanding of their video: GraphQL is the better REST. The rest of this post describes the content of the sketchnote.

A sketchnote about why to use GraphQL

But First... What is GraphQL?

  • An API standard created by Facebook.
  • An alternative to the REST API standard.

What is an API?

API stands for "application programming interface." It's an agreement for how one program talks to another.

One program (the client) sends a request to another application (the server) to ask for information. The server sends a response with the requested data back to the client.

Why Use GraphQL?

Reason #1: Only One Endpoint

A GraphQL server exposes a single endpoint that handles all incoming requests, regardless of the data being requested.

An analogy:

  • REST is like shopping at multiple, smaller retailers. You have to make multiple trips to different stores to buy all the things you need.
  • GraphQL is like doing all your shopping at one big-box retailer (like Costco). You can make a single trip to buy everything you're looking for in one place.

Reason #2: No Underfetching

Underfetching: When the response from the first endpoint doesn't have all the information you need, so you have to make another requst to a different endpoint.

An analogy:

  • REST is like solving a crime by following a trail of clues. Each clue on its own isn't enough to solve the mystery: you have to put them all together to get the final answer you're looking for.
  • GraphQL is like identifying a thief directly by playing back security camera footage. You can get the information you're looking for in one step.

Reason #3: No Overfetching

Overfetching: When the response from an endpoint includes more data than you actually need for your use case.

An analogy:

  • REST is like asking a chatty person for information. You ask them, "When is your birthday?" and they start telling you their entire life story.
  • GraphQL is like asking a terse person for information. You ask them, "When is your birthday?" and they tell you a date. They give you only as much information as you ask for.

Learn More

Top comments (5)

Collapse
 
bias profile image
Tobias Nickel

you show some differences, between rest and graphql, and I also like your analogies.

In practice I had other experience. I find the frondend engineers do the overfetching themselfs. by putting fragments if queries together. querying more data then actually shown. The single endpoint is a nightmare for caching. The varnish cache is great for working with varying URLs, with different post data not so much. So caching solutions have to move into the appserver itself.

As a backend developer, I still continuously have frontend engineers asking for API's in very specific usecases, needed on only one page, with no way to use it well in other situations.

While you can build good successful projects with graphql, I don't see it as much better than Rest API.

Collapse
 
meganesulli profile image
Megan Sullivan

Thanks for sharing your perspective! I'm still learning about GraphQL, so it's interesting to hear from someone with more day-to-day experience. It sounds like there are tradeoffs, like when choosing any technology. And of course room for user error 😂

Collapse
 
greedybrain profile image
Naya Willis

Thanks Megan

Collapse
 
eevajonnapanula profile image
Eevis

I love the analogies! Next time someone asks me why use GraphQL I'll show this post to them 😊

Collapse
 
meganesulli profile image
Megan Sullivan

Thanks! I appreciate that 🥰