Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

E-Commerce GraphQL API

A simple e-commerce backend built with Node.js, Express, PostgreSQL and GraphQL. Supports product and order management with JWT auth.


Database Setup

Make sure PostgreSQL is running, then run the SQL below to create the tables:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(100) UNIQUE NOT NULL,
    password VARCHAR(255) NOT NULL
);

CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    price DECIMAL(10, 2) NOT NULL
);

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id INT REFERENCES users(id) ON DELETE CASCADE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE order_items (
    order_id INT REFERENCES orders(id) ON DELETE CASCADE,
    product_id INT REFERENCES products(id) ON DELETE CASCADE,
    quantity INT NOT NULL DEFAULT 1,
    PRIMARY KEY (order_id, product_id)
);

Running the Project

  1. Install dependencies:
npm install
  1. Create a .env file:
PORT=4000
DATABASE_URL=postgresql://username:password@localhost:5432/your_db
JWT_SECRET=your_secret
  1. Start the server:
npm run dev

Go to http://localhost:4000/graphql to open the Apollo Sandbox and test everything.


GraphQL Examples

For mutations and order queries, add this to the Headers tab in Apollo Sandbox after logging in:

{ "Authorization": "Bearer YOUR_TOKEN" }

Signup & Login

mutation {
  signup(email: "user@test.com", password: "password123")
}
mutation {
  login(email: "user@test.com", password: "password123")
}

Products

query {
  products {
    id
    name
    description
    price
  }
}
query {
  product(id: "1") {
    name
    price
  }
}
mutation {
  createProduct(name: "Wireless Mouse", description: "Ergonomic 2.4GHz", price: 29.99) {
    id
    name
    price
  }
}
mutation {
  updateProduct(id: "1", price: 24.99) {
    id
    name
    price
  }
}
mutation {
  deleteProduct(id: "1")
}

Orders

mutation {
  createOrder(productIds: ["1", "2"]) {
    id
    created_at
  }
}
query {
  orders {
    id
    created_at
    items {
      quantity
      product {
        name
        price
      }
    }
  }
}
query {
  order(id: "1") {
    id
    created_at
    items {
      quantity
      product {
        name
        price
      }
    }
  }
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages