Skip to content

FredericoSFerreira/node-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node GraphQL API

graphql logonodejs logo

A GraphQL API for user management with authentication built with Node.js, Apollo Server, and MongoDB.

Technologies Used

  • Node.js
  • GraphQL (Apollo Server)
  • MongoDB (Mongoose)
  • JWT Authentication
  • Docker (for MongoDB)

Features

  • User CRUD operations
  • JWT-based authentication
  • GraphQL API

Prerequisites

  • Node.js (v14 or higher)
  • Docker and Docker Compose (for running MongoDB)
  • npm or yarn

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd node-graphql
  2. Install dependencies:

    npm install
    # or
    yarn install
  3. Set up environment variables:

    • Create a .env file 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
      

Running the Application

Start MongoDB using Docker

docker-compose up -d

This will start a MongoDB instance on port 27017.

Start the Application

Development mode with auto-reload:

npm run dev
# or
yarn dev

Production mode:

npm start
# or
yarn start

The GraphQL server will be available at: http://localhost:4000

API Documentation

Authentication

The API uses JWT for authentication. To authenticate:

  1. Create a user or login to get a token
  2. Include the token in the Authorization header for protected requests:
    Authorization: Bearer <your_token>
    

GraphQL Operations

Queries

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
  }
}

Mutations

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")
}

Testing the API

You can use tools like:

Project Structure

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors