Learning GraphQL Part 2: How to deploy an Apollo GraphQL backend

How to deploy an Apollo GraphQL server to railway.app

ยท

2 min read

Learning GraphQL Part 2: How to deploy an Apollo GraphQL backend

Photo by Dan Gold on Unsplash

Table of contents

No heading

No headings in the article.

This is continuing from my earlier post: rohanbagchi.hashnode.dev/learning-graphql where we setup a running GraphQL server and consumed it with a React client. So far so good. However we do need to deploy it to some environment that is accessible on the internet.

For the backend counterpart, I was looking for a Heroku alternative as Heroku will be killing all free plans later this year and I do not want to pay for a POC-ish project. I ended up choosing railway.app as it seemed relatively simple for my needs.

Before we can begin deploy preparations, I decided to keep introspection on. While it is not recommended for production deployment, it makes it easy to understand the structure of the graph. Also, there is no mission critical data here, so not much to worry.

Some reading material on introspection: apollographql.com/tutorials/lift-off-part5/..

Also, we need some changes in the server config as the PORT number will be provided by the server environment. This is how the server start code looks like: index.ts

server.listen({ port: process.env.PORT || 4000 }).then(({ url }: { url: string }) => {
  console.log(`๐Ÿš€  Server ready at ${url}`);
});

Next I updated the server start scripts in the package.json

"scripts": {
    "dev": "nodemon --exec ts-node src/index.ts --watch",
    "build": "tsc",
    "start": "node dist/index.js",
    "generate-gql-types": "graphql-codegen"
},

Server start for development env will now be npm run dev. Build and release will be a sequence of npm run build followed by npm run start.

Inside the railway dashboard, create a new project by linking it to the github repo.

Screenshot 2022-09-25 at 11.45.33 AM.png

Then go to settings and make the following entries:

  1. The root directory must point to /server (in our repo, the server side code lives in the /server directory. Screenshot 2022-09-25 at 11.47.04 AM.png

  2. Build command must be npm run build as we configured some time back. Screenshot 2022-09-25 at 11.48.10 AM.png

  3. Finally, the start command must be npm run start Screenshot 2022-09-25 at 11.57.44 AM.png

Once a deployment is successful, we need to ask railway to assign a publicly accessible URL for it. Screenshot 2022-09-25 at 12.03.38 PM.png If you are following along, your URL will be different.

We can keep rest of the config same.

At this point, the deployment should begin on it's own.

Our deployed GraphQL server: graphql-tutorial-notes-production.up.railwa.. Screenshot 2022-09-25 at 12.07.21 PM.png

๐ŸŽ‰๐ŸŽ‰

ย