Skip to content

Starter kit for GraphQL server and application with MongoDB as database, Lerna for monorepo, deployable to AWS Lambda, local development

License

Notifications You must be signed in to change notification settings

artem-korolev/prismajs-mongodb-graphql-aws-lambda-starter-kit

Repository files navigation

PrismaJS with TypeScript + MongoDB + Lerna + AWS Lambda + Serverless

project build success status

Starter kit for quick start with Typescript, Lerna, GraphQL and MongoDB locally and deploy to AWS Lambda + MongoDB Shared Cloud using GitHub Actions.

Demo

Checkout GraphQL with Playground deployed to AWS Lambda with MongoDB Cloud Shared:

https://rb7zk6fsce.execute-api.us-east-1.amazonaws.com/dev/graphql

Query example:

# Write your query or mutation here
query {
  users {
    id
    email
    name
    __typename
  }
}

🤘💪🤣😍❤

Table of Contents

Technologies used

TypeScript Lerna GraphQL MongoDB Prisma Parcel.js GitHub AWS Lambda

  • Prisma - Next-generation Node.js and TypeScript ORM with GraphQL interface

    https://www.prisma.io/

  • MongoDB - JSON document database

    https://www.mongodb.com/

  • Typescript - JavaScript with syntax for types

    https://www.typescriptlang.org/

  • Lerna - A tool for managing JavaScript projects with multiple packages

    https://lerna.js.org/

  • GraphQL - query language for APIs and a runtime for fulfilling those queries with your existing data

    https://graphql.org/

  • Parcel.js - Parcel is a zero configuration build tool for the web. It combines a great out-of-the-box development experience with a scalable architecture that can take your project from just getting started to massive production application

    https://parceljs.org/

  • Docker Compose - tool for defining and running multi-container Docker applications. Its used in this project to run MongoDB database in dev environment locally. Prisma Client requires Mongo to run in replica mode, so standalone MondoDB Community Server is not enough

    https://docs.docker.com/compose/

  • GitHub Actions - makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub

    https://github.com/features/actions

Bootstrap lerna environment

npx lerna bootstrap

It will generate Prisma Client automatically with npm postinstall script. When you modify your schema and want to test locally, then follow steps in Build Prisma Client library

Run project locally with MongoDB in Docker Compose

You will have a complete local development experience with debug and live reload using MongoDB in Docker and Prisma Client in serverless offline mode.

Start MongoDB with replica

docker-compose up -d

Run whole project in serverless offline mode with live-reload for all packages

npx lerna run --parallel start-dev

or inside packages/api

npm run start-dev

GraphQL Playground: http://localhost:3000/dev/graphql

Build Prisma Client library

With Lerna (Manually):

npx lerna run build-lib

Or from inside packages/prisma-client package you can use it directly:

npm run build-lib

or run in live-reload mode (Automatic):

npx lerna run start-dev

start-dev - you can run this script within some specific package, if you want live reload only for this one.

Run sample code

npx ts-node index.ts

Prisma Studio

start-dev script starting it Automatically, if you want to run it manually, then Inside packages/prisma-client directory run this:

npx prisma studio

MongoDB Cloud Shared (M0 clusters) configuration

You will have to add the infamous 0.0.0.0/0 CIDR block to your MongoDB Atlas cluster IP Whitelist because you won’t know which IP address AWS Lambda is using to make calls to your Atlas database.

Deploy to AWS Lambda

I'm using GitHub Actions as CI/CD tool to deploy using Serverless Framework to AWS Lambda. But you can easily adjust it for any other CI/CD tool you prefer. Checkout .github/workflows/node.js.yml for details.

It requires secrets to be added to project Actions configuration (Repository / Settings / Secrets / Actions):

  • PROD_AWS_ACCESS_KEY_ID

  • PROD_AWS_SECRET_ACCESS_KEY

    AWS security keys. Read here

  • PROD_DATABASE_URL

    MongoDB connection string. Get it from MongoDB Cloud admin (you can use any plan)

Warning Execute deploy commands below only if you know what you are doing and want to deploy it manually (temporary staging environment, for example, or demo purposes), then crete .env.local and override DATABASE_URL with your own and run this:

npx lerna deploy

or inside packages/prisma-client

npx serverless deploy

Connect project to Serverless Dashboard

npx serverless

🤘💪🤣😍❤