Get Started with Hasura DDN and SQL Server
Overview
This tutorial takes about twenty minutes to complete. You'll learn how to:
- Set up a new Hasura DDN project
- Connect it to a SQL Server database
- Generate Hasura metadata
- Create a build
- Run your first query
- Create relationships
Additionally, we'll familiarize you with the steps and workflows necessary to iterate on your API.
This tutorial assumes you're starting from scratch but you can easily follow the steps if you already have data seeded. Hasura will never modify your source schema.
Prerequisites
Install the DDN CLI
To use this guide, ensure you've installed/updated your CLI to at least v2.28.0
.
- macOS and Linux
- Windows
Simply run the installer script in your terminal:
curl -L https://graphql-engine-cdn.hasura.io/ddn/cli/v4/get.sh | bash
Currently, the CLI does not support installation on ARM-based Linux systems.
- Download the latest DDN CLI installer for Windows.
- Run the
DDN_CLI_Setup.exe
installer file and follow the instructions. This will only take a minute. - By default, the DDN CLI is installed under
C:\Users\{Username}\AppData\Local\Programs\DDN_CLI
- The DDN CLI is added to your
%PATH%
environment variable so that you can use theddn
command from your terminal.
Install Docker
The Docker-based workflow helps you iterate and develop locally without deploying any changes to Hasura DDN, making the
development experience faster and your feedback loops shorter. You'll need Docker Compose v2.20
or later.
Validate the installation
You can verify that the DDN CLI is installed correctly by running:
ddn doctor
Tutorial
Step 1. Authenticate your CLI
ddn auth login
This will launch a browser window prompting you to log in or sign up for Hasura DDN. After you log in, the CLI will acknowledge your login, giving you access to Hasura Cloud resources.
Step 2. Scaffold out a new local project
ddn supergraph init my-project && cd my-project
Once you move into this directory, you'll see your project scaffolded out for you. You can view the structure by either
running ls
in your terminal, or by opening the directory in your preferred editor.
Step 3. Start a local SQL Server container
docker pull mcr.microsoft.com/mssql/server:2022-latest
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Password123" -p 1433:1433 --name sqlserver -d mcr.microsoft.com/mssql/server:2022-latest
Step 4. Connect and create a table
docker exec -it sqlserver /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P Password123 -C -N
CREATE TABLE users (user_id int primary key, name varchar(255), age int);
GO
INSERT INTO users (user_id, name, age) VALUES (1, 'Alice', 25), (2, 'Bob', 30), (3, 'Charlie', 35);
GO
SELECT * FROM users;
GO
You should see a list of users returned.
Step 4. Initialize your SQL Server connector
ddn connector init my_sqlserver -i
From the dropdown, start typing sqlserver
and hit enter to accept the default port. Then, provide the following
values:
Connection string
The connection string format will be in the format
Server=<hostname>,<port>;Database=<database>;Uid=<username>;Password=<password>
and so is as follows:
Server=local.hasura.dev,1433;Database=master;Uid=sa;Password=Password123;TrustServerCertificate=true
TrustServerCertificate=true
should only be added for local databases.
Step 5. Introspect your SQL Server database
ddn connector introspect my_sqlserver
After running this, you should see a representation of your database's schema in the
app/connector/my_sqlserver/configuration.json
file; you can view this using cat
or open the file in your editor.
ddn connector show-resources my_sqlserver
Step 6. Add your model
ddn model add my_sqlserver users
Open the app/metadata
directory and you'll find a newly-generated file: Users.hml
. The DDN CLI will use this Hasura
Metadata Language file to represent the users
table from SQL Server in your API as a
model.
To track all available models in the dababase, run:
ddn model add my_sqlserver "*"
Step 7. Create a new supergraph build
ddn supergraph build local
The build is stored as a set of JSON files in engine/build
.
Step 8. Start your local services
Use the docker-start
script in the Hasura context to start your local services.
ddn run docker-start
Your terminal will be taken over by logs for the different services.
Step 9. Run your first query
ddn console --local
query {
users {
userId
name
age
}
}
{
"data": {
"users": [
{
"userId": 1,
"name": "Alice",
"age": 25
},
{
"userId": 2,
"name": "Bob",
"age": 30
},
{
"userId": 3,
"name": "Charlie",
"age": 35
}
]
}
}
Step 10. Iterate on your SQL Server schema
CREATE TABLE posts (user_id int, post_id int primary key, title varchar(255), content varchar(255), FOREIGN KEY (user_id) REFERENCES users(user_id));
GO
INSERT INTO posts (user_id, post_id, title, content) VALUES (1, 1, 'My First Post', 'This is Alice''s first post.'), (1, 2, 'Another Post', 'Alice writes again!'), (2, 3, 'Bob''s Post', 'Bob shares his thoughts.'), (3, 4, 'Hello World', 'Charlie joins the conversation.');
GO
SELECT posts.post_id, posts.title, posts.content, users.name AS author FROM posts JOIN users ON posts.user_id = users.user_id;
Step 11. Refresh your metadata and rebuild your project
The following steps are necessary each time you make changes to your source schema. This includes, adding, modifying, or dropping tables.
Step 11.1. Re-introspect your data source
ddn connector introspect my_sqlserver
In app/connector/my_sqlserver/configuration.json
, you'll see schema updated to include operations for the posts
table. In app/metadata/my_sqlserver.hml
, you'll see posts
present in the metadata as well.
Step 11.2. Update your metadata
ddn model add my_sqlserver posts
Step 11.3. Kill your services
Bring down the services by pressing CTRL+C
in the terminal tab logging their activity.
Step 11.4. Create a new build
ddn supergraph build local
Step 11.5 Restart your services
ddn run docker-start
Step 12. Query your new build
query GetPosts {
posts {
userId
postId
title
content
}
}
{
"data": {
"posts": [
{
"userId": 1,
"postId": 1,
"title": "My First Post",
"content": "This is Alice's first post."
},
{
"userId": 1,
"postId": 2,
"title": "Another Post",
"content": "Alice writes again!"
},
{
"userId": 2,
"postId": 3,
"title": "Bob's Post",
"content": "Bob shares his thoughts."
},
{
"userId": 3,
"postId": 4,
"title": "Hello World",
"content": "Charlie joins the conversation."
}
]
}
}
Step 13. Create a relationship
ddn relationship add my_sqlserver posts
You'll see a new metadata object added to the app/metadata/posts.hml
file of kind Relationship
explaining the
relationship between posts
and users
.
Step 14. Rebuild your project
Bring down the services by pressing CTRL+C
in the terminal tab logging their activity.
ddn supergraph build local
ddn run docker-start
Step 15. Query using your relationship
query GetPosts {
posts {
postId
title
content
user {
userId
name
age
}
}
}
{
"data": {
"posts": [
{
"postId": 1,
"title": "My First Post",
"content": "This is Alice's first post.",
"user": {
"userId": 1,
"name": "Alice",
"age": 25
}
},
{
"postId": 2,
"title": "Another Post",
"content": "Alice writes again!",
"user": {
"userId": 1,
"name": "Alice",
"age": 25
}
},
{
"postId": 3,
"title": "Bob's Post",
"content": "Bob shares his thoughts.",
"user": {
"userId": 2,
"name": "Bob",
"age": 30
}
},
{
"postId": 4,
"title": "Hello World",
"content": "Charlie joins the conversation.",
"user": {
"userId": 3,
"name": "Charlie",
"age": 35
}
}
]
}
}
Next steps
Congratulations on completing your first Hasura DDN project with SQL Server! 🎉
Here's what you just accomplished:
- You started with a fresh project and connected it to a local SQL Server database.
- You set up metadata to represent your tables and relationships, which acts as the blueprint for your API.
- Then, you created a build — essentially compiling everything into a ready-to-use API — and successfully ran your first GraphQL queries to fetch data.
- Along the way, you learned how to iterate on your schema and refresh your metadata to reflect changes.
Now, you're equipped to connect and expose your data, empowering you to iterate and scale with confidence. Great work!
Take a look at our SQL Server docs to learn more about how to use Hasura DDN with SQL Server. Or, if you're ready, get started with adding permissions to control access to your API.