A GraphQL API for user management with authentication built with Node.js, Apollo Server, and MongoDB.
- Node.js
- GraphQL (Apollo Server)
- MongoDB (Mongoose)
- JWT Authentication
- Docker (for MongoDB)
- User CRUD operations
- JWT-based authentication
- GraphQL API
- Node.js (v14 or higher)
- Docker and Docker Compose (for running MongoDB)
- npm or yarn
-
Clone the repository:
git clone <repository-url> cd node-graphql
-
Install dependencies:
npm install # or yarn install -
Set up environment variables:
- Create a
.envfile in the root directory based on.env.sample - Fill in the required values:
JWT_SECRET=your_jwt_secret_key MONGO_DB_URI=mongodb://localhost:27017/node-graphql
- Create a
docker-compose up -dThis will start a MongoDB instance on port 27017.
Development mode with auto-reload:
npm run dev
# or
yarn devProduction mode:
npm start
# or
yarn startThe GraphQL server will be available at: http://localhost:4000
The API uses JWT for authentication. To authenticate:
- Create a user or login to get a token
- Include the token in the Authorization header for protected requests:
Authorization: Bearer <your_token>
Get All Users (Requires Authentication)
query {
users {
id
name
email
}
}Get User by ID
query {
user(id: "user_id_here") {
id
name
email
}
}Get Current User (Requires Authentication)
query {
me {
id
name
email
}
}Create User
mutation {
createUser(
name: "John Doe"
email: "john@example.com"
password: "password123"
) {
token
user {
id
name
email
}
}
}Login
mutation {
login(
email: "john@example.com"
password: "password123"
) {
token
user {
id
name
email
}
}
}Update User (Requires Authentication)
mutation {
updateUser(
id: "user_id_here"
name: "John Updated"
email: "john.updated@example.com"
) {
id
name
email
}
}Delete User
mutation {
deleteUser(id: "user_id_here")
}You can use tools like:
- Apollo Studio Explorer (available at http://localhost:4000)
- Postman
- Insomnia
- GraphQL Playground
node-graphql/
├── auth/
│ └── auth.js # Authentication utilities
├── models/
│ └── user-model.js # User Mongoose model
├── schema/
│ └── user-schema.js # GraphQL schema and resolvers
├── .env # Environment variables (create from .env.sample)
├── .env.sample # Sample environment variables
├── docker-compose.yml # Docker configuration for MongoDB
├── index.js # Application entry point
└── package.json # Project dependencies and scripts