# mohamedhabibfrigui/ecommerce-microservices architecture

> Architecture diagram of the GitHub repository mohamedhabibfrigui/ecommerce-microservices, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohamedhabibfrigui/ecommerce-microservices
- Repository: https://github.com/mohamedhabibfrigui/ecommerce-microservices

## Overview

##### Project Type & Purpose
- This is a Node.js-based, e-commerce microservices platform.
- It provides a GraphQL API Gateway for client interactions, three domain services (User, Product, Order) communicating internally over gRPC, event-driven messaging via Kafka, and data persistence in MongoDB.

##### File Structure Analysis
- Top-level directories:
- api-gateway/ – GraphQL server (Apollo, Express) with middleware (auth, rate-limit), routes, resolvers, schema, server entrypoint.
- user-service/, product-service/, order-service/ – Each contains a `.proto` file, a service entrypoint (e.g. `userMicroservice.js`), and a `models/` folder with Mongoose schemas.
- Supplementary files:
- `.gitignore`, `package.json` at root.
- `docs/` with images for Postman/Apollo.

##### Key README Insights
Client → (REST/GraphQL) → API Gateway → (gRPC) → Services → (MongoDB / Kafka)
- Stack highlights: Apollo Server, Express, gRPC, MongoDB, Kafka, JWT authentication, rate limiting.
- Emphasizes loose coupling, independent deployments, event-driven integration.

##### Main Components & Interactions
a. Components:
- Clients (web/mobile)
- API Gateway (Apollo GraphQL + Express)
- User Service, Product Service, Order Service (gRPC servers)
- MongoDB instances (one DB per service)
- Kafka cluster for events
b. Relationships:
- Clients send REST/GraphQL requests to API Gateway.
- Gateway authenticates (JWT), rate-limits, then dispatches gRPC calls to microservices.
- Services read/write to their MongoDB databases.
- Order Service publishes events to Kafka; User/Product Services subscribe for inventory updates, notifications, etc.
- Responses flow back along the same path to the client.

##### Architectural Patterns & Design Principles
- Microservices: each bounded context in its own process with its own datastore.
- API Gateway pattern: single entrypoint, hides service complexity, aggregates data.
- Event-driven via Kafka: loose coupling for cross-service workflows.
- gRPC for high-performance, strongly typed internal RPC.
- JWT for stateless auth, rate limiting for security.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Client["Clients\n(Web & Mobile)"]:::client

    %% API Gateway
    subgraph "API Gateway (Node.js)" 
        direction TB
        Server["server.js\n(Apollo & Express Entrypoint)"]:::gateway
        Auth["auth.js\n(JWT Auth Middleware)"]:::middleware
        RateLimit["rateLimit.js\n(Rate Limiting)"]:::middleware
        Schema["schema.js\n(GraphQL Schema)"]:::schema
        ResolversIndex["index.js\n(Resolvers Index)"]:::resolvers
        UserRes["userResolvers.js"]:::resolvers
        ProductRes["productResolvers.js"]:::resolvers
        OrderRes["orderResolvers.js"]:::resolvers
        RoutesUser["userRoutes.js"]:::routes
        RoutesProd["productRoutes.js"]:::routes
        RoutesOrder["orderRoutes.js"]:::routes
    end

    %% Services
    subgraph "User Service" 
        direction TB
        UMicro["userMicroservice.js\n(gRPC Server)"]:::service
        UProto["user.proto\n(Contract)"]:::contract
        UModel["User.js\n(Mongoose Model)"]:::model
    end

    subgraph "Product Service" 
        direction TB
        PMicro["productMicroservice.js\n(gRPC Server)"]:::service
        PProto["product.proto\n(Contract)"]:::contract
        PModel["Product.js\n(Mongoose Model)"]:::model
    end

    subgraph "Order Service" 
        direction TB
        OMicro["orderMicroservice.js\n(gRPC Server)"]:::service
        OProto["order.proto\n(Contract)"]:::contract
        OModel["Order.js\n(Mongoose Model)"]:::model
    end

    %% Databases
    UserDB["UserDB\n(MongoDB)"]:::db
    ProductDB["ProductDB\n(MongoDB)"]:::db
    OrderDB["OrderDB\n(MongoDB)"]:::db

    %% Kafka Broker
    Kafka["Kafka Broker"]:::broker

    %% Connections
    Client -->|"REST/GraphQL"| Server
    Server --> Auth
    Auth --> RateLimit
    RateLimit --> Schema
    RateLimit --> ResolversIndex
    RateLimit --> RoutesUser
    RateLimit --> RoutesProd
    RateLimit --> RoutesOrder

    Schema --> ResolversIndex
    ResolversIndex --> UserRes
    ResolversIndex --> ProductRes
    ResolversIndex --> OrderRes

    RoutesUser --> UserRes
    RoutesProd --> ProductRes
    RoutesOrder --> OrderRes

    ResolversIndex -->|"gRPC"| UMicro
    ResolversIndex -->|"gRPC"| PMicro
    ResolversIndex -->|"gRPC"| OMicro

    UMicro --> UModel
    PMicro --> PModel
    OMicro --> OModel

    UModel --> UserDB
    PModel --> ProductDB
    OModel --> OrderDB

    OMicro -->|order.created| Kafka
    Kafka -.->|order.created| UMicro
    Kafka -.->|order.created| PMicro

    %% Return paths
    UMicro -->|gRPC Response| ResolversIndex
    PMicro -->|gRPC Response| ResolversIndex
    OMicro -->|gRPC Response| ResolversIndex
    ResolversIndex -->|GraphQL Response| Server
    Server -->|HTTP Response| Client

    %% Click Events
    click Server "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/server.js"
    click Auth "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/middleware/auth.js"
    click RateLimit "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/middleware/rateLimit.js"
    click Schema "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/schema.js"
    click ResolversIndex "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/resolvers/index.js"
    click UserRes "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/resolvers/userResolvers.js"
    click ProductRes "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/resolvers/productResolvers.js"
    click OrderRes "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/resolvers/orderResolvers.js"
    click RoutesUser "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/routes/userRoutes.js"
    click RoutesProd "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/routes/productRoutes.js"
    click RoutesOrder "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/api-gateway/routes/orderRoutes.js"
    click UMicro "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/user-service/userMicroservice.js"
    click UProto "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/user-service/user.proto"
    click UModel "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/user-service/models/User.js"
    click PMicro "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/product-service/productMicroservice.js"
    click PProto "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/product-service/product.proto"
    click PModel "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/product-service/models/Product.js"
    click OMicro "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/order-service/orderMicroservice.js"
    click OProto "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/order-service/order.proto"
    click OModel "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/order-service/models/Order.js"
    click RootPkg "https://github.com/mohamedhabibfrigui/ecommerce-microservices/blob/main/package.json"
    click Docs "https://github.com/mohamedhabibfrigui/ecommerce-microservices/tree/main/docs/images"

    %% Styles
    classDef client fill:#cccccc,stroke:#333,stroke-width:1px;
    classDef gateway fill:#a8e6cf,stroke:#2d7f5e,stroke-width:2px;
    classDef middleware fill:#dcedc1,stroke:#2d7f5e,stroke-width:1px;
    classDef schema fill:#dcedc1,stroke:#2d7f5e,stroke-width:1px;
    classDef resolvers fill:#dcedc1,stroke:#2d7f5e,stroke-width:1px;
    classDef routes fill:#dcedc1,stroke:#2d7f5e,stroke-width:1px;
    classDef service fill:#b3cde0,stroke:#034f84,stroke-width:2px;
    classDef contract fill:#cce0ff,stroke:#034f84,stroke-width:1px;
    classDef model fill:#cce0ff,stroke:#034f84,stroke-width:1px;
    classDef db fill:#ffd1a4,stroke:#cc7a00,stroke-width:2px,shape:cylinder;
    classDef broker fill:#e0bbE4,stroke:#6a0dad,stroke-width:2px,shape:cloud;
```
