# kinngh/shopify-nextjs-prisma-app architecture

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

- Interactive diagram: https://gitdiagram.com/kinngh/shopify-nextjs-prisma-app
- Repository: https://github.com/kinngh/shopify-nextjs-prisma-app

## Overview

##### Main components
- Frontend: Next.js pages & React components (embedded app UI using Polaris & App Bridge).
- Backend: Next.js API Routes (REST & GraphQL endpoints, proxy, checkout, GDPR, webhooks).
- Middleware & Utils: HMAC/jwt verification, session handling, Shopify client provider.
- Database: Prisma ORM connected to a MySQL-compatible store (e.g., PlanetScale).
- External Services: Shopify Admin API (GraphQL), Shopify Webhooks, App Bridge CDN.

##### Relationships & data flow
- The browser UI calls serverless API routes.
- API Routes invoke middleware stacks, then handlers in `utils/` and `pages/api/*`.
- Handlers talk to Shopify Admin API or to Prisma for persistence.
- Webhook route receives events directly from Shopify and routes them to handler functions.
- Prisma translates queries to the underlying PlanetScale/MySQL database.

##### Architectural patterns & principles
- Serverless functions (Next.js API Routes) with per-route middleware.
- Separation of concerns: UI vs. API vs. data access.
- Use of providers (AppBridgeProvider, clientProvider) for dependency injection.
- Modular utils for cryptography, session, webhooks, and Shopify integration.

## Diagram (Mermaid)

```mermaid
flowchart TB
  %% Frontend Layer
  subgraph "Frontend" 
    direction TB
    Pages["Next.js Pages<br/>(pages/)"]:::ui
    Components["React Components<br/>(components/)"]:::ui
    AppBridge["AppBridgeProvider"]:::ui
    Pages -->|renders UI| Components
    Components -->|wraps in| AppBridge
    AppBridge -->|injects session & App Bridge| APIIndex
  end

  %% Backend / API Layer
  subgraph "API Routes & Middleware"
    direction TB
    APIIndex["API: /api/index.js"]:::logic
    GraphQL["API: /api/graphql.js"]:::logic
    Proxy["API: /api/proxy_route/json.js"]:::logic
    Checkout["API: /api/checkout/index.js"]:::logic
    GDPR1["API: /api/gdpr/customers_data_request.js"]:::logic
    GDPR2["API: /api/gdpr/customers_redact.js"]:::logic
    GDPR3["API: /api/gdpr/shop_redact.js"]:::logic
    WebhookEP["API: /api/webhooks/[...webhookTopic].js"]:::logic
    Apps["API: /api/apps/index.js"]:::logic
    AppsDebug["API: /api/apps/debug/*.js"]:::logic

    subgraph "Middleware Stack"
      direction TB
      VerifyHmac["verifyHmac"]:::logic
      VerifyReq["verifyRequest"]:::logic
      VerifyProxy["verifyProxy"]:::logic
      VerifyCheckout["verifyCheckout"]:::logic
      IsInitialLoad["isInitialLoad"]:::logic
      WithMiddleware["withMiddleware"]:::logic
      VerifyHmac --> VerifyReq --> VerifyProxy --> VerifyCheckout --> IsInitialLoad --> WithMiddleware
    end

    APIIndex --> WithMiddleware --> HandlerGroup
    GraphQL --> WithMiddleware --> HandlerGroup
    Proxy --> WithMiddleware --> HandlerGroup
    Checkout --> WithMiddleware --> HandlerGroup
    GDPR1 --> WithMiddleware --> HandlerGroup
    GDPR2 --> WithMiddleware --> HandlerGroup
    GDPR3 --> WithMiddleware --> HandlerGroup
    WebhookEP --> WithMiddleware --> HandlerGroup
    Apps --> WithMiddleware --> HandlerGroup
    AppsDebug --> WithMiddleware --> HandlerGroup
  end

  %% Handler Utilities
  subgraph "Handlers & Utils"
    direction TB
    Session["sessionHandler"]:::logic
    JWT["validateJWT"]:::logic
    ClientProv["clientProvider"]:::logic
    ShopifyUtil["shopify.js"]:::logic
    Crypt["cryption.js"]:::logic
    WebhookHandler["app_uninstalled.js"]:::logic
    Fresh["freshInstall"]:::logic
    Setup["setupCheck"]:::logic
    HandlerGroup[/"Route Handlers"/]:::logic
    HandlerGroup --> Session --> JWT --> ClientProv --> ShopifyUtil
    HandlerGroup --> Crypt
    HandlerGroup --> WebhookHandler
    HandlerGroup --> Fresh --> Setup
  end

  %% Database Layer
  subgraph "Database"
    direction TB
    PrismaClient["Prisma Client<br/>(utils/prisma.js)"]:::data
    Schema["schema.prisma"]:::data
    MySQL["PlanetScale MySQL"]:::data
    Session --> PrismaClient --> Schema --> MySQL
  end

  %% External Services
  subgraph "External"
    direction TB
    ShopifyAPI["Shopify Admin API<br/>(GraphQL)"]:::external
    WebhooksSrv["Shopify Webhooks"]:::external
    HandlerGroup -->|GraphQL| ShopifyAPI
    WebhookEP -->|Incoming POST| WebhooksSrv
    WebhookEP --> HandlerGroup
    AppBridge -->|App Bridge CDN| ShopifyAPI
  end

  %% Debug & Dev
  subgraph "Debug & Developer Tools"
    direction TB
    DebugPages["pages/debug/*.jsx"]:::ui
    DevTypes["_developer/types/"]:::logic
    WebhookWriter["_developer/webhookWriter.js"]:::logic
    TomlWriter["_developer/tomlWriter.js"]:::logic
  end

  %% Style Classes
  classDef ui fill:#D0E6FF,stroke:#003366,color:#003366
  classDef logic fill:#DFFFE0,stroke:#336600,color:#336600
  classDef data fill:#FFF5D0,stroke:#996600,color:#996600
  classDef external fill:#F3DFFF,stroke:#660066,color:#660066

  %% Click Events - Frontend
  click Pages "https://github.com/kinngh/shopify-nextjs-prisma-app/tree/main/pages/"
  click Components "https://github.com/kinngh/shopify-nextjs-prisma-app/tree/main/components/"
  click AppBridge "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/components/providers/AppBridgeProvider.jsx"

  %% Click Events - API Routes
  click APIIndex "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/index.js"
  click GraphQL "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/graphql.js"
  click Proxy "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/proxy_route/json.js"
  click Checkout "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/checkout/index.js"
  click GDPR1 "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/gdpr/customers_data_request.js"
  click GDPR2 "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/gdpr/customers_redact.js"
  click GDPR3 "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/gdpr/shop_redact.js"
  click WebhookEP "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/webhooks/[...webhookTopic].js"
  click Apps "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/pages/api/apps/index.js"
  click AppsDebug "https://github.com/kinngh/shopify-nextjs-prisma-app/tree/main/pages/api/apps/debug/"

  %% Click Events - Middleware
  click VerifyHmac "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/middleware/verifyHmac.js"
  click VerifyReq "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/middleware/verifyRequest.js"
  click VerifyProxy "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/middleware/verifyProxy.js"
  click VerifyCheckout "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/middleware/verifyCheckout.js"
  click IsInitialLoad "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/middleware/isInitialLoad.js"
  click WithMiddleware "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/middleware/withMiddleware.js"

  %% Click Events - Session & JWT
  click Session "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/sessionHandler.js"
  click JWT "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/validateJWT.js"

  %% Click Events - Shopify Client & Utilities
  click ClientProv "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/clientProvider.js"
  click ShopifyUtil "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/shopify.js"
  click Crypt "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/cryption.js"

  %% Click Events - Prisma
  click PrismaClient "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/prisma.js"
  click Schema "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/prisma/schema.prisma"

  %% Click Events - Webhook Handlers
  click WebhookHandler "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/webhooks/app_uninstalled.js"

  %% Click Events - Fresh Install & Setup
  click Fresh "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/freshInstall.js"
  click Setup "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/utils/setupCheck.js"

  %% Click Events - Debug & Developer Tools
  click DebugPages "https://github.com/kinngh/shopify-nextjs-prisma-app/tree/main/pages/debug/"
  click DevTypes "https://github.com/kinngh/shopify-nextjs-prisma-app/tree/main/_developer/types/"
  click WebhookWriter "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/_developer/webhookWriter.js"
  click TomlWriter "https://github.com/kinngh/shopify-nextjs-prisma-app/blob/main/_developer/tomlWriter.js"
```
