Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this Quickstart, you build a set of Data API builder configuration files to target the Azure Cosmos DB for NoSQL emulator.
Prerequisites
Tip
Alternatively, open this Quickstart in GitHub Codespaces with all developer prerequisites already installed. Simply bring your own Azure subscription. GitHub accounts include an entitlement of storage and core hours at no cost. For more information, see included storage and core hours for GitHub accounts.
Install the Data API builder CLI
Install the Microsoft.DataApiBuilder package from NuGet as a .NET tool.
Use
dotnet tool installto install the latest version of theMicrosoft.DataApiBuilderwith the--globalargument.dotnet tool install --global Microsoft.DataApiBuilderNote
If the package is already installed, update the package instead using
dotnet tool update.dotnet tool update --global Microsoft.DataApiBuilderVerify that the tool is installed with
dotnet tool listusing the--globalargument.dotnet tool list --global
Configure the local database
Start by running the local emulator. Then, you can seed a new container with sample data.
Get the latest copy of the
mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latestcontainer image from Docker Hub.docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latestStart the docker container by publishing port 8081 and the port range 10250-10255.
docker run \ --publish 8081:8081 \ --publish 10250-10255:10250-10255 \ --detach \ mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latestDownload the self-signed certificate for the emulator
curl -k https://localhost:8081/_explorer/emulator.pem > ~/emulatorcert.crtInstall the self-signed certificate using either the Bash steps for Linux, or the PowerShell steps for Windows.
sudo cp ~/emulatorcert.crt /usr/local/share/ca-certificates/ sudo update-ca-certificatescertutil -f -addstore "Root" emulatorcert.crtConnect to your local database using your preferred data management environment. Examples include, but aren't limited to: the Azure Databases extension for Visual Studio Code.
Tip
The default connection string for the emulator is
AccountEndpoint=https://localhost:8081;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;.Create a new
bookshelfdatabase andauthorscontainer.Seed the container with this basic JSON data.
[ { "id": "01", "firstName": "Henry", "lastName": "Ross" }, { "id": "02", "firstName": "Jacob", "middleName": "A.", "lastName": "Hancock" }, { "id": "03", "firstName": "Sydney", "lastName": "Mattos" }, { "id": "04", "firstName": "Jordan", "lastName": "Mitchell" }, { "id": "05", "firstName": "Victoria", "lastName": "Burke" }, { "id": "06", "firstName": "Vance", "lastName": "DeLeon" }, { "id": "07", "firstName": "Reed", "lastName": "Flores" }, { "id": "08", "firstName": "Felix", "lastName": "Henderson" }, { "id": "09", "firstName": "Avery", "lastName": "Howard" }, { "id": "10", "firstName": "Violet", "lastName": "Martinez" } ]Tip
The method used to seed data will largely depend on the data management tool. For Azure Data Studio, you can save this JSON array as a .json file and then use the Import feature.
Create configuration files
Create a baseline configuration file using the DAB CLI. Then, add a development configuration file with your current credentials.
Create a new file named schema.graphql with this schema content.
type Author @model { id: ID! firstName: String! middleName: String lastName: String! }Create a typical configuration file using
dab init. Add the--connection-stringargument with the emulator's default connection string.dab init --database-type "cosmosdb_nosql" --host-mode "Development" --cosmosdb_nosql-database bookshelf --graphql-schema schema.graphql --connection-string "AccountEndpoint=https://localhost:8081;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;"Add an Author entity using
dab add.dab add Author --source "authors" --permissions "anonymous:*"
Test API with the local database
Now, start the Data API builder tool to validate that your configuration files are merged during development.
Use
dab startto run the tool and create API endpoints for your entity.dab startThe output of the tool should include the address to use to navigate to the running API.
Successfully completed runtime initialization. info: Microsoft.Hosting.Lifetime[14] Now listening on: <http://localhost:5000> info: Microsoft.Hosting.Lifetime[0]Tip
In this example, the application is running on
localhostat port 5000. Your running application may have a different address and port.Go to the GraphQL endpoint by navigating to
/graphqland running this operation.query { authors { items { id firstName lastName } } }Tip
In this example, the URL would be
https://localhost:5000/graphql. You can navigate to this URL using your web browser.