Skip to content

hasura/ddn-sample-app

Repository files navigation

Ecommerce Application Demo using Data Delivery Network

This demo provides a practical example of building an Ecommerce App using Hasura's Data Delivery Network (DDN) with a supergraph architecture. Please note that a new release for ther repo is out. Make sure to try the workflow again if you are a returning user.

Instructions

You may also use a pre-packed codespace that pre-installs all the above and clone this project by clicking on the following link.

Open in GitHub Codespaces

local development

  1. Start the engine and connector services
HASURA_DDN_PAT=$(ddn auth print-pat) docker compose -f docker-compose.hasura.yaml watch

Note: For local development, Hasura runs several services (engine, connectors, auth, etc.), which use the following ports: 3000, 4317, 4318, 8081, 8082, 8083, 8084, and 8085. Please ensure these ports are available. If not, modify the published ports in the Docker Compose files from this repository accordingly.

  1. start the TS function runtime

Open a new terminal (the previous script needs to be running to continou further) and execute the following

cd sales/connector/ts && npm i
npx dotenv -e .env.local -- npm run watch

This example supergraph is composed of four subgraphs - users, analytics, experience, and sales, each backed by one or more data connectors. These subgraphs integrate various data sources to provide a comprehensive Ecommerce solution as follows.

  1. To rebuild after any changes, you may run the following command to get the changes reflected on the GraphQL schema.
ddn supergraph build local --output-dir ./engine

Deploy to DDN Cloud

ddn project create

# 5:34PM INF Project "vast-buzzard-0000" created on Hasura DDN successfully
# +-------------+-----------------------------------------------------+
# | Name        | vast-buzzard-0000                                   |
# +-------------+-----------------------------------------------------+
# | Console URL | https://console.hasura.io/project/vast-buzzard-0000 |
# +-------------+-----------------------------------------------------+

This will create a new project and shows the project name output in the terminal

  • Setup Project Name and change it on .hasura/context.yaml . Cloning the repo will generate all confgiration files you need. All you need to change is the project name.
ddn context set project <Project Name>

# ddn context set project vast-buzzard-0000
# 5:36PM INF Key "project" set to "vast-buzzard-0000" in the context successfully
  • Execute the following commands to set up your subgraphs (copy paste them and run them one by one as it is)
ddn project subgraph create analytics
ddn project subgraph create sales
ddn project subgraph create experience
ddn project subgraph create users

Deploy each connectors

## deploy connectors from analytics subgraph
ddn connector build create --connector analytics/connector/clickhouse/connector.cloud.yaml --target-supergraph supergraph.cloud.yaml --target-connector-link clickhouse

## deploy connectors from experience subgraph
ddn connector build create --connector experience/connector/pg/connector.cloud.yaml --target-supergraph supergraph.cloud.yaml --target-connector-link pg
ddn connector build create --connector experience/connector/mongo/connector.cloud.yaml --target-supergraph supergraph.cloud.yaml --target-connector-link mongo

## deploy connectors from sales subgraph
ddn connector build create --connector sales/connector/pg/connector.cloud.yaml --target-supergraph supergraph.cloud.yaml --target-connector-link pg
ddn connector build create --connector sales/connector/ts/connector.cloud.yaml --target-supergraph supergraph.cloud.yaml --target-connector-link ts --log-level DEBUG

## deploy connectors from users subgraph
ddn connector build create --connector users/connector/user_pg/connector.cloud.yaml --target-supergraph supergraph.cloud.yaml --target-connector-link user_pg

Deploy supergraph

ddn supergraph build create --supergraph supergraph.cloud.yaml

Note: Once deployed to your new project, if you don't have any connector changes, you can only rebuild supergraph alone, ie. connectors are only need to be deployed when there is change (data schema changes, functions etc.)

  • go to console and test using GraphQL API queries from the Composability folder.
    • For AuthZ: Set x-hasura-role = customer and x-hasura-user-id = some_user_id and run the AuthZ query

This example supergraph is composed of four subgraphs - users, analytics, experience, and sales, each backed by one or more data connectors. These subgraphs integrate various data sources to provide a comprehensive Ecommerce solution as follows.

alt text

Subgraphs and Data Sources

  • Subgraph: users

    • Data Connector: postgres
      • Models: Users, Notifications, Reviews
  • Subgraph: experience

    • Data Connector: postgres
      • Models: Cart, CartItems, Categories, Manufacturers, Products, ProductVectorDistance
    • Data Connector: mongoDB
      • Models: ProductDetails
  • Subgraph: sales

    • Data Connector: postgres
      • Models: Coupons, Orders
    • Typescript Functions
      • Commands: ToCurrencyString, ToDateString
  • Subgraph: analytics

    • Data Connector: clickhouse
      • Models: BrowsingHistory, RecentlyViewedProducts, SessionHistory

alt text

Disclaimer

This repository has credentials for databases, which are intentionally published by Hasura for demo purposes. These credentials allows for read-only access to synthetic datasets, which do not contain any user or customer sensitive data.

Core Concepts

The following section outlines the core concepts of Hasura DDN, providing a deeper understanding of its architecture and functionality.

Subgraph

For a multi-team organization working on a Hasura project, it can make sense for any one team to not have access to all metadata objects. Subgraphs introduces the notion of a module system for your Hasura metadata. Think of it is as an independent domain consisting of one or more data sources. Read More

Models

Models are the link between your data connectors and the API Hasura generates. A model may be backed by a database table, an ad-hoc SQL query, a pre-materialized view, a custom REST or GraphQL API server, etc. Read More

Commands

Commands are backed by functions or procedures declared in a DataConnectorLink allowing you to execute business logic directly from your GraphQL API. You can use them to validate, process or enrich data, call another API, or even log a user in.

Read More

Build Process

A build is a fully-functional, immutable supergraph API which is built based on your project's configuration.

During the build process, Hasura builds and deploys all the data connectors and supergraph builds. This includes connector configurations, models, functions, and all other related components, which are integrated into the deployments.

Considering the size of the supergraph and separate deployments, it may initially take some time to complete. Once deployed, the supergraph provides a unified GraphQL API that leverages the capabilities of all subgraphs to offer a comprehensive Ecommerce solution.