What is GraphQL?

GraphQL is a query language for APIs. In this lesson, we go through an initial discussion on GraphQL and how GraphQL differs from traditional REST APIs.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL

What we've built so far with our Node app conforms to a little more to what a REST API is, REST standing for Representation State Transfer. In traditional REST APIs, the client interacts with a server by accessing various endpoints to get, post, put, or delete data while leveraging the HTTP protocol. In our app to get all the listings we had to make a request to the /listings endpoint. If we wanted our server to serve data for just one listing, we could implement a /listing/id endpoint, where ID is a dynamic value. If we had users in our mock data, we could also implement a user /id endpoint to serve certain user data, and so on. Now let's consider an example scenario. A client app needs to display some user information plus all the listings for that user. Now this client doesn't have to be ours. Someone else can develop a web or mobile application that interacts with our API. How would we play out this scenario with REST? With an example REST API, the client would need to make a request to /user/id to fetch the initial user data for that particular ID. Then the client would have to make a second request to something maybe like user id /listings. That returns all the listings for that user. So far this isn't too bad, it's just two requests. Where does GraphQL fall in here? GraphQL is a query language for making requests to APIs. With the GraphQL, the client tells a server exactly what it needs and the server responds back with the data. GraphQL is not tied to any specific fronted or back-end technology, meaning you can use GraphQL with React, View, Node, Ruby, Rails, etc. Here is an example query where we attempt to query the user of a certain ID. And when we query this user, we can tell our GraphQL API which fields we want to be returned. In this case, we're saying we want the id field, the name field, and then for the listings for this user, we also want the id and title of these listings. Now GraphQL is also a typed language. So before we declare how we want each of these fields to be resolved in our back-end API, we have to tell GraphQL the type of the data we expect. We want the id to be of type GraphQL id, the name to be of type GraphQL string. We want the listings field to be of type a list of listing types. And the exclamation mark here is basically telling our API that this is a required field. GraphQL allows for some significant benefits. GraphQL APIs are intuitive because the client specifies exactly what data it needs, thus making the code intuitive and easy to understand. GraphQL APIs are performance because no useless data needs to be transferred. And as a result, reduced latency will make our app feel faster. This is especially important for slower internet connections. And GraphQL APIs are typed. GraphQL uses a type system of its own to validate requests. This integrates beautifully with TypeScript to create a robust, statically typed application. GraphQL APIs are also self-documenting. They encourage the use of GraphQL IDEs or integrated development environments. And GraphQL APIs also consist of a single endpoint. So for all the data that might be returned from a database or a server, everything is to be served within a single endpoint in GraphQL. Listings, a certain listing, users, a certain user, etc. As we've mentioned, GraphQL isn't tied to any specific technology. This is because GraphQL is a specification, not a direct implementation. The community has created several different implementations based on the tools and technologies we might be using. For example, there's the Apollo Server library for Node Frameworks, GraphQL Java for Java applications, GraphQL Ruby for Ruby applications, and a lot more. And this is the same for client tools as well. There's Apollo clients for the React, View, and other JavaScript client code that can exist. There's GraphQL Python to work with Python, GraphQL.client for .NET applications, and a lot more as well. This all might still sound pretty vague and too good to be true. So in the next lesson, we're going to go through an example-driven approach to compare a real REST and GraphQL API. We'll be using the GitHub API that has been made publicly available. And only after going through the next lesson will we come back and discuss some of the more core concepts of GraphQL. [BLANK_AUDIO]