Setting Up Prisma with Heroku and a Postgres Database

October 22, 2019 0 Comments

Setting Up Prisma with Heroku and a Postgres Database

 

 

In this article, we’re going to go over the basics of creating a GraphQL API using Prisma with a Postgres Database.

Prisma is a GraphQL framework that takes care of all of the little and redundant things like data relationships and CRUD operations for every type. The setup can be a bit complicated, but Prisma will more than make up for it by how much it’ll help you build quick and stable GraphQL APIs.

Prerequisites

While you don’t need to know much about GraphQL to set up Prisma, you obviously won’t be able to do play around with it without some basic skills like working with mutations and subscriptions, which I covered here, and using schemas and resolvers, which you can brush up on here.

Knowing a bit of Docker couldn’t hurt either, but isn’t necessary.

Installation

Since installing Docker can differ depending on your OS, it won’t be covered here to keep things brief. If you’re new to Docker, I recommend checking out Docker Toolbox to get started.

Getting Prisma is as simple as you would imagine by installing it globally using either npm:

…or Yarn:

Database Setup

First we’re going to need a database to store everything, since GraphQL is just an intermediary layer. We can do this by starting a new project over on Heroku. Over in Resources you should have the choice to Find more add-ons, which should take you to a page with a bunch of database options. Currently Prisma only fully supports MySQL and Postgres, with future support for MongoDB on its way. We’ll get started with Heroku Postgres, to keep everything in Heroku itself.

Now back in resources we can open our new add-on and go to settings for our Database Credentials. We’ll need all of this information to connect our container to our database.

Prisma Credentials

These credentials won't be available when you're reading this, don't even try ;).

Prisma Setup

Now that we have Heroku with the Postgres add-on setup, we can move on to setting up Prisma itself. The prisma init command will give us some help for creating our GraphQL API’s boilerplate.

The terminal should ask if you want to use an existing database, and yes we do! Then it should ask for all of our credentials from Heroku, fill it out and say yes to the SSL. If it asks to generate a client, pick don’t generate.

This should have created a new folder in your current directory called prisma, with three files in it.

* prisma 📂 * datamodel.graphql * docker-compose.yml * prisma.yml 

Datamodel is what actually sets up our GraphQL types and relationships. If the file’s content isn’t styled I recommend using the GraphQL for VSCode extension. Prisma may have created a datamodel.prisma file instead, you can just rename it to datamodel.graphql for the syntax highlighting, just remember to go to prisma.yml and rename the reference there too.

datamodel.graphql

type User { id: ID! @id name: String! 
}

Your docker-compose.yml should look something like this, with your own Heroku credentials, of course.

docker-compose.yml

version: '3' 
services: prisma: image: prismagraphql/prisma:1.34 restart: always ports: - "4466:4466" environment: PRISMACONFIG: | port: 4466 databases: default: connector: postgres host: YOURHOST database: YOURDB schema: public user: YOURUSER password: YOUR_PASSWORD ssl: true rawAccess: true port: '5432' migrations: true

Docker Setup

Once you have Docker Toolbox installed you can open the Docker Quickstart Terminal. This may take a minute, but when it’s ready it’ll show this cute little whale, his name is Moby Dock 🐳.

Moby Dock

Now we can go into our prisma.yml file and link to our Docker endpoint, which will differ depending on your OS. If you’re on Windows then you’ll want to use the IP Docker gives you with :4466, and otherwise use http://localhost:4466.

prisma.yml

endpoint: http://192.168.99.100:4466 # or http://localhost:4466 
datamodel: datamodel.graphql

Now we’re ready to actually start up a new container, first move into the prisma directory and use docker-compose to start up our container.

$ docker-compose up -d 
$ prisma deploy

Now you should be able to go to whatever endpoint you passed to prisma.yml to open up GraphQL Playground. If you look at the docs inside of the playground you’ll notice that there are already queries, mutations, and subscriptions that we never created. This is one way that Prisma makes our lives a lot easier, just by having our User type over in datamodel.graphql every other operation was automatically generated for us.

Try creating and deleting a user with the help of the GraphQL Playground docs, everything we do here is actually be saved to our Postgres database.

GraphQL Playground

When you make any changes to datamodel.graphql, you will need to re-deploy Prisma to update the Docker container.

Closing Thoughts

I hope this was a helpful introduction to get things up and running with Prisma, Heroku and Postgres. In future posts we’ll go over more on how to work with Prisma to create your GraphQL APIs.


Tag cloud