RESTful APIs are great: they follow a resource-oriented approach that is clear and well structured. But when the data gets more and more complex, the routes get longer and longer, and sometimes it isn’t even possible to fetch the data you want with only one request. This is where GraphQL comes into play. It’s designed to work with data represented by a graph, and it has a powerful query syntax for traversing, retrieving, and mutating data.
In this course, Markus Mühlberger will show you how to build a back-end GraphQL API. You'll start by using an interactive demo server to explore the basics of GraphQL and the syntax. Then you'll move on to implementing your own GraphQL server—a database of information about Star Wars characters—complete with queries, custom types, mutations, and a number of other advanced GraphQL features.
1.Introduction2 lessons, 05:15
2.GraphQL Basics4 lessons, 20:29
3.Using GraphQL3 lessons, 16:17
4.Building GraphQL4 lessons, 25:20
5.Conclusion1 lesson, 01:23
1.2 Installing the Prerequisites
Hi, and welcome back to build APIs with GraphQL. In this lesson, we will install all prerequisites needed for this course and set up the Node.js server to run the GraphQL API. First of all, we need Node.js. Depending on your operating system, you can either install it from the official website, use a version manager like nbm, or, if you're on a Mac, like I am, use Homebrew. The second thing you will need is a package manager. npm, Node's default, is fine, but I like Yarn, and we'll use that. Finally, I'm going to install nodemon, so the server automatically restarts when a file is changed. Without it, you would have to kill and restart the Node process every time you save a change to the file. To install it, either run yarn global add nodemon or npm install -g nodemon. Now that all prerequisites are installed, we can start building the server. I'm going to use the Express JS web framework, because the reference implementation of GraphQL has an Express-compatible package that is very easy to add. Let's get started by running yarn add express express-graphql to initialize the package to the JSON file and install the two necessary packages. Then, in your editor of choice, -atom, add a server.js file under projects folder. This is where our Express app is going to live. We're using plain Node here, so we need to use the require syntax and not import. First, let's require express. Then, we also need expressGraphql. I'm going to store it in the expressGraphQL variable. Now I can create an express app by calling the required express module. Express GraphQL is a middleware for Express, so we can call app.use and pass the GraphQL endpoint. Enter required package. You can also pass some configuration options to Express GraphQL, like graphical, which we will use as a client. Finally, we have to start the server by calling app.listen and pass in a port. In the callback, let's also confirm that the server is running with a consulted lock statement and output URL. In this shell, we can now call node1 server, which will start the server. A neat treat on Mac OS, when you double-click the URL while holding down the command key, it will automatically open the page in your default browser. In our next lesson, we are going to talk about the advantages of GraphQL compared to our classic RESTful API. See you there.