Skip to content

An example of GraphQL implementation of a shopping cart with NodeJS + Express + Knex

License

Notifications You must be signed in to change notification settings

samhoooo/express-graphql-shopping-cart

Repository files navigation

Shopping Cart with Express GraphQL Server

NodeJS GraphQL Postgres

An example of a simple GraphQL server with NodeJS, express-graphql and knex with PostgreSQL, which is serving GraphQL API for e-commerce website featuring:

  1. Get product details
  2. Get cart details
  3. Add / update / remove items from cart

Prerequisites

  1. Install Node.js version 16 or above
  2. Running PostgreSQL on port 5432
  3. Set up .env file with database information, please refer to the format of .env.default

Installation and Running

  1. Run yarn install
  2. Make sure you have PostgreSQL running on port 5432. You can download PostgreSQL from here
  3. Set up .env file with database host, port, name, user and password. Please refer to .env.default for exact format.
  4. Run yarn setDatabase to set up schema on database
  5. Run yarn seedDatabase to fill in sample data to database
  6. Run yarn build
  7. Run yarn start to start the application

Navigate to http://localhost:5000/graphql to see the GraphQL introspection.

Available Queries and Mutations

Queries

  • cartById(id: Int): Cart
  • productById(id: Int): Product
  • productByName(name: String): Product

Mutations

  • addProductToCart(productId: Int, cartId: Int): Cart
  • removeProductFromCart(productId: Int, cartId: Int): Cart
  • UpdateProductQuantityToCart(productId: Int, cartId: Int, quantity: Int): Cart

Schema Design

image

Generated by GraphQL Voyager