Skip to main content

Command Palette

Search for a command to run...

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

How to deploy an Apollo GraphQL server to railway.app

Updated
2 min read
Learning GraphQL Part 2: How to deploy an Apollo GraphQL backend

This is continuing from my earlier post: https://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 https://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: https://www.apollographql.com/tutorials/lift-off-part5/deploying-apollo-server#-what-is-graphql-introspection

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: https://graphql-tutorial-notes-production.up.railway.app/ Screenshot 2022-09-25 at 12.07.21 PM.png

🎉🎉

Learning GraphQL

Part 2 of 3

In this series I will be documenting my learning experience while setting up an Apollo GraphQL server with a basic schema and consuming it in a React app.

Up next

Learning GraphQL Part 3: Consuming data from Apollo server in a React app setup with vite

How to setup a React app configured with vite to consume data from a GraphQL backend locally and in production.