Build and Deploy a GraphQL API to the Edge with MySQL and PlanetScale — Part 1

Build and Deploy a GraphQL API to the Edge with MySQL and PlanetScale — Part 1

Set up Grafbase

This series is perfect for anyone looking to remove the bloat from their GraphQL backends and switch to a fully managed deployment platform with support preview environments, instant GraphQL APIs, and more.

💡
The code for this tutorial is available on GitHub.

In this 11-part series, we will build a GraphQL API with Grafbase, and persist data to a MySQL database managed by PlanetScale.

Here's a breakdown of what we'll be building:

  1. This is part 1

  2. Set up MySQL Database with PlanetScale

  3. Set up Database Connection

  4. Create Product Mutation

  5. Update Product Mutation

  6. Delete Product Mutation

  7. Fetch Product by ID or Unique field Query

  8. Fetch all Products Query

  9. Add cursor-based pagination

  10. Enable edge caching to reduce database round trips

  11. Deploy to the Edge with GitHub

Grafbase

Grafbase simplifies the process of creating and implementing your personalized GraphQL API at the edge while ensuring complete end-to-end type safety.

By using resolvers and connectors, Grafbase allows seamless integration with any data source. Additionally, you can take advantage of features such as edge caching, authentication and permission rules configuration, serverless search functionality, and more.

Grafbase's versatility extends to local development as well, thanks to the Grafbase CLI. Each Git branch has its preview deployment, facilitating convenient testing and collaboration.

PlanetScale

PlanetScale simplifies database management and scaling. It ensures high availability, seamless replication, and effortless deployment across multiple regions.

With features like automatic sharding, intelligent load balancing, and instant schema changes, PlanetScale simplifies integration and offers robust security measures, including end-to-end encryption and comprehensive access controls.

From startups to enterprises, PlanetScale provides a reliable and scalable solution for managing databases, allowing you to focus on building innovative applications and services.

1. Set up Grafbase

Let's begin by creating a new Grafbase project. If you have an existing frontend application that you want to create a GraphQL API, you must run that inside of that project's root directory.

npx grafbase init graphql-mysql-api

This command will ask whether you want to create a Grafbase project using GraphQL SDL or TypeScript as its configuration type.

💡
This guide will use the TypeScript SDK to configure all queries, mutations, types and input types.

We'll next install the graphql dependency so we can adequately throw GraphQLError's inside resolvers later on.

cd graphql-mysql-api
npm install graphql

That's it! You now have a Grafbase project set up and ready to use.

👉 Continue to Part 2