Check out a free preview of the full Client-Side GraphQL with React, v2 course

The "What is GraphQL" Lesson is part of the full, Client-Side GraphQL with React, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott describes GraphQL as a powerful query language for APIs that allows clients to request exactly what they need and nothing more. He discusses the advantages of GraphQL over REST, such as type safety and the ability to query highly relational data in any format. Scott also provides examples of GraphQL queries and explains the basic building blocks of a query, including fields, nested objects, and arguments.


Transcript from the "What is GraphQL" Lesson

>> Scott Moss: First thing is, what is GraphQL? And why is it here? So here's the definition of GraphQL, and I'll basically tell you what I think it is and how it works. Graphkill is a powerful query language for APIs and a runtime for executing those queries with your data's type system.

Unlike REST, which uses predefined endpoints to access data, GraphQL allows clients to request exactly what they need and nothing more. ChatGPT helped me write that, by the way, cuz it sounds nothing like me. [LAUGH] What I think GraphQL is in the way I would describe it, it's basically like TypeScript for your API.

That's basically it. Who here has used TRPC or heard of it? Okay, TRPC is another way you can get typed queries. It's another way you can get typed data for your APIs. GraphQL what it is, it's basically a few things. There's a query language, which looks very similar to JSON and which you can use to describe the shape of your data.

And then there's a backend component that takes that query language, does exact type matching with it to make sure it fits the type so it does the craziest input validation you can think of on down to a property level. And then if that works, it then tries to resolve each one of those fields on your query that you asked for.

So that way, you can query highly relational data in any format that you want, whereas if you didn't have GraphQL, you could only query the relational data in whatever way that the person who built it back in allowed you to. Because they would have to write those queries, those database queries ahead of time, and it create the routes ahead of time and then give you the options for the query string ahead of time and like here are the rules you have to follow.

With GraphQL it's infinite. You can do whatever you want. So it's a different mental model when thinking about it, especially on the back end, but basically it's just a way for you to describe the data that you want and get it back exactly the way that you asked for it and guarantee type safety.

That's it So, that's GraphQL. So it's pretty cool. Created by Facebook a long time ago, and then they open sourced it. And so far it's been well used. You have big companies like Netflix using it. There are like startups who have billion dollar products that are tools just for GraphQL.

It's a huge ecosystem of people building products, companies using it, tons of open source. There's so many plugins and applications and tooling built around GraphQL. And one use case that I've learned with GraphQL that's quite useful in the client is for mobile, like mobile and GraphQL actually works very well.

Because for mobile you're very data constrained, you're network constrained. You really only wanna get the information that you need to put it on the screens. Anything more is can be quite expensive as far as like network requests. So GraphQL is great for that. And there's many other but that's actually a really great one.

I kind of already talked about some of the main advantages of rest and things like that. But really, the big advantage is you basically get type checking for free, you don't have to do the input validation. And on the Frontend, at least you get to describe what you want.

Whereas like with REST, you would have to have a conversation with the back end team like, okay, this is what I need on the frontend, can you make me a route that returns it exactly like this so I can use it on the frontend? You don't have to have that conversation anymore.

You just write the code for it and it just works. So you describe that shape within the constraints of the schema that you created. Some of the bigger differences are, REST is like a combination of a URL and a verb, basically, right? So if I have resources, like a user, a post, an article, whatever, for every one of those resources, I have a URL path for it, and then I have a verb that describes some crud action, a post for creating, Git for getting a patch, or a put for updating things like that.

So for every one resource, you might have, I don't know, five, six different routes just for one resource and REST. And then if you have many resources, those are a lot of routes you gotta manage. With GraphQL, it's just one URL the entire time, no matter what you wanna get, it's usually GraphQL.

And it's mostly always gonna be a post request. Pretty much guaranteed always gonna be a poster Chris. So that right there just makes it simpler in its own. You're only managing one URL or one route and you don't have to think about HTTP verbs. You don't have to think about any of those verbs or those methods because it's always gonna be a post.

So that already makes it simple. And then I would say, yeah, I could talk about the differences a lot in practicality, but high level, that's it, yeah, Mark.
>> Mark: What do you get with type safety when it comes to GraphQL?
>> Scott Moss: Yeah, so type safety is just a fancy word of saying when a client is sending some type of query to the back ends, let's say it's trying to create a user.

And to create a user, you have to supply an email and a password. Well, if you wrote a really good back enf, what you would do, you would first before you operate on your database, you would check the inputs that came from that client to make sure that there is an email there, you would make sure there is a password there.

If you had other validations that you put on the front end as far as like the length of the password, you would also check that on the back end. So it's like you're validating the input before you operate on the database, but you'd have to write that code yourself if you were doing something non-GraphQl.

