YTread Logo
YTread Logo

GraphQL vs REST: What's The Difference And When To Use Which?

Mar 04, 2024

rest

versus

graphql

you're probably thinking oh this is one of those videos again where we're supposed to figure out

which

one is better but then it gets boring, it depends on

what

you want to use it for so

which

one is better. it depends, you know why, because we are nuanced people, the world of software development is not black and white, it is 254 shades of gray, 50 amateurs, so we will delve into the details, talk about some pros and cons of

rest

and

graphql

. I'll give you a couple of examples and then give you some of my ideas on

when

to use one over the other, but before we dive in, let's first talk about the sponsor of this video.
graphql vs rest what s the difference and when to use which
Skillshare Skillshare is an online learning community with thousands of inspiring classes. for creators explore new skills deepen existing passions and get lost in creativity share skills has many classes on web development programming in software engineering and software design Python right now I'm following Frank Kane's course on data science and learning automatic with Python, it is really complete. contains lessons on statistics, data types, clustering algorithms, decision trees, covers libraries like pandas, basically everything you need to know to get started. Skillshare is designed for learning, there are no ads and they are always releasing new premium classes so you can stay focused and follow wherever your creativity takes you.
graphql vs rest what s the difference and when to use which

More Interesting Facts About,

graphql vs rest what s the difference and when to use which...

The first 1,000 of my subscribers to click the link in the description will get a free one-month trial of Skillshare so you can start exploring your creativity today. The rest of the interface is already quite old. It was created in 2000 by Roy. in your thesis psd, architectural styles and the design of network-based software architectures, rest means representational state transfer, the basic idea is that

when

you want to perform some action on a resource, you send a request, an http request and the body of that request contains the new desired state and the server will respond with the actual state after handling the request, rest interfaces are therefore resource oriented, this is different from remote procedure calls of rpc style interfaces , which are action oriented, you call a remote function that performs a certain task and you get the result, for example you could have a block title set rpc that you pass my title to and it returns okay instead of the new block.
graphql vs rest what s the difference and when to use which
Now let's take a look at how we can create a simple rest interface in Python using flask. This is a basic example of a restful API for blogs and blog authors. I'm using flask to create the API and then I define a number of routes, one for each of the API endpoints, so this route here is the root route and it just returns hello. world let me start the server and then when I switch to my browser I make this request so here we have the localhost and the port in this case is 5000 so when I do this I get the request which is basically

what

