Hello, and welcome to this talk about how to build a GraphQL server using Fastify and Mercurius.
So, first of all, let me introduce myself. I'm Luca Del Pupo, a science software developer at Neo4m and a JavaScript and Typescript enthusiast. My free time, I try to run my YouTube channel and also write technical posts for tech people. I also love running, hiking, and pets, but now it's time to jump in the topic of today.
Why GraphQL? So, basically, when we start to work with API, we start with SOA and then move to REST API, but we serve only desktop application or desktop browser application. But nowadays, we have to serve different kinds of devices, like mobile, or smartwatch, and TV. And in some cases, the REST API is a limitation. For instance, every device has its specific user interface and different needs, because the desktop has a big screen, the mobile has a small one, and the smartwatch has a pretty, pretty small one. So, the info that you can show in this kind of device are different. And basically, what happens is that the mobile needs a subset of the data that is needed in the desktop application, and the same is for the smartwatch. And what happens is that basically, the people start to create specific endpoint just to remove the not needed fields, to improve also the performance of the mobile application and the time that the server had to spend to serialize and deserialize the data.
Another problem is that if you are in the browser, your browser has a limitation in time of request in the same moment in parallel, and the number is 10. So, in some scenario, this also is another limitation that you cannot avoid if you are using a REST API. And this is Bill. Bill is a junior backend developer that every day fight with the frontend and the mobile to build a specific endpoint for a single view that they need. Joking apart, REST API is not a mess, but in some scenario, they are not the best solution for your application. And this is why Facebook, now Meta, has created GraphQL.
GraphQL is a query language that permits you to create a sort of server that serves different kind of service device. And if you are a good developer and you build it in the right way, you can use only one server and create only one API and serve different kind of server of device like desktop, mobile, smartwatch, and so on and so on. GraphQL is pretty simple. So basically, it contains a schema. A schema is like your open API, okay? And inside, you can define the queries that is the way to retrieve data from the server. You can define mutation that is the way to update data, delete data, or add data in your server. And there is also subscription. There are also subscriptions that permit you to subscribe to an event. And when something happens in the server, the server will emit this event, and the client maybe can react in some way. I don't know. You can use a mutation to add an item in the car. And when this happens, you can modify the client that is a subscribe to this event.
Comments