GraphQL — the quick and basic beginner’s edition
While attempting to access an API recently, I stumbled across GraphQL. As someone who has only ever seen, accessed and built REST APIs, I was a bit bewildered. (Yes, I know I’m behind - I’m not building applications as regularly anymore, but it could also be that I’m just living under a rock.) In this article, I’ve centralized some resources and covered the questions that initially came to mind.
What is GraphQL?
At the core of it, GraphQL is a query language. It is not an architecture. It was developed by Meta (Facebook) back in 2012 to initially address the challenge of fetching data more efficiently on mobile applications.
What format is the data returned in?
JSON — same as REST.
What frameworks does it work with?
Framework agnostic — it can be integrated with any existing backend technology. Developers can retrofit GraphQL into their current stack without the need for major architectural changes.
How does it work? [back to TOC]
GraphQL operates via a single endpoint where clients can send queries to the server using a special syntax. These queries can retrieve data using two main types of operations: queries and mutations.
You can think of “queries” as your GET request in REST, used for retrieving data from the server and “mutations” as your CREATE, PUT AND DELETE requests, allowing clients to modify data.
What is the syntax? [back to TOC]
The syntax for GraphQL queries resembles the structure of the data being requested (kind of like retrieving a value for dictionary/hash map data structure).

[Side note — a bit surprised that there isn’t the ability to create tables on the Medium. Here are screenshots for reference instead.]
Note that “query” can be omitted and “HeroNameAndFriends” can be renamed to fit your query.

Note that the “!” here means the variable is not nullable and must always have a value. Can be used for both variable declarations in queries and defining fields values in response data as well.
What are the benefits of GraphQL over REST? [back to TOC]
This primary advantage is no more overfetching (getting more properties back that are not needed) or underfetching (having to make multiple calls in REST to get to the data point needed) — you get exactly what you need. This is much more efficient compared to REST. I love the ability to send multiple related queries in a single request. For example, instead of separate calls to Posts and Comments, I can now retrieve all the necessary data in a single query.
What tools are there for GraphQL? [back to TOC]
Additional resources [back to TOC]
Learn more:
-> https://graphql.org/
Practice with Star Wars data:
-> https://studio.apollographql.com/public/star-wars-swapi/variant/current/home
-> Try demo graph option with GraphOS
I didn’t cover any of the disadvantages since I’m still in the early stages and haven’t actually built any GraphQL APIs yet. Will need to explore this more and authentication as a next step. Would appreciate suggestions for any good resources out there!