DEV Community

Cover image for How to deploy your GraphQL API from Monorepo
Daniel Lima
Daniel Lima

Posted on

How to deploy your GraphQL API from Monorepo

What is a Monorepo ?

Monorepo is an increasingly popular approach to managing large codebases, where multiple projects are stored in a single repository. It offers various advantages such as simplified dependency management, easier code sharing, and unified versioning. When it comes to deploying a GraphQL API from a monorepo, there are a few additional considerations to keep in mind. In this article, we will discuss the steps involved in deploying a GraphQL API from a monorepo, only the backend.

Where should i deploy my server ?

Is

☁️ Cloud-based platforms: AWS, Azure, GCP
πŸ’» Virtual Private Servers (VPS): Digital Ocean, Linode, Vultr
πŸ–₯️ Dedicated servers: Hetzner, OVH, SoYouStart
🌩️ Serverless computing: AWS Lambda, Azure Functions, Google Cloud Functions
πŸš‚ Platform as a Service (PaaS): Heroku, Railway, Vercel oops, vercel no ...

I do not not recommend trying to deploy your GraphQL server on Vercel since Vercel is a Serverless and Edge-first platform.

Remember to consider factors such as cost, scalability, reliability, security, and ease of use when choosing where to deploy your server. In this tutorial i will use Railway.

1 - Set up your monorepo

The first step in deploying your GraphQL API from a monorepo is to set up your monorepo. You can use various tools such as Lerna or Turborepo to manage your monorepo. Choose the tool that best fits your needs and set up your monorepo accordingly. I will use turborepo for this project.

2 - Create your GraphQL API

The next step is to create your GraphQL API. You can use any GraphQL server implementation such as Apollo Server, GraphQL Yoga, or Prisma to create your GraphQL API. Make sure to create your GraphQL API as a separate package within your monorepo. I will use Apollo Server.

3 - Configure your Backend

Once you have created your GraphQL API package, you need to configure it for deployment.

Environment variables

You can use various configuration management tools such as dotenv or config to manage your configuration files. Make sure to separate your configuration files for each environment, such as development, staging, and production.

In my project i'm using MongoDB, so my mongo_key should be at my .env, and i always like to create a .env.example to remember the developer the keys that the project need to run.

MONGODB_API=
PORT=
Enter fullscreen mode Exit fullscreen mode

Package.json

If you are using Yarn / Npm, you need to config your runnable scripts, because is there where your server will run the start script to build and consume your API .

Railway get the "yarn run start" to be the main script to run your project, but this is configurable at the plataform.

I'll not change the command, but you can see a example of building at "./dist" and running the index.js file after build.

Thats important, because if are you not using DENO, node will not understand and transpile Typescript as default.

So you need to build your project and run your builded version.

At package.json :

  "scripts": {
    "dev": "tsnd --respawn --transpile-only index.ts",
    "compile": "tsc",
    "start": "npm run compile && node ./dist/index.js"
  },

Enter fullscreen mode Exit fullscreen mode

And at tsconfig.json :

 "outDir": "./dist", 
Enter fullscreen mode Exit fullscreen mode

Build your GraphQL API

The next step is to build your GraphQL API package for deployment. You can use various build tools such as webpack or babel to build your package. Make sure to include all the necessary dependencies and configuration files in your build.

4 - Deploy at Railway

Create a account, use github to auth.

Choose your monorepo project.

Add your ENV :

Image description

At general tab config the railway to run at your backend folder :

Image description

πŸŽ‰ Congrats

If every things is ok you can choose a domain :

Image description

And when access it you should see your super cool server running:

Image description

Thanks for read !

Repo: https://github.com/bolodissenoura/GQL-study-project

Hope this post can be helpful!
For some feedback or more content, follow me on twitter or github

Top comments (0)