apollo-server/docs/source/deployment/heroku.md

78 lines
3.7 KiB
Markdown
Raw Normal View History

2018-04-19 21:41:27 -07:00
---
2018-04-20 20:03:50 +03:00
title: Deploying with Heroku
sidebar_title: Heroku
description: Deploying your GraphQL server to Heroku
2018-04-19 21:41:27 -07:00
---
Heroku is a common Platform as a Service solution that allows users to deploy and have a functioning GraphQL endpoint running in a matter of minutes.
2018-04-19 21:41:27 -07:00
## Prerequisites
2018-04-19 21:41:27 -07:00
The following must be done before following this guide:
2018-04-19 21:41:27 -07:00
- Setup a [Heroku](https://heroku.com) account
- [Install the Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) if pushing to Heroku manually (see below)
<h2 id="configure-heroku" title="Configure Heroku">Set up a new Heroku application</h2>
Before deploying, a new application must be setup. To do this, log into the [Heroku dashboard](https://dashboard.heroku.com/apps). Then click `New > Create New App` in the top right. The name you choose will be referred to later as `<HEROKU_APP_NAME>`, so be sure to replace it in the later sections.
2018-04-19 21:41:27 -07:00
![New App Screenshot](../images/deployment/heroku/new-app.png)
2018-04-19 21:41:27 -07:00
Name your app and hit "Create app"
2018-04-19 21:41:27 -07:00
![Create App Screenshot](../images/deployment/heroku/create-app.png)
2018-04-19 21:41:27 -07:00
## Setting up the project
For Heroku, projects can be setup using any of the `apollo-server` HTTP variants (like express, hapi, etc).
The only special consideration that needs to be made is to allow heroku to choose the port that the server is deployed to. Otherwise, there may be errors, such as a request timeout.
To configure `apollo-server` to use a port defined by Heroku at runtime, the `listen` function in your setup file can be called with a port defined by the `PORT` environment variable:
```
server.listen({ port: process.env.PORT || 4000 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
```
## Deploying the project
There are a couple of ways to push projects to Heroku. Automatically, with GitHub integration, or manually using Heroku push.
<h3 id="deploy" title="Deploy with Heroku Push">Deploying with Heroku push</h3>
2018-04-19 21:41:27 -07:00
Install the [Heroku Cli](https://devcenter.heroku.com/articles/heroku-cli), then inside of your project, run:
```shell
2018-04-19 21:41:27 -07:00
$ git init #existing git repositories can skip this
$ heroku git:remote -a <HEROKU_APP_NAME>
$ git add .
$ git commit -am "make it better"
$ git push heroku master # or your branch name
2018-04-19 21:41:27 -07:00
```
Send a query to your GraphQL service at your Heroku Application at `<HEROKU_APP_NAME>.herokuapp.com`
<h3>Automatically deploying with GitHub</h3>
2018-04-19 21:41:27 -07:00
If the project is already pushed to GitHub, it may be easier to setup automatic deployments from the project's repository
2018-04-19 21:41:27 -07:00
On the Heroku dashboard, click on the name of the app that will be deployed from GitHub.
2018-04-19 21:41:27 -07:00
Then, on the app deatail page, there is a tab bar at the top, with a "Deploy" option. On that page, the deployment method can be chosen and setup to integrate with GitHub.
2018-04-19 21:41:27 -07:00
![github deployment instructons](../images/deployment/heroku/heroku-github-instructions.png)
2018-04-19 21:41:27 -07:00
<h2 id="env-vars" title="Environment variables"> Configuring environment variables</h2>
2018-04-19 21:41:27 -07:00
In order to enable the production mode of Apollo Server, you will need to set the `NODE_ENV` variable to production. To ensure you have visibility into your GraphQL performance in Apollo Server, you'll want to add the `ENGINE_API_KEY` environment variable to Heroku. For the API key, log into the [Engine UI](https://engine.apollographql.com) and navigate to your service or create a new one.
2018-04-19 21:41:27 -07:00
Then under the “Settings” tab, click “Reveal Config Vars". Next set `NODE_ENV` to `production` and copy your key from the [Engine UI](http://engine.apollographql.com/) as the value for `ENGINE_API_KEY`.
2018-04-19 21:41:27 -07:00
![Add Engine Api Key Screenshot](../images/deployment/heroku/add-env-vars.png)
2018-04-19 21:41:27 -07:00
Send a query to your GraphQL service at your Heroku Application at `<HEROKU_APP_NAME>.herokuapp.com` and then check out the tracing data in the [Engine UI](http://engine.apollographql.com/).