is. then i will get hello world as return value.
graphql vs rest what s the difference and when to use which
There are also a couple more things here, for example I have a blogs route that calls the allblogs function and then converts it to a json data structure to have data. pi file also in this project that provides me with a series of basic operations to perform with data on blogs and authors. I have a bunch of classes here like there's a blog class, so a blog has an id, a title content, and an author id. I have a payload, this is basically what we're going to use when we update a blog, so we want to be able to update the title and content of the blog post and we have an author that simply has an id and a name.
Obviously there would be a lot more stuff here, but this is just to give a simple example and then I've also coded it here in the data.pi file. I have several blogs and several authors. Normally this would be a database, but since I don't want to talk too much about database topics in this video, I just did something very simple like this and now we have a couple of functions that we can use to read and write this data, so finally I have a class that no error found which is basically an exception that will be cleared when we can't find a blog with a particular id and then have additional functions like this returns all blogs this gets a blog with a particular blog id this updates a blog that we have all the authors and get a specific offer, so these are just a couple of common operations that you might want to do in a blog system and as you can see in app.pi, I just add a couple of routes here, i have a route to get the blocks the route to get a blog with a particular id, this is how it is set up in flask, so it provides the id here as part of the route and then it is passed as a parameter to the function and the same for the authors and the foreign author of a particular ID, so for example, let me go back to the browser and then when I go to put something like this here, this is going to give me all the blocks, so I'm just going to get this series of objects from blog in json format. and I can also get a block with a particular id, so let me get a block with id 2, for example, and then we don't get anything because I think I have to put an s here, yeah, there we go, so now we have to block with id 2.
It's very simple now obviously if you look at the app.pi file again I didn't add anything else and then I get a request if you want to update a blog then you need to add an API route for that so currently No I'm going to do that so let's add a simple route here to also update the block and that's actually relatively simple so I can add a route here app dot route and then let's say we're going to use the same route so blog slash id and let's use, you could use a sell request or you could use a post request like this and now we have a post request and let's add a function here, let's call that route update blog and you'll see that I'm actually using The Copilot from github here is already suggesting the things I should write, so basically we have an id which is a parameter and we get the payload.
I'm not sure this.json is actually going to work. I usually do it like this with the get json function call gives me the payload and then we call the update block. I pass the id but I have to convert it to an integer because ids are stored as an integer in the system and we pass the payload and then I'm also going to return the result of this call to the blog update function and what it does the object block function call is it returns the updated block as you can see here, so let's go back to app.pi, so let's restart the server, which happens automatically and now let me move. to another terminal and now I'm going to make a post request and update a particular block, let's look back so currently this is block 2 so we have this title and we have this content so now let's change the title of this particular block.
That's what I'm going to do this post request for, so I'm going to send a body with json data that contains a title which is what we're going to update and we're going to post it to the URL of this server, slash, slash two, so it gets updated. the second blog for us is like this and now you see that we also get json as a result with the title updated and if I go to my browser again and repeat this get request, you will also see that the title is now updated, this is how we set up an API simple rest and as you can see we have routes for blogs, we also have routes for authors, if you want to introduce new concepts in the system, like editors, books or anything else, you just have to add a separate route for this. and then you can process the create, read, update and delete request through that route.
One thing that's not so good about this design is that we directly return the data we get from operations like getblog and updateblog, and that could lead to security issues. I'll talk more about that in a minute, but this is basically how you set up a basic rest API. By the way, there are a couple of problems with rest interfaces. If you're enjoying the video so far, give it a thumbs up it helps. spread the word about this channel. The first problem is that you need to make sure your rest interface meets the standard. A good starting point to help you with this is the arrogance in the open AI standard.
They also provide tools to help you design. their apis while making sure they meet the standard. I'll put a link to swagger and open ai in the description of this video. Another problem with the rest interface that you will see in this example is that here to get the author of a blog requires a separate request, you could implement a population mechanism of some kind, but there is not really a standard way to do it and that leads to having to coordinate multiple requests on the front-end to get the data you need and wait for it to complete, which ultimately slows down and reduces the user experience, a third problem is that rest does not enforce a distinction between the structure of the data in the database and the structure of the data it receives and sends via the API, inviting developers to simply directly send the data retrieved from the database raises potential security issues, e.g. accidentally send information that you don't want to be public, like a user's email address or password, and if you're not careful, it's very easy to make mistakes here and eventually with the rest, there's no way to control the amount of data you get from the request.
Now blogs aren't that big, but if you have a document with a lot of fields, you might want to control this somehow. You could create something for that, but again, there is no standard way. to do it at rest, Graphql tries to find a solution to all these problems instead of rest, which has multiple endpoints and uses various http verbs that you know, delete post etc. to interact with the server. Graphql uses a single endpoint and query language to interact. with server, ql in Graphql means query language and what about graph? Basically, that illustrates the

difference

between rest and Graphql, which is that Graphql views data as a graph structure where objects are connected by relationships and therefore form a graph.
It is then combined with the query language, allowing you to specify exactly what data you want to get from the server and, more specifically, what part of the graph structure you want to retrieve instead of the rest, then the blog post and its associated offers are handled . Also, in a single request you define the interface to the Graphql backend via the schema and there you specify exactly what the data looks like and this solves a lot of security issues if you try to do something that is not specified in the Graphql schema you are going to to do.
I'm getting an error so now let's change our rest interface to a Graphql interface and see how it works, so I've already done some of the scaffolding for the Graphql example. This is basically what the main application file looks like, so I'm still using Flask. to configure the actual server for this, but then for my Graphql server I am using a library called ariadne and there are also other libraries for Graphql, you also have graphene and strawberry, each has its advantages and disadvantages. I could make a comparison. all these Graphview libraries in the future, but basically Ariadne is a wrapper library that allows you to easily define a Graphql server with Graphql types and as you can see, there is an important

difference

