Create the database container
Create the necessary database container needed to represent Books. There are different ways to model this sample and you can learn more about data modeling. We'll use the embedded data model in this sample.
- Books: Collection containing books and it's authors with 'ID' as the partition key.
Once the container is created, we can import the sample data that is placed in the 'azure-cosmos-db' folder to the book collection by using the 'add new item' option (Make sure you add one by one item) in the Azure Data Explorer.
Add Book schema file
We need to expose the books collection so that it can be used via GraphQL. Cosmos DB, being schema agnostic, requires us to provide the schema definition for the collection. The schema definition needs to be added in the schema.gql file.
Start by adding the Book schema:
type Book @model {
id: ID
title: String
Authors: [Author]
}
type Author {
id: ID
first_name: String
middle_name: String
last_name: String
}
Get the Cosmos DB Account connection string
You can obtain the connection string by navigating to your Azure Cosmos DB account page, and select Primary connection string. Copy the value to use in the Data API Builder
AccountEndpoint=AccountEndpoint=https://localhost:8081/;AccountKey=REPLACEME;
Creating a configuration file for DAB
The Data API builder for Azure Databases engine needs a configuration file. There you'll define which database DAB connects to, and which entities are to be exposed by the API, together with their properties.
For this getting started guide, you'll use DAB CLI to initialize your configuration file. Run the following command:
dab init --database-type "cosmosdb_nosql" --graphql-schema schema.gql --cosmosdb_nosql-database PlaygroundDB --connection-string "AccountEndpoint=https://localhost:8081/;AccountKey=REPLACEME;" --host-mode "Development"
The command generates a config file called dab-config.json looking like this.
{
"$schema": "dab.draft.schema.json",
"data-source": {
"database-type": "cosmosdb_nosql",
"connection-string": "AccountEndpoint=https://localhost:8081/;AccountKey=REPLACEME;"
"options": {
"database": "PlaygroundDB",
"schema": "schema.gql"
}
},
"runtime": {
"rest": {
"path": "/api"
},
"graphql": {
"allow-introspection": true,
"path": "/graphql"
},
"host": {
"mode": "development",
"cors": {
"origins": [],
"allow-credentials": false
},
"authentication": {
"provider": "StaticWebApps"
}
}
},
"entities": {}
}
Add Book entity
We want to expose the books collection so that they can be used via GraphQL. For doing that, all we need is to add the related information to the entities section of the configuration file.
dab add Book --source books --permissions "anonymous:*"
Start Data API builder for Azure Cosmos DB
Run the below command (this starts the engine with default config dab-config.json, use option --config otherwise).
dab start
Once it's successfully started, then you'll see something like.
info: Azure.DataApiBuilder.Service.Startup[0]
Successfully completed runtime initialization.
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
Once you done with above step you will run https://localhost:5001/graphql/ and able to make query.
{
books(filter: { title: { eq: "Foundation" } }) {
items {
id
title
Authors {
first_name
last_name
}
}
}
}
Now working fine please follow step carefully and comment if any dowt or query .
Thanks :)