通过


快速入门:将数据 API 生成器与NoSQL配合使用

在本快速入门中,你将使用数据 API 生成器(DAB)为本地 Azure Cosmos DB for NoSQL 模拟器创建 GraphQL 终结点。

注释

数据 API 生成器中的 Azure Cosmos DB for NoSQL 仅支持 GraphQL 终结点。 REST 终结点不适用于此数据库类型。

先决条件

安装数据 API 生成器 CLI

将 NuGet 中的 Microsoft.DataApiBuilder 包安装为 .NET 工具。

  1. 使用dotnet tool install来安装带有Microsoft.DataApiBuilder参数的--global的最新版本。

    dotnet tool install --global Microsoft.DataApiBuilder
    

    注释

    如果包已安装,请改用 dotnet tool update更新包。

    dotnet tool update --global Microsoft.DataApiBuilder
    
  2. 使用dotnet tool list参数验证是否已安装--global工具。

    dotnet tool list --global
    

拉取模拟器映像

下载 Azure Cosmos DB for NoSQL 模拟器映像。 此下载可能需要几分钟时间,因为仿真器映像很大。

docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest

启动模拟器

在 Docker 中运行 Cosmos DB 模拟器。 需要该 AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE 设置,以便模拟器为其网络终结点播发 127.0.0.1 ,使其可从主机访问。

docker run --name dab-cosmos --publish 8081:8081 --publish 10250-10255:10250-10255 --env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=127.0.0.1 --detach mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest

注释

模拟器启动 11 个内部分区,可能需要 30 到 60 秒 才能准备就绪。 可以通过在浏览器中打开 https://localhost:8081/_explorer/index.html 来验证它是否正在运行。 浏览器可能会警告关于自签名证书。这是安全的,请继续。

安装模拟器证书

Cosmos DB 模拟器使用自签名 SSL 证书。 下载并信任此证书,以便数据 API 生成器可以连接到模拟器。

curl -k https://localhost:8081/_explorer/emulator.pem > ~/emulatorcert.crt
sudo cp ~/emulatorcert.crt /usr/local/share/ca-certificates/
sudo update-ca-certificates

创建数据库和种子数据

使用模拟器的内置数据资源管理器创建数据库、容器和示例项。 无需额外的工具 - 数据资源管理器在浏览器中作为模拟器的一部分运行。

  1. 打开数据资源管理器。https://localhost:8081/_explorer/index.html

  2. 选择 “新建数据库”。 输入 待办事项 作为数据库 ID,然后选择“ 确定”。

  3. 展开 todos 数据库,选择省略号 (...) 菜单,然后选择 “新建容器”。 输入 todos 作为容器 ID, /id 作为分区键,然后选择“ 确定”。

  4. 展开 todos 容器并选择 项目。 然后选择 “新建项”,将默认 JSON 替换为以下内容,然后选择“ 保存”。 对每个项重复此操作。

    项目 1:

    {
      "id": "1",
      "title": "Walk the dog",
      "completed": false
    }
    

    项目 2:

    {
      "id": "2",
      "title": "Feed the fish",
      "completed": false
    }
    

    项目 3:

    {
      "id": "3",
      "title": "Comb the cat",
      "completed": true
    }
    

创建 GraphQL 架构文件

Azure Cosmos DB for NoSQL 需要 GraphQL 架构文件。 创建包含以下内容的名为 schema.gql 的文件。

type Todo @model {
  id: ID!
  title: String!
  completed: Boolean!
}

配置数据 API 生成器

  1. 使用模拟器的默认连接字符串初始化配置。

    dab init --database-type "cosmosdb_nosql" --host-mode "Development" --cosmosdb_nosql-database todos --graphql-schema schema.gql --connection-string "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
    
  2. 添加 Todo 实体。

    dab add Todo --source "todos" --permissions "anonymous:*"
    

文件 dab-config.json 现在应类似于以下示例:

{
  "$schema": "https://github.com/Azure/data-api-builder/releases/latest/download/dab.draft.schema.json",
  "data-source": {
    "database-type": "cosmosdb_nosql",
    "connection-string": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
    "options": {
      "database": "todos",
      "schema": "schema.gql"
    }
  },
  "runtime": {
    "graphql": {
      "enabled": true
    },
    "host": {
      "mode": "development"
    }
  },
  "entities": {
    "Todo": {
      "source": {
        "object": "todos",
        "type": "table"
      },
      "permissions": [
        {
          "role": "anonymous",
          "actions": [
            "*"
          ]
        }
      ]
    }
  }
}

小窍门

可以使用此处显示的内容直接跳过 dab initdab add 命令并创建 dab-config.jsonschema.gql 文件。

启动 API

使用 dab start 来运行该工具并为您的实体创建 API 终结点。

dab start

输出应包括正在运行的 API 的地址。

      Successfully completed runtime initialization.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: <http://localhost:5000>

小窍门

在此示例中,应用程序在端口 localhost 上运行。 正在运行的应用程序可能有不同的地址和端口。

测试 API

  1. 打开浏览器并导航到 GraphQL 终结点。

    http://localhost:5000/graphql
    

    在开发模式下,此 URL 将打开 Nitro GraphQL IDE。

  2. 创建新文档并运行以下查询以检索所有待办事项。

    query {
      todos {
        items {
          id
          title
          completed
        }
      }
    }
    
  3. 响应应包含所有三个待办事项。

    {
      "data": {
        "todos": {
          "items": [
            { "id": "1", "title": "Walk the dog", "completed": false },
            { "id": "2", "title": "Feed the fish", "completed": false },
            { "id": "3", "title": "Comb the cat", "completed": true }
          ]
        }
      }
    }
    

清理

完成后停止并删除 Docker 容器。

docker stop dab-cosmos && docker rm dab-cosmos

后续步骤