A modern REST and GraphQL API built with Microsoft Data API Builder (DAB) using the classic Northwind database. This project demonstrates how to create a containerized API with automatic Swagger documentation and GraphQL endpoint.
erDiagram
Categories ||--o{ Products : contains
Products }o--|| Suppliers : supplied_by
Products }o--o{ Orders : "ordered in"
Orders }|--|| Customers : placed_by
Orders ||--|{ OrderDetails : contains
Orders }o--|| Employees : processed_by
Employees ||--o{ Employees : reports_to
Territories }o--|| Region : in_region
Employees }o--o{ Territories : manages
Shippers ||--o{ Orders : ships
Categories {
int CategoryID PK
string CategoryName
string Description
blob Picture
}
Products {
int ProductID PK
string ProductName
int SupplierID FK
int CategoryID FK
string QuantityPerUnit
decimal UnitPrice
int UnitsInStock
int UnitsOnOrder
int ReorderLevel
boolean Discontinued
}
Suppliers {
int SupplierID PK
string CompanyName
string ContactName
string ContactTitle
string Address
string City
string Region
string PostalCode
string Country
string Phone
string Fax
string HomePage
}
Orders {
int OrderID PK
string CustomerID FK
int EmployeeID FK
date OrderDate
date RequiredDate
date ShippedDate
int ShipVia FK
decimal Freight
string ShipName
string ShipAddress
string ShipCity
string ShipRegion
string ShipPostalCode
string ShipCountry
}
Customers {
string CustomerID PK
string CompanyName
string ContactName
string ContactTitle
string Address
string City
string Region
string PostalCode
string Country
string Phone
string Fax
}
Employees {
int EmployeeID PK
string LastName
string FirstName
string Title
string TitleOfCourtesy
date BirthDate
date HireDate
string Address
string City
string Region
string PostalCode
string Country
string HomePhone
string Extension
blob Photo
text Notes
int ReportsTo FK
}
OrderDetails {
int OrderID PK,FK
int ProductID PK,FK
decimal UnitPrice
int Quantity
float Discount
}
Shippers {
int ShipperID PK
string CompanyName
string Phone
}
Territories {
string TerritoryID PK
string TerritoryDescription
int RegionID FK
}
Region {
int RegionID PK
string RegionDescription
}
- REST API endpoints for Northwind database entities
- GraphQL API with full query capabilities
- Swagger/OpenAPI documentation
- Docker containerization
- SQL Server database with Northwind sample data
- Cross-Origin Resource Sharing (CORS) enabled
- Anonymous access for easy testing
-
Clone the repository:
git clone https://github.com/vvidov/DabRestGraphQLBaseDemo.git cd DabRestGraphQLBaseDemo
-
Start the containers:
.\start.ps1
-
Access the APIs:
- Swagger UI: http://localhost:8080/swagger
- REST API: http://localhost:8080/api
- GraphQL Playground: http://localhost:8080/graphql
Before running the application, you need to set up your environment variables:
-
Copy the
.env.template
file to create a new.env
file:cp .env.template .env
-
Edit the
.env
file with your secure values:MSSQL_SA_PASSWORD
: Set a secure password for SQL Server SA accountMSSQL_PID
: SQL Server edition (default: Developer)CONNECTION_STRING
: Database connection string (update with your SA password)
- Never commit the
.env
file to version control - Use strong passwords in production
- The
.env
file is already added to.gitignore
The project uses Data API Builder, a powerful tool from Microsoft that automatically generates REST and GraphQL APIs from your database. Our DAB container is configured using:
- Base image:
mcr.microsoft.com/dotnet/sdk:8.0
- Configuration: Uses
dab-config.json
for API definitions - Exposed port: 8080
- Runs as a containerized service using the DAB CLI
-
API Support:
- REST endpoints (POST, GET, PUT, PATCH, DELETE)
- GraphQL endpoints with queries and mutations
- OpenAPI/Swagger support
-
Database Support:
- SQL Server and Azure SQL
- PostgreSQL and Azure Database for PostgreSQL
- MySQL and Azure Database for MySQL
- Azure Cosmos DB (NoSQL and PostgreSQL)
- Azure SQL Data Warehouse
-
Advanced Capabilities:
- Multiple simultaneous data sources
- Support for tables, views, and stored procedures
- Built-in filtering, sorting, and pagination
- In-memory caching
- Relationship navigation
- Dynamic schemas
-
Security Features:
- OAuth2/JWT authentication
- EasyAuth and Microsoft Entra Identity integration
- Role-based authorization
- Item-level security via policy expressions
-
Developer Experience:
- Zero-code configuration
- Cross-platform CLI
- Docker-friendly container
- Native OpenAPI support
- Automatic CRUD operations
To adapt this for your own database:
-
Keep the DAB container setup:
# dab/Dockerfile remains unchanged FROM mcr.microsoft.com/dotnet/sdk:8.0 RUN dotnet tool install -g Microsoft.DataApiBuilder
-
Update database connection in
dab-config.json
:{ "data-source": { "database-type": "mssql", // or postgresql, mysql, etc. "connection-string": "Server=db;Database=YourDatabase;..." } }
-
Define your entities:
{ "entities": { "YourEntity": { "source": "DatabaseTable", "rest": { "path": "/your-endpoint" }, "permissions": [ { "role": "anonymous", "actions": ["*"] } ] } } }
Benefits:
- Zero-code API development
- Automatic documentation
- Built-in GraphQL support
- Easy configuration
- Production-ready middleware
This repository provides a ready-to-use Data API Builder setup with SQL Server and Northwind database. You can use it as a base for your own projects to quickly get a REST and GraphQL API.
Follow these steps to use it in your project:
mkdir my-project
cd my-project
git init
git submodule add https://github.com/vvidov/DabRestReactDemo.git dab-base
Create a new file named .env
in the dab-base
directory with the following content:
MSSQL_SA_PASSWORD=YourStrongPassword123!
MSSQL_PID=Developer
CONNECTION_STRING=Server=db;Database=Northwind;User Id=sa;Password=YourStrongPassword123!;TrustServerCertificate=True
YourStrongPassword123!
with a secure password.
cd dab-base
docker-compose up -d --build
Once running, you'll have access to:
- SQL Server with Northwind database (port 14330)
- REST API endpoints (http://localhost:8080/api)
- GraphQL endpoint (http://localhost:8080/graphql)
- Swagger documentation (http://localhost:8080/swagger)
You can now build your frontend application using these APIs.
- Categories:
GET /api/categories
- Products:
GET /api/products
- Suppliers:
GET /api/suppliers
All endpoints support standard HTTP methods (GET, POST, PUT, DELETE) for CRUD operations.
Access the GraphQL playground at /graphql
to:
- Write and test queries
- Explore the schema
- View documentation
Access the GraphQL playground at http://localhost:8080/graphql. Here are some sample queries:
query {
products {
items {
ProductName
UnitPrice
UnitsInStock
category {
CategoryName
Description
}
supplier {
CompanyName
ContactName
Country
}
}
}
}
query {
orders {
items {
OrderID
OrderDate
customer {
CompanyName
ContactName
}
employee {
FirstName
LastName
}
shipper {
CompanyName
}
orderDetails {
items {
UnitPrice
Quantity
product {
ProductName
}
}
}
}
}
}
query {
categories {
items {
CategoryName
Description
products {
items {
ProductName
UnitPrice
UnitsInStock
}
}
}
}
}
query {
employees {
items {
EmployeeID
FirstName
LastName
Title
manager {
FirstName
LastName
}
directReports {
items {
FirstName
LastName
}
}
territories {
items {
TerritoryDescription
region {
RegionDescription
}
}
}
}
}
}
query {
products(first: 5, filter: {UnitPrice: {gt: 15}}) {
items {
ProductName
UnitPrice
category {
CategoryName
}
}
}
}
mutation {
createCategories(item: {
CategoryName: "Organic Foods"
Description: "Certified organic products"
}) {
CategoryID
CategoryName
Description
}
}
mutation {
updateProducts(ProductID: 1, item: {
UnitPrice: 20.00
UnitsInStock: 100
}) {
ProductID
ProductName
UnitPrice
UnitsInStock
}
}
These queries demonstrate:
- Nested relationships (products β category β products)
- Many-to-one relationships (products β supplier)
- One-to-many relationships (categories β products)
- Many-to-many relationships (employees β territories)
- Self-referential relationships (employee β manager/directReports)
- Filtering and pagination
- Basic mutations for data modification
Access the GraphQL playground to try these queries and explore the auto-generated documentation.
The project uses a Microsoft SQL Server container (mcr.microsoft.com/mssql/server:2019-latest
) that can be customized for any database:
- Base Image: SQL Server 2019
- Database: Northwind (sample database)
- Initialization: Automatic via
init.sql
script - Credentials: Configurable via environment variables
You can easily modify this setup for your own database:
-
Replace the initialization script:
# db/Dockerfile COPY init.sql /docker-entrypoint-initdb.d/
- Replace
init.sql
with your own database schema and data
- Replace
-
Update environment variables in
docker-compose.yml
:environment: - ACCEPT_EULA=Y - SA_PASSWORD=YourPassword - MSSQL_PID=Developer
-
Modify DAB configuration:
// dab/dab-config.json { "data-source": { "database-type": "mssql", "connection-string": "Server=db;Database=YourDatabase;..." } }
This containerized approach ensures:
- Consistent database setup across environments
- Easy database version control
- Portable development environment
- Quick setup for new team members
dabrestreactdemo/
βββ dab/ # Data API Builder configuration
β βββ Dockerfile # DAB API container setup
β βββ dab-config.json # DAB configuration
βββ db/ # Database setup
β βββ Dockerfile # SQL Server container setup
β βββ init.sql # Database initialization script
βββ docker-compose.yml # Container orchestration
βββ start.ps1 # Startup script
βββ README.md # This file
This demo uses anonymous authentication for simplicity. In a production environment, you should:
- Implement proper authentication
- Use HTTPS
- Restrict CORS settings
- Secure database credentials
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.