How to build a GraphQL API

Perhaps you’ve heard about GraphQL and sold on the concept. Now you’re looking at how to get a scalable and ‘best practice’ GraphQL API up and running.

Prisma is a great option. With tens of thousands of Github stars, hundreds of contributors and backed by a well-funded & for-profit company (which has raised a few million $ by investors from companies like Heroku, Netlify and Algolia) — Prisma is a solid and growing product.

Today we’ll take a look at what and why you should consider using Prisma and GraphQL-Yoga to build a GraphQL API.

The Prisma architecture

  • The Client (eg. a mobile app and/or website) connects to a GraphQL server — this is where GraphQL-Yoga comes in:
  • The GraphQL Server — GraphQL-Yoga (provided by the Prisma team) is a great GraphQL server which talks to Prisma nicely and uses the popular Apollo Server under-the-hood. The GraphQL server contains our custom business-logic — everything from CRUD operations, authentication & authorization, database schema, migrations, seeders etc. Our GraphQL server obviously needs data, and this is where Prisma steps in:
  • Prisma sits between the business logic and the database, acting as an ORM (you’ll see why soon — keep reading). Prisma can be run on its own server (optional) to enhance your scaling options or run it on the same server as your database (eg. with Docker).
  • Database — currently Prisma can talk to MySQL, Postgres and MongoDB. Your database can sit on the same server as Prisma or you could use a database-as-a-service such as Digital Ocean’s Managed DB or AWS RDS.

When I first read about the Prisma architecture, I felt a bit uneasy. At first, the added complexity of having another (the Prisma layer) service to manage seemed unnecessary. When I learned why, it started to make sense, so keep reading.

What does Prisma do?

Prisma is a data layer that replaces traditional ORMs in your application architecture. The key features ‘on-the-box’ are:

  • Enables realtime subscriptions (no matter the database)
  • Auto-generates type-safe database access
  • Supports SQL and NoSQL DBs and handles joins and filtering via a consistent and seamless API
  • Handles migrations and seeding
  • Has a visual data management interface

So, Why Prisma?

The following features convinced me that the Prisma way is a great option and robust pattern for building a GraphQL API.

1. Realtime data streaming

The majority of database types (eg. MySQL, Postgres) don’t support realtime streaming (letting clients subscribe to changes happening to the database). Implementing this realtime feature means that we need additional services (eg. Pusher) and can get complex.

Prisma’s dedicated data-layer provides a realtime API for every (supported) database, letting you subscribe to any database event (eg. data create, update or delete) without any fuss.

2. Scalable Architecture

A lot of the complexity in designing your application architecture, comes from query optimization, performance and security. Industry titans (eg. Facebook, Twitter) implement a DAL (data access layer) to abstract complexities of database access. The DAL provides an API to be consumed by the application server, allowing us to think about what data we need rather how to securely and performantly retrieve it from the database.

Prisma is an auto-generated DAL which follows the same principles as industry-leading DALs (such as Twitter’s Strato or Facebook’s TAO). Prisma allows you start your project with a clean architecture from the beginning.

What’s more, the GraphQL-Yoga component of the stack can be deployed “serverless-ly” via Serverless Framework to AWS Lambda (or the like).

3. DX — Up and Running Quick

Purely based on my own experience: I was able to get up and running with Prisma and GraphQL-Yoga relatively quickly. I tried a few Prisma competitors (Apollo Server, AWS AppSync) but had the best DX (developer experience) with Prisma. From local development, staging and deploying to production — Prisma has some great CLIs and other helpful tooling to get moving.

4. Longevity

Let’s be honest, learning a new platform can be draining. If we’re investing time and energy into something new, we should be confident it’s not going to ‘go away’ any time soon. After reviewing Prisma, it would appear that the company and team supporting it is showing markers of longevity and the community is active and responsive.

Get Started with Prisma & GraphQL-Yoga

You may now be interested to try out Prisma. I’ve created a boilerplate Prisma project with a bunch of basics that you may need to start your next project:

  • Authentication and Authorisation
  • Example data types/models
  • An opinionated boilerplate file structure
  • Tooling to help in dev and to deploy
  • Subscription example

You’ll also find helpful instructions to get you started in both dev and in production (I’ll link you to the repo’s instructions as it’ll be kept up to date):

I lead, design & build digital goods @wearepvtl

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store