This is an attempt to create a sample project that has following capabilities.
- Nest framework TypeScript starter repository.
- GraphQL Graphql for APIs.
- Serverless for deployment to AWS.
- Clean Architecture.
- Prisma for ORM.
- Jest for testing.
- [Environment Variables] - using dotenv.
This was built using Node 18.x, Serverles framework 3.0. So please make sure you have those setup.
npm install
# use docker to host postgres, make sure you dont have port 5432 already taken
# the below command uses postgres alphine (15.2)
docker compose up
CREATE DATABASE apibase;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Create a .env file and add a DATABASE_URL variable and set it to postgres database.
DATABASE_URL=postgresql://your_user_name:your_password@hostname:5432/apibase
NODE_ENV=DEV
IS_OFFLINE=TRUE
Make sure you run primsa migrations to create tables/populate the tables. You have to manually create the database. No script included for it.
# run npx generate
npx prisma generate
# development
$ npm run start
# watch mode
$ npm run start:dev
# SLS local mode
$ npm run start:offline:dev
Create a default AWS profile. Make sure you set your region in profile and match it to to serverless.yml file.
This repo reads environment variables from AWS Parameter Store. So it requires to use default profile. There is a bug in SLS reading parameter store using other profiles.
If you don't want to read from ssm then remove it from serverless.yml file.
SLS requires good amount of permissions to deploy depending upon what AWS features you are using.
Here is a blog on what permissions you might need. https://serverlessfirst.com/create-iam-deployer-roles-serverless-app/
npm run deploy:dev
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
After starting SLS locally go to http://localhost:3001/dev/graphql and test GraphQL calls.
query
{
books
{
id
title
}
}# Write your query or mutation here
This project uses GraphQL Code first approach. All the schema is defined in schema.gql file.
Edit the file to add new types, queries, or mutations. Implement those in proper handlers and try it.
Some things are not working.
- I used paths in ts.config to see if I can reference using names as described in ts.config but that didn't work.
- Using Apollo server / gateway.
- Schema first graphql approach.
- Original Author - Kamil Myśliwiec
- Git repo - (https://github.com/jeffersondossantosaguiar/graphql-base-api/tree/main)