GraphQL is the rising star of backend technologies. It's a popular alternative for REST as an API design paradigm and is becoming the new standard for exposing the data and functionality of a web server.
In this tutorial, you'll learn how to build a GraphQL HTTP server entirely from scratch.
You are going to use the following technologies:
- Node.js (opens in a new tab) - as an engine and runtime for our server
graphql-yoga- the easiest way to build an HTTP GraphQL server
graphql-js(opens in a new tab) - we will use the core
graphqllibrary as execution engine for our server
- GraphiQL (opens in a new tab): A graphical interface that allows you to interactively
explore the functionality of a GraphQL API by sending query, mutation and subscription operations. It's somewhat similar to
Postman (opens in a new tab) which offers comparable functionality for REST APIs. Among other things,
- Auto-generates comprehensive documentation for all available API operations
- Provides an editor where you can write queries, mutations & subscriptions with auto-complete and syntax highlighting
- SQLite (opens in a new tab): A lightweight and easy-to-use relational database management system for persisting data on the file system
- Prisma (opens in a new tab): The glue between your code and SQLite database. Use Prisma Client to access your database inside GraphQL resolvers
You can find the code of the tutorial in this repository (opens in a new tab).
What to Expect
The goal of this tutorial is to build an API for a HackerNews (opens in a new tab) clone.
Hackernews is a platform where people can share links to websites and people can comment on them.
Here is a quick rundown of what to expect from this tutorial.
You'll start by learning the basics of how a GraphQL server works, simply by defining a GraphQL schema (opens in a new tab) for the server and implementing your first GraphQL resolvers for posting links and retrieving a feed of links.
In the beginning, these resolvers will only work with data that are stored in memory - so nothing will persist beyond the runtime of the server.
Nobody wants a server that's not able to store and persist data, right?
No worries! Next, you're going to add a SQLite (opens in a new tab) database to the project which will be accessed using the Prisma 3 ORM (opens in a new tab).
Once you have the database connected, you are going to add more features to the API.
You will implement comments on the links and introduce graph relations.
Afterward, you will learn all about the proper error and sanitizing invalid user input.
Finally, you'll implement pagination and filtering capabilities to the API!
Let's get started 🚀