Building a Scalable GraphQL API with Node.js and MySQL
Are you ready to take your API to the next level? With a MySQL database already in place, you’re just a few steps away from creating a scalable GraphQL API. In this tutorial, we’ll guide you through the process of building a small GraphQL endpoint for a ticketing system, covering everything from creating models based on your database to making relations between different tables.
Getting Started
To begin, let’s assume you’re working with an existing MySQL database. We’ve provided an SQL file that you can import into your MySQL database, which includes four tables: tickets, priorities, status, and users. You can also create your own database using the provided schematics.
Bootstrapping the Application
Before we dive into building our GraphQL API, let’s set up our application. We’ll use Webpack to compile our app.js file inside the dist directory, which serves as a basic Express Hello World app. After cloning the project, run npm install
to install the dependencies, and then npm run start
to start the app.
Creating Database Models
To interact with our MySQL database, we’ll use Sequelize as our Object-Relational Mapping (ORM) package. We can either create the database models by hand or auto-generate them using the Sequelize-Auto package. For this tutorial, we’ll use Sequelize-Auto to generate the models based on our existing database.
Implementing Database Models
Now that we have our database models, let’s implement them. In the app directory, create a database.js file where we’ll import the database models and export them for use throughout our app. We’ll also fill in our MySQL database credentials and import the freshly created models.
Setting up an Apollo Server
Next, we’ll set up an Apollo server to create our GraphQL API. Apollo Server is a spec-compliant and production-ready JavaScript GraphQL server that helps us build fast and reliable GraphQL services. We’ll add two packages, apollo-server-express
and graphql
, and then set up the Apollo server inside our app.js file.
Creating GraphQL Types and Resolvers
In GraphQL, we need types and resolvers. Types describe the data we can fetch or write via a GraphQL endpoint, while resolvers are the logic to resolve a request from a user. We’ll create a new file, tickets.js, where we’ll define our GraphQL types and resolvers.
Creating Relations Between Data
To take our API to the next level, let’s create relations between our data. We’ll start by adding a relation between a Ticket and a user. We’ll edit the Ticket type to include a user field, and then add a nested resolver to resolve the user inside the ticket.
Adding More Relations
Now that we’ve added a relation between a Ticket and a user, let’s add more relations between our data. We’ll add relations for priority, status, and assignedtouser, and then test our code to make sure it works.
The Final Result
We’ve now created a usable GraphQL API that you can customize to your heart’s content. You can add multiple layers of relationships between entities, and easily create new Sequelize models for your application. Want to check out the code? You can find it in our repository.