between a Rest API and a Graphql API, which is that Graphql only has two. routes, there is a get request which is actually mainly used to get a playground, so you have a built in web server automatically where you can run queries and see what the result is, so normally in production, I would disable this and then you have the main graphql interface which is actually a post request to that same url so the server pulls graphql or you can choose whatever you want but this is the standard way to do it and there you get the json of the request because anything you do with graph, any way you interact with it will be through json and then it will process that request, it creates a graph schema that defines the structure of the request that you can make on the graph surface and then it executes that request through the scheme. and through the resolvers in that schema and then it will return a json structure that contains the result of that particular Graphql request and this part here I basically copy and pasted this from the ariatni project website, we're not going to change this, that's right.
We're going to leave this as is, all the boilerplate we're going to need for our Graphql server and then handling the request and modeling the entire system will be different, that will happen somewhere else, so I did some additional things as well. So here we have a filesimple that it's just there to create a Graphql schema for the moment which doesn't do much either, it just depends on the type definition that I'm taking from this file and a mutation and a query now in Graphql. Let me, I'll show you in this file that there are two main ways that you will interact with Graphql servers: one is by posting a query which basically means you are retrieving data and the other is by doing a mutation and the mutation basically means a change in some way. , so these two things you'll need are somewhat similar to what you have in a rest API with a get request versus a post or delete request, so I have queries. and limitations and you always need to define them and you can define your own custom versions of this later I'll show you in a minute and then we have the type definition, in this case the type definition is empty, so actually this graphical service doesn't I don't expose anything and as you can see I have the file structure for this here, so I created a schema folder that contains this create.pi and types.pi file and then the application loads them and just creates the schema in this graph in particular. route, so it's very basic at the moment, there's nothing here related to blogs or authors, but the first thing I'm going to do is define a couple of types and queries, so let's start with the queries and I'll add here to the main typedef a query of type that Github copied and already completes and then we will have the blogs because we want to retrieve the blocks and that will return an array of blocks, this is how we define it in Graphql, then we will have a blog that we will get through an id and this is what comes with github.
We agree, but we actually want this id not to be at the end, but to be an actual id, so this is what we're going to do here and this will also return a block with an exclamation point, which means that this It will either always return a block or raise an error, so the exclamation point basically means it's not an optional value and we're going to do the same thing for authors, so here I have authors, which is an array of one authors list and then I will have author by id and that will return the author so these are the basic queries that we also had in the rest API above and I defined them here now of course I'm using a couple of types but I have a blog here and an author which right now are not defined, so let's define these as well and to do that I'm going to create another file here inside the outline folder which I'm going to call let's get started. with the blog, so I'm going to call this blog.pi where I'm going to store the blog type data, so let me create a blog type definition here which is just a constant string that contains the type definitions for the blog and that goes to have a type blog like this and there we will have an id, each blog will have a title, it will have content that is also a string and there will be an author and we will also put an exclamation point behind that. let's assume that each block actually has an author and then we have a key that we still need to add, so this is basically our blog type definition and now what we can do is handle here the various queries that we defined in the types here to that we have blogs and we got a blog by id, so they basically correspond to what we had in the rest API before and these things are going to be part of this type of main query that we have here, so block and blocks are going to be ​​part of this and the way you do it in ariatni is like this, the first thing we need to do is import from the schema point types the query because we're going to add the block queries here, so we're going to need that object. and then we'll add a field to this so that the query actually behaves like a decorator in ariatne, so I can have a field here and those are the field blocks because we're going to read all the blocks and then I'll add a function here.
Let's call that resolve blocks and it will get some parameters, so this information object which is actually of type graph ql resolve information and this will return a list of blogs and now of course we need a couple of imports to resolve all that stuff . what we will need is from Graphql, we will need resolution information from Graphql and from the data, we will need a block because that is what we are going to return and we will also need all the blocks and let's go If we also need a gut blog so that all the blogs and get records, our solver here be really simple because here we will just return all the blocks like this and we can do the same for the block with a particular id, so let's also add another field for the query called blog and define a function solver block and this will also get the info object but it will also get a blog id, this is how we set up a simple resolver for blogs and for a particular block and I also support all these things.
I also need to add this type definition that we just made here to the schema, so when I create again, I need to add it to the schema creation function here, so let's add it here in the typedef blog and that's going to automatically import it like this now the type definition is also available for our schema and that means it is exposed through the Graphql API, so I'm going to run the application now, so now I started the Graphql server, so when I have the Graphql server, Basically, I also have this playfield that allows you to easily write queries and get the results, so for example what you could do is write a query here and we will get all the records and for each block we want to have the id and we want to have the title and we want to have the content and then when I run this query, I get the blocks like when I used them with the rest of the API, but here I'm just defining it as a Graphql query, so look, I didn't do that.
I have to specify the endpoints. I'm specifying the way queries and mutations are handled in Graphql and because in the query we specify what we want to retrieve on the backend, we now have much more control over the data, so for example if I only need the ids of the block. I can just remove these two things from the query, then run this query again and then I just get the ids, so this gives you in the front end basically a lot more control over the data that you need and reduces the amount of data that the server needs send to you and will generally speed up your application.
I want to show you one more thing in this Graphql example which is to allow constraints, so instead of just defining a type here, if let's say you also want to update the block, then we will need an input that is a payload of the block and that will get the title, so it's not required, it's optional and we also have content and finally we need a type mutation. with which we can update the block, so let's call that update block and it will get an id and a payload and it will return a block and then what we need to do in our Graphql system is define a solver for this mutation of the update block and maybe because it's python we should also name it like that to follow python type of structure which is not necessary.
You can also use the Camel Case version. The only thing we are going to need is to add. an additional import here which is mutation because we're going to add a mutation and then we're going to do exactly the same thing we did with the query field, we're going to add a mutation field and that's an update block. and then we're going to write a resolution update block so we see that we already have it here and it will call the blog update function and then it will update the block and return it as a result, so it's very similar to how you would set it up . in a rest interface, but here we're using resolvers to do it and there's one more thing I want to show you that's actually pretty cool with Graphql and we already did that when we specified the types, but we noticed that if you look here on the blog tab , we have an author field that returns the author and that's the nice thing about graphs, that you can create these kinds of connections that create this graph type structure which is very nice, let me show you how it works.
To do this I need to specify a special query type for blogs specifically and we can do that right here. It's just one line of code with ariatni, so I'm just going to create a blog query which is what's called an object type. and that's a block like that, that's our blog query object and now we can attach resolvers to our blog object and the resolver that we're going to add is the resolver of the author field, so let me add here a query field of blog which is called author and then we have a resolution author, resolution block author, we could also call it, so what github generates here is not entirely correct, in fact, the first thing we have here is actually the block , so the first thing we get here in the solver is the actual block that this field belongs to and then what this is going to return is of course the author and then we're going to return get author and then we give it the id of the author of the blog and we need to access that through a dictionary so this is then what happens let me save this and now let's go back to our playground here and now what can I do oh this is not working somehow I think I forgot something somewhere here, let's see, so I need to add the lock query to the schema, so let's import it here and then add it to the list here, log query, obviously, if you define a query object, you also need to make sure make it part of the scheme, so that's what I have now, so let's try this one more time.
So now we're back in the playgrounds and initially we got the vlogs like that, but now what we can do because of the author resolution we just added. Now I can type here author and then from author I can get the id and the name for example and now let's run this query and you will see that now for each blog we get the author so this is exactly the graph type structure that Graphql allows you to create and I don't want this video to be too much about Graphql. mainly about the difference between rest and graphql, but here you see an important difference: instead of having to make two requests like what you have to do with the rest interface here, you just add them to the query and then get exactly the data you want .
Although necessary, Graphql does not have many of the problems that the rest of the interface has, it also presents some problems of its own. Let's look at some of the disadvantages of Graphql. One of the disadvantages of Graphql is that sending a request to the server is a bit more complicated than with a rest interface since you have to specify the query or invite you want to make and with the rest it is simply hardcoded into the endpoint and the http verb, the second problem with Graphql is that it suffers from n plus one. problem, if you retrieve a bunch of blogs with authors, a separate database request will be made for each offer and that potentially slows things down to fix this, you need to introduce some kind of local caching mechanism on the backend to avoid everything. those different database requests or a mechanism for bundling solvers in Graphql, most Graphql libraries don't offer something for that right now.
Another thing I think can be improved is the query language, which is quite suitable for both in some cases, for example specifying the notation is a You have to define variables in both the invite name and the deploy call. I think it can be shortened to make it easier to use and finally not everything in Graphql is standardized, for example it would be nice to have a standard way to do searches with page pagination. sizes and results or using cursors or something, also having some standard options for auth rules permissions would be a big improvement. I have few ideas on when to use rest interfaces versus when to use Graphql first because rest interfaces are really simple to use.
They are great for smaller applications and for public APIs, but it is very important that you have to take care of security and only expose the data that you want to be public, so make sure you use some kind of layered architecture where you explicitly translate the data from the database to your public form for more complex applications that are tightly integrated and need specific sets of data. Graphql is really a great option. Your interface will be much easier to manage, especially if you use a Graphql client like Apollo that supports things like automatic caching of local files. data subscribe to changes and more I hope this video was useful please like it if you liked it and consider subscribing to my channel if you want to learn more about software design and development thanks for watching, take care and see you soon

If you have any copyright issue, please Contact