Whereas with GraphQL, that's done for you for free. That type of validation is executed by the GraphQL server without you having to write the code for it before any of your logic runs to actually satisfy the query. So it basically just means it'll break if the query that you send up doesn't satisfy the type constraints of your GraphQL server.

Just the same way as if you would throw an error if someone sent up a payload that wasn't the exact shape or type that you need in order to resolve that request. It's just done for you for free. So that's what I mean by type safety and then to expand on that, you can generate type scripts from your graph QL and have type safety all the way down to your frontend code, and then it gets even crazier.

So that's what that means.
>> Speaker 3: When you say generate type script, I mean, you mean like for Frontend or explain that expansion?
>> Scott Moss: Yeah, for sure. So there are tools in GraphQL that will look at a GraphQL schema and then output a TypeScript file that has TypeScript types that you can then import into any other TypeScript file you want.

So in this case, maybe it's a React component, it's a file in which you're doing a query and then now, instead of just getting back a generic data property that is the result of your query. You can map that to an actual type that matches a server, right?

So like, you have
>> Speaker 3: Like object mapping.
>> Scott Moss: Yep, object mapping. Exactly, yeah. Because it's very common. TypeScript is great, but where it starts to fall apart is when you have to do network requests, because how would TypeScript know that this API returns this type of data, it just doesn't know.

So with GraphQL and then doing code generation, you can bring that all the way down to, literally the component level. So if you integrate with that tooling, yeah, you will have full type safety. So, and there's different ways to do that. TRPC solves this differently than GraphQL. GraphQL you have your cogeneration, TRPC you don't because it's Typescript and it uses TypeScript in the same repo to get it to work.

So there's different ways but yeah, there's cogent. We won't be doing code in this course because I felt like it's too TypeScript specific. And you have to know TypeScript to really understand it. So I didn't do it, but it's actually quite easy to do. Let's talk about the query language.

So GraphQL language, GraphQL. So let's talk about the foundations of it and then we'll kind of take a look at it. So one of the few things or one of the basic things about the Query language is something called a field. And again, I said, if you've written JSON before or you've written Javascript objects, you'll probably feel pretty familiar with GraphQL, is very similar to that.

So a field is exactly what it sounds like. It's like a field on an object. So you can specify which fields from what objects you need, and you'll only get back that field if it's there, depending on the schema and whether or not it's a non no field or it can be no or whatever.

But if you ask for it, that's what you'll get and nothing else. Nested objects, this is really cool because this has a lot to do with relational data. So if you had an author that had posts and you wanted to be able to get the posts for an author, you can start from the author object and nest into t the post object and ask for that data as well.

So you can do that in a way that is beneficial for the front end and not the other way around where the person writing the back end will have to predefine those queries ahead of time or they'll have to like create. Basically what will end up happening is people in the back end end up creating their own query language on a query string, and like, yeah, if you sent me this on a query string, this is how you can change how I do the relations.

But at that point, they're creating their own query language. So GraphQL is like trying to solve that. And then there's arguments. So you can pass different arguments to some of these fields and nested objects. That way you can do things like give me this author by this ID or given this user by this e-mail.

So there's a lot of argument passing. We'll discuss a lot of that as well. But those are the basic building blocks of doing a query from a request site. There's a little more we'll cover, but these are like the foundation, the basic stuff. So enough talking, let's get into it.

So here's what a graphical query looks like. Like I said, the kind of looks like an object, right? There's some slight differences here, you don't see like a key value, so it isn't like a field and then a value here, what this is describing is, imagine if we had a blog application where authors have posts.

This is saying, hey, I want to execute the author query, and I want to get the author with this ID. And then on the author object, I wanna get the name, I wanna get the email. And then I know authors have a list of posts. So what I wanna do is I wanna get the title and the content and the publish date for all the posts for this author.

So what I would get back is an object called author that has all these properties on it the exact same name, it will have an array of posts on it. And those objects will have these properties on it. Nothing more, nothing less. It'll be exactly this shape. So now you can write your UI in a predictable way.

You know it's always gonna come back this way. And the reason you know that is because GraphQL would break before it didn't come back this way. There's no way that it will respond to you with a successful request without it being this shape, because you can only send a query that matches the schema that was created.

So if you send something that wasn't possible, you'll get back an error. So if you didn't get back an error, you got back what you asked for. And that's the guarantee, that's the contract that type safety. So it's quite cool, but you would have to see the other side of it, which is a GraphQL schema, which is more backend related GraphQL, I'll cover that the backend course.

This course there is no GraphQL schema, you're just interacting with the GraphQL server you don't actually create the schemer. So you just have to imagine that there is scheme in the background.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now