How to set up a powerful API with GraphQL, Koa, and MongoDB — deploying to production

Our GraphQL runs smoothly locally, but what if we want to share it with the world?

P.S: This is a series where I explain how to set up GraphQL from scratch and go to production. Check out the previous chapter if you haven’t.

In order to make our graphQL API available to the public, we’ll need to deploy it on a production server. I chose Heroku for its simplicity.

Head over to Heroku, create a user if you haven’t, and create a new project. We don’t have to pay anything for our demo.

Head over to the deploy tab and sync the Heroku with Github. The easiest way is to deploy via our Github repository.

And finally, add the mlab Heroku add-on.

Heroku Dashboard

pm2 set-up

Next, let’s get the pm2 ready for production. Since Heroku runs our scripts, we’ll need to add pm2 to our package.json.

yarn add pm2

Next, we’ll generate the file for the pm2 ecosystem.

pm2 init

And the contents of the ecosystem.config.js file will be;

Inside the ecosystem.config.js we can specify all kinds of environment-related variables, such as how many instances do we want to run, which script to run, etc.

Learn more about ecosystem file here.

Before pushing our changes to the repository, there’s a small change we need to do with our Koa server.

server.js

app.listen(process.env.PORT || 9000);

This guarantees Heroku gets to pick the port they wish.

Publishing the app

Head over to the deploy tab on Heroku, scroll to the bottom, pick the branch you wish to deploy, and press the deploy branch button.

Voila!

You can set-up an automatic deployment too, it’s in the deploy tab.

Head over to https://koa-graphql-mongodb.herokuapp.com/graphql and you can see our graphQL app is open to the public.

If you don’t know the URL of your app, use the Open app button.

We can query on the Heroku server, just like we did locally.

Notice the URL is public, anyone can access it.

Securing our app

Our app is not very secure right now. We have hard-coded our MongoDB and there is no way to change it other than code. Ideally, we want to have two, if not even three separated databases. One for local development, one for production, and (optional) one for testing.

We can secure our API with dotenv files. If you ever used .env files – you’ll feel right at home.

Create the two following files;

touch .env .env.example

The .env file is for reading values, the .env.example we push to the repository with empty values so other developers can contribute.

.env file;

MONGODB_URI=MONGODB_URI=mongodb://indrek1:indrek1indrek1@ds213615.mlab.com:13615/koa-graphql

Note: Don’t forget to add the .env file to .gitignore!! We do not want to expose this variable to the outside world.

Injecting .env variables to our app

There’s a popular package called dotenv which injects the .env values to our process.env object.

Add the dotenv package.

yarn add dotenv

Place this code in server.js

require('dotenv').config()

And finally, inject the .env variable to our database.js

mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true });

Voila! If we open our graphiql or insomnia client, all queries should work.

Push the changes to the repository and deploy.

Production database

With this setup, we have 2 databases. One for dev and one for production. No need to change anything, the production database gets automatically inserted by Heroku already.

Head over to the production app and query all gadgets. If everything went alright, the query should return an empty array.

Perfect! Let’s add a gadget.

Head over to the Heroku dashboard, click on the mLab add-on. This should redirect you to our production database. If we inspect the tables and the database name;

Perfect! We have successfully made our dev and production database. The reason why this is a good practice is we don’t want to fiddle too much with existing records already. Imagine users already have registered, paid for our app, etc. We don’t want to disturb this process. So we use dev for adding new features and experimenting/

Note: If you want to access the GraphQL via the frontend, you’ll need to enable cors. Use this package (@koa/cors)

yarn add @koa/cors
server.js

Congratulations on making it to the end!

You did it, you made it to the very end. Well done! I assume you like to read and like GraphQL — if you want to learn more about GraphQL, I’d suggest reading through the “The Road to GraphQL” book.

As always, source code for the chapter can be found here

Thanks for reading! If you have any questions, I’m active on Twitter

You might enjoy my previous articles;

Software engineer, tech journalist, startups. Stay up to date https://thewholesome.dev, www.startupscrushing.com Twitter @ https://twitter.com/trevorlasn

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