This demo combines both aspects from data-api-builder and static-web-app into one for a shorter demo.
-
Set up an
.envfile indata-api-builder:MSSQL="Server=localhost,1433;Database=database-combined;User Id=sa;Password=YourStrong@Passw0rd;trustServerCertificate=true" # development/production environment=development -
Run SQL Server by running
docker compose up -dincombined/dab. -
Create a database called
database-combinedand run thescript.sqlscript to set up data. -
Run
dotnet tool restoreto install the DAB CLI.- If this doesn't work well, try using
dotnet tool install Microsoft.DataApiBuilder -ginstead.
- If this doesn't work well, try using
-
Run
dab startincombined/dabto start the API. -
In another terminal, execute the following steps:
-
Install the React project dependencies by running
npm iincombined/react. -
Run
npm startincombined/reactto start the React app.
- Showcase the front-end and talk about having a React library application with books and authors, with DAB as the back-end and SQL Server as the database in Docker.
- Talk about the roles and permissions and the log-in button switching roles which are set in the header requests.
- Talk about DAB using the
Simulatorauthentication provider so we can easily test roles in development. We'll showcase this later and talk more about production grade authentication in the presentation as well. - First, show the request and response that's done to get the books and authors (REST and GraphQL)
- Try to delete a book and author when logged out and show the 403 error. Show the auth header in the request.
- Log in as admin and delete a book and author. Show the auth header in the request, now it works.
- Create a book called
C# in Depthby author3, categoryTechnologyand imageUrlhttps://i.pinimg.com/736x/ea/ba/ce/eabacee45f65cd08801c135756314cd5.jpgto show how that works. - Finally, look at the book details of the 4th author. Showcase the GraphQL query that retrieves the author + books.
- Summarize that all of this API functionality required 0 code, it was all done in the DAB configuration file, which we'll look at next.
- After this, go through the DAB Urls to showcase the Swagger, GaphQL and OpenAPI page
- Then showcase the code tour.
- Open
http://localhost:3000/for the React app. - Open
http://localhost:5000/swaggerto see the API documentation. - Open
http://localhost:5000/api/openapito see OpenAPI definition. - Open
http://localhost:5000/graphqlto see the GraphQL playground.
-
Set up an
.envfile indata-api-builder:MSSQL="Server=localhost,1433;Database=database;User Id=sa;Password=YourStrong@Passw0rd;trustServerCertificate=true" # development/production environment=development -
Run SQL Server by running
docker compose up -dindata-api-builder. -
Import the
.bacpacusing SQL Server Management Studio (or another tool). -
Run
dotnet tool restoreto install the DAB CLI.- If this doesn't work well, try using
dotnet tool install Microsoft.DataApiBuilder -ginstead.
- If this doesn't work well, try using
-
Run
dab startindata-api-builderto start the API.
- Showcase the API documentation and retrieve a list of books to let people get a feel for the API.
- Show the
author-books-countandGetAllCowrittenBooksByAuthorendpoints which are actually a view and store procedure! - Showcase the
api/openapiendpoint and/graphqlendpoint. - Showcase adding a book. We use a mix of REST and GraphQL purely for demo purposes!
- Add a series for "Red Rising" (and remove the
idin the request body){ "name": "Red Rising" } - Add an author for "Pierce Brown" (and remove the
idin the request body){ "first_name": "Pierce", "last_name": "Brown" } - Add a book and book author link using a GraphQL mutation:
mutation { createBook(item: { title: "Red Rising", year: 2014, pages: 382, series_id: 1000000 }) { id title } } ## Uncomment and execute this after executing the previous mutation # mutation { # createBookAuthor(item: { book_id: 1000002, author_id: 1000001, author_type_id: 1 }) { # book_id, # author_id # } # }
- Add a series for "Red Rising" (and remove the
- Go through the code tour in the
data-api-builder/dab-config.jsonfile.
- Open
http://localhost:5000/swaggerto see the API documentation. - Open
http://localhost:5000/api/openapito see OpenAPI definition. - Open
http://localhost:5000/graphqlto see the GraphQL playground.
-
Set up an
.envfile instatic-web-app/swa-db-connections:DATABASE_CONNECTION_STRING="Server=tcp:sql-data-api-builder-demo.database.windows.net,1433;Initial Catalog=db-data-api-builder-demo;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication='Active Directory Interactive'" -
Ensure the other demo's process has stopped before continuing.
-
Ensure that the database has whitelisted the IP address.
-
Run
npm i && npx swa start http://localhost:3000 --run "cd library-demo && npm i && npm start" --data-api-location swa-db-connectionsinstatic-web-app.
- Open the localhost version of the app and talk about it being a simple UI for another library app, built with React and running with the SWA cli.
- There's a github action that will push changes to the live version of the app.
- Showcase the CLI command and the
--data-api-locationflag. - Showcase the
swa-db-connectionsfolder and the config file. - Show that logged out users can read and things like delete will result in 403.
- Showcase that this is all done using data-api-builder under the hood. There is no API to be found in this project!
- When logging in, we can use the Azure Static Web App emulator's authentication to log in, making local development easier.
- Log in with an admin role.
- Now we can do things like deleting a book.
- Open the live version and showcase thats its running the project in the app, again, with data api builder!
- Now showcase the azure portal and the database connection setup.
- Open
http://localhost:4280for the localhost version of the app - Open
https://witty-smoke-062083f03.5.azurestaticapps.net/for the live version of the app. - Open
https://portal.azure.com/#@Sandertenbrinkexs4all.onmicrosoft.com/resource/subscriptions/65cbea20-04e6-49ae-869f-bcd6fb4d318f/resourceGroups/rg-data-api-builder-demo/providers/Microsoft.Web/staticSites/swa-data-api-builder-demo/databaseConnectionfor the Static Web App resource. - Open #1 for the PR that creates a temporary environment for SWA.
- Open
https://witty-smoke-062083f03-1.westeurope.5.azurestaticapps.net/for the PR environment (Not set up with DAB!)
This project lets you use an Azure Static Web Apps with cosmosdb_nosql.
-
You can run
npm installandnpm install -g @azure/static-web-apps-clito set up base packages. -
You'll also need to use the Azure CosmosDb emulator.
-
Create an
.envfile in/static-web-app-cosmosdbwith the following contents:AZURE_SUBSCRIPTION_ID=YOUR_SUBSCRIPTION_ID_HERE AZURE_TENANT_ID=YOUR_TENANT_ID_HERE -
Then create an
.envfile instatic-web-app-cosmosdb/swa-db-connectionswith the following contents:# The connection string for the CosmosDB emulator DATABASE_CONNECTION_STRING="AccountEndpoint=https://localhost:8081;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;" -
You can use the
ms-azuretools.vscode-cosmosdbextension to importstatic-web-app-cosmosdb/swa-db-connections/authors.jsoninto a database calledbookshelfwith a container calledauthors. -
Then run
swa startto run the project. Go tohttp://localhost:4280to see the list of authors. -
Then deploy a real instance of CosmosDb and import the data again. You might need to enable system assigned identities.
-
Then run
swa deploy --env Productionto deploy the project to Azure Static Web Apps. You will need to set up the database connection in the Azure portal and possibly redeploy again. -
Then go to the URL shown in the CLI to see the live version of the app, using Data API Builder with GraphQL to talk to cosmosdb!