A GraphQL-based API built with NestJS, TypeORM, and PostgreSQL, featuring user authentication with JWT tokens.
- GraphQL API with Apollo Server
- User Authentication (Sign-up & Sign-in)
- JWT Token-based Authentication
- PostgreSQL Database with TypeORM
- TypeScript for type safety
- Password Hashing with bcryptjs
- Input Validation with class-validator
- Node.js v22+
- PostgreSQL 18+
- npm or yarn
npm install-
Set up environment variables
cp .env.example .env
Update
.envwith your PostgreSQL credentials:DATABASE_HOST=localhost DATABASE_PORT=5432 DATABASE_USER=postgres DATABASE_PASSWORD=your_password DATABASE_NAME=task_api_db JWT_SECRET=your_jwt_secret_key -
Create PostgreSQL database
psql -U postgres createdb task_api_db
If psql command not found: https://stackoverflow.com/a/79875872/7455192
-
Check connection to PostgreSQL
nc -vz <hostname> <port_number>
for example, in our case:
nc -vz 127.0.0.1 5432
-
Also you can check if <db_name> exists
This method lists all databases and pipes the output to grep to find your specific database name.
psql -l | grep -qw your_db_name && echo "Database 'your_db_name' exists." || echo "Database 'your_db_name' does not exist."in dev mode: ```bash psql -U postgres -l | grep -qw task_api_db && echo "Database 'task_api_db' exists." || echo "Database 'task_api_db' does not exist."
#### Method 2: Using pg_isready (For connection status)
This utility checks if the server is accepting connections for a specific database.
```bash
pg_isready -d your_db_name -q && echo "Server ready for 'your_db_name'." || echo "Server not ready or DB not found."
in dev mode:
pg_isready -d task_api_db -q && echo "Server ready for 'task_api_db'." || echo "Server not ready or DB not found."- Use PostgreSQL from terminal to create and setup a new db user
# connect to postgreSql command line tool
sudo -i -u postgres
# create a new user in db
createuser --interactive --pwprompt <username>ALTER USER <username> PASSWORD 'mynewpassword@#$@#sfsSDF';-
pg_hba.conf
sudo find / -name pg_hba.conf
cat /opt/homebrew/var/postgresql@14/pg_hba.conf
sudo cat /Library/PostgreSQL/18/data/pg_hba.conf
open /Library/PostgreSQL//uninstall-postgresql.app open /Library/PostgreSQL/18/uninstall-postgresql.app
sudo /Library/PostgreSQL//uninstall-postgresql.app/Contents/MacOS/installbuilder.sh sudo /Library/PostgreSQL/18/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
# development
npm run start
# watch mode
npm run start:dev
# production mode
npm run build
npm run start:prodThe GraphQL playground is available at http://localhost:5001/graphql
Sign Up:
mutation {
signUp(input: {
email: "user@example.com"
password: "password123"
firstName: "John"
lastName: "Doe"
}) {
accessToken
id
email
firstName
lastName
}
}Sign In:
mutation {
signIn(input: {
email: "user@example.com"
password: "password123"
}) {
accessToken
id
email
firstName
lastName
}
}Get User (after Sign In):
query {
user {
id
email
firstName
lastName
createdAt
updatedAt
}
}Note: The user query requires JWT authentication. Include the accessToken from the Sign In response in the Authorization header:
Authorization: Bearer <accessToken>
Get User by ID:
query {
getUserById(id: "user-uuid-here") {
id
email
firstName
lastName
createdAt
updatedAt
}
}
query {
getUserById(id: "af23-4234-e0a9-8b3b4-af23-4234-e0a9-8b3b4") {
id
email
firstName
lastName
createdAt
updatedAt
}
}Note: The getUserById query does not require authentication.
# unit tests
npm run test
# e2e tests
npm run test:e2e
# test coverage
npm run test:covsrc/
├── auth/ # Authentication module
│ ├── strategies/ # JWT strategy
│ ├── dto/ # Auth DTOs
│ ├── auth.service.ts
│ ├── auth.resolver.ts
│ └── auth.module.ts
├── users/ # Users module
│ ├── entities/ # User entity
│ ├── users.service.ts
│ ├── users.resolver.ts
│ └── users.module.ts
├── config/ # Configuration
│ └── database.config.ts
├── app.module.ts # Main app module
└── main.ts # Entry point
Deployment configuration depends on your target environment (AWS, Heroku, Docker, etc.).
MIT licensed