I am learning about Node.js and GraphQL stacks. Along the way, I will share my experience and highlight the parts I find most interesting!
During my research phase, I looked at the differences between GraphQL and REST. What problems does GraphQL solve that REST doesn't?
With the traditional REST API, we use a CRUD system. For each resource, you would have a GET, POST, PUT, DELETE, which are each methods on an individual resource. Each resource will have all of these methods. You can start to see how many endpoints you can end up with quickly! With more endpoints to maintain, it can be more difficult for the developer and the consumer of your endpoints. GraphQL provides one single endpoint that you use to query for only the data you need.
Over-fetching or Under-fetching is an issue for REST APIs. I have experienced this in my own projects. The issue is that a REST API returns a fixed structure of data; the API consumer cannot change it. If you have an endpoint that is /tasks, you will always get all of the tasks with all of the data defined by the endpoint. What if all you wanted was the title of a task and not the text content? In a traditional REST API, you would fetch all of the data and disregard what you didn't need for your view, which means that you transferred more data than necessary, putting more load on both the backend API as well as your front end application that has to deal with the excess data. We will get into the details in a later post, but the concept with GraphQL we could only ask for the titles and only receive them back from the API, which is much more efficient.
Maybe the biggest thing that jumped out at me as being totally awesome about GraphQL was the automatic API documentation generated with GraphQL Playground! With a traditional REST API, someone has to maintain the documentation and details for each endpoint so that everyone knows how to use it. What params are needed, what is returned, etc? This is neglected on many projects and can result in an API that is hard to understand and use over time. With GraphQL, all of this work is automatically done for you with a complete explanation of the API, allowing consumers of the API to use it to its fullest. I will be going over this in much more detail in an upcoming post as I learn more about this tool.
I am going over the fundamentals of GraphQL and will share what I learn along the way. The initial goal is to learn about the innerworkins of the stack and create a boilerplate template to base my future projects on as a solid starting point. The technologies I will specifically use are:
I hope you follow along my journey and at the end I will share my boilerplate for everyone to use. Lets get started...
Contact me anytime to offer feedback, ask a question, or just say hello :)