And what you want to avoid is essentially having a single REST endpoint that is being called across all of these platforms and half of the information that is going to the wearable devices essentially underutilized because you don't need all of the information. You're not going to display all of that information. That is where graph QL sort of shines out here where you can have that common base of or a schema as we call it in GraphQL, which we'll talk about in a little bit.
You create the schema essentially with all of the different fields that you want exposed, all of the different data points that you want exposed. And then that can be consumed specifically based on the kind of platform that is actually making the request. So that does enable a lot of flexibility, but that flexibility also brings with itself a little bit of a challenge because with that flexibility, essentially, the onus is on the consuming application to essentially use it in whichever way you want, which means that the structure of the queries, the depth of those queries and what order of that query is gonna be is slightly unpredictable. It's a lot more unpredictable in that matter.
Of course, now it is predictable in terms of the way you would query information and receive information, but you as a developer of the backend resource may not have total control in terms of exactly how the frontend application is actually requesting that information, can be requesting in many different ways. So you need to be careful specifically around security in this case, where you need to have the power to essentially expose the different fields that you want to be exposed depending on the kind of users that are gonna be using that application. And to take the point even further, you're also looking at things like multiple depths or multiple layers of depths when it comes to making those queries. So you can go deep, there is really no well-defined limit in terms of how deep you can go with these queries. You can have nested queries within GraphQL and that can again cause issues around server load management and sometimes these can be required but sometimes these can also be malicious. So there are a couple of challenges because you're no longer just working at the API endpoint level. You're not just working at that HTTP level which is essentially what REST does where you obviously need to have an authentication authorization there but you also need to think about what's going on at the data layer as well where all of your queries and the requests are coming from. So, there are a couple of different challenges there.
Again, it doesn't adhere to the standard rules of your status codes and errors. Typically, you can have errors even with a status code of 200 which is again, something that can be a bit of a challenging thing to work around. But again, because it is flexible you can write your own custom error objects and you could provide a lot more information depending on what your use case could be. So there are positive, there are negative. So I think the mantra here is to ask the question that you, the question that you really need to ask here is, what is my use case? And what are my priorities? Each of these tools, each of these, well, API styles have their own positives. Each of these API styles have their own challenges. So which of these challenges are deal breakers for you? Which of the use case are you really, really focused on? And depending on that, you can make a choice. So either one of these essentially could work for you depending on your use case. It is not a question of GraphQL coming in to replace REST. Essentially each one of them have different problems that they can, they are really, really good at solving. And you should ask which question or which problem that you're gonna be, that matters to you, and therefore pick a solution accordingly. So that is a little bit of a gist around GraphQL. A quick introduction into the world of GraphQL.
I want to also introduce a couple of really, really simple GraphQL concepts, which essentially would help you to understand further as we start building out our solution. So what I've done here is I've just taken this schema. This is essentially the schema that we are gonna be building based off of. So this is gonna be the end result of the Express GraphQL app that we're gonna be building over the next hour. So let's start with defining what a schema is. A schema is essentially in GraphQL, a schema is essentially the blueprint of your API. It defines all of the different types, all of the different kinds of things that you can do, the different actions that you can take, the different data that you can request. So that is essentially the blueprint of your entire GraphQL API. Now, beyond that, if you see here on top, you see something called type mutation, you see something called type query. Now, if you go back to the objectives of APIs, you look at perfoming CRUD applications, the C-R-U-D, which is essentially create, read, update and delete. These are the basic tasks that you want to accomplish usually with your APIs. Maybe one of them, maybe all of them, depending on what you want to accomplish. So in rest, you would have something like get requests for your requesting information, reading information, your R within the CRUD, creating, probably making a POST request, updates could potentially be patches, maybe even puts and delete, could be just delete. So in a similar vein, essentially, we have our two different types here, which is essentially querying and mutations. Queries, typically, are meant to request information, as the name would suggest. Now with queries, essentially, this is the structure that you will see, you define the kind of actions that you can take or the kind of information that you can request, and queries will help you accomplish it. Now this is by far the most utilized aspect of GraphQL. Mostly, this is kind of the request that is made. But beyond that, we have what you call is mutations. As the name suggests, mutations essentially is manipulation or changing or modification of data at your data source. Now that would include adding new information, adding new data to your data source, that would include updating data to your data source, or it could be about removing information from your data source. In today's example, we'll be actually looking at how you could add a user. But perhaps as an exercise, when you go back and try it out yourself, perhaps you could also explore how you can update and delete information as well, as maybe when you try and practice these things yourself. But, so those are kind of the big two. If you are starting off with a schema, this is essentially where you would start off, this is kind of the objective of essentially building out your schema. You're either trying to enable people querying your information, querying your data sources, or you're trying to enable people to actually make changes or modify those data sources. So queries and mutations. There is a third type here, which is subscription, which is essentially a listener. You can listen for changes to your data source, but for the purpose of today's conversation, I'm gonna leave out subscription, but do feel free to read up on that as well. There is gonna be an exciting new update that I'll be talking about maybe in a month's time, so stay tuned for that. But subscriptions, again, unlike what you would have with your basic REST operations, subscription comes pretty much built out, built in to GraphQL, and it's a mechanism for you to listen for specific events, or listen for changes, for example, to maybe user information, maybe changes to if there is a new to-do list that is being added, then maybe you want to be notified based on that. So that's what subscriptions do. But we'll stay focused on queries and mutations for the purpose of today's demonstration.
Now, beyond that, if you look at it, the very, very, the, what do you say? The building blocks, the essential building blocks of your GraphQL schema is, or are, your types, your types and your fields. Now, if you see an example here, you've got your type to-do, you've got your type user, and within these types, these are essentially objects. These are objects that are defining the different kinds of data, data that you can, different kinds of fields that are come together to provide the information that you need. They usually have a correlation to your data source behind the scenes, as you will see in really, really shortly, but they're also defined by, these fields have types associated with them. So these are what you would call scalars. Scalars are essentially built out of GraphQL, again. So you have some basic types that are already provided by GraphQL. You have your int, you have your strings, you do have a Boolean as well. And then further on you can also have lists.
Comments