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
for example, in our case:
nc -vz <hostname> <port_number>
nc -vz 127.0.0.1 5432
# 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:5002/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