共用方式為


Data source

data-source 段會定義數據庫存取詳細數據。 它也會定義資料庫選項。

數據源設定

Property Description
data-source 包含資料庫連線設定的物件
data-source.database-type 後端中使用的資料庫:mssql、、postgresqlmysqlcosmosdb_nosqlcosmosdb_postgresql
data-source.connection-string 所選資料庫類型的連接字串
data-source.options 資料庫特定屬性(例如,SQL Server、Cosmos DB 等的選項)
data-source.options.database 適用於 NoSQL 資料庫的 Azure Cosmos DB 名稱(需要時 database-type = cosmosdb_nosql
data-source.options.container 適用於 NoSQL 的 Azure Cosmos DB 容器名稱(需要時 database-type = cosmosdb_nosql
data-source.options.schema GraphQL 架構檔案的路徑(需要時 database-type = cosmosdb_nosql
data-source.options.set-session-context 啟用將 JSON Web 令牌 (JWT) 宣告傳送為工作階段內容 (僅限 SQL Server)
data-source.health 設定數據源健康情況檢查的物件
data-source.health.enabled 啟用健康情況檢查端點
data-source.health.name 健康情況報告中使用的標識碼
data-source.health.threshold-ms 健康情況檢查查詢的持續時間上限,以毫秒為單位

Format overview

{
  "data-source": {
    "database-type": <string>,
    "connection-string": <string>,
    "options": {
      // mssql only
      "set-session-context": <true> (default) | <false>,
      // cosmosdb_nosql only
      "database": <string>,
      "container": <string>,
      "schema": <string>
    },
    "health": {
      "enabled": <true> (default) | <false>,
      "name": <string>,
      "threshold-ms": <integer; default: 1000>
    }
  },
  "data-source-files": ["<string>"]
}

Data source

Parent Property 類型 Required Default
$root database-source 物件 ✔️ Yes -

Nested properties

Parent Property 類型 Required Default
data-source database-type 列舉 ✔️ Yes None
data-source connection-string 字串 ✔️ Yes None
data-source options 物件 ❌ 否 None

Property values

database-type Description Min Version
mssql 網狀架構中的 SQL -
mssql Azure SQL Database -
mssql Azure SQL MI -
mssql SQL Server 2016
dwsql Azure Synapse Analytics -
dwsql Fabric Warehouse -
dwsql 網狀架構 SQL 分析端點 -
postgresql PostgreSQL ver. 11
mysql MySQL ver. 8
cosmosdb_nosql Azure Cosmos DB for NoSQL -
cosmosdb_postgresql Azure Cosmos DB for PostgreSQL -

Format

{
  "data-source": {
    "database-type": <string>,
    "connection-string": <string>,
    "options": {
      "<key-name>": <string>
    }
  }
}

範例:Azure SQL 和 SQL Server

"data-source": {
  "database-type": "mssql",
  "connection-string": "Server=tcp:myserver.database.windows.net,1433;Initial Catalog=MyDatabase;User ID=MyUser;Password=MyPassword;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
    "options": {
      "set-session-context": true
    }
}

Note

We use SqlClient for Azure SQL and SQL Server, which supports these connection strings variants.

消費 SESSION_CONTEXT

針對 Azure SQL 和 SQL Server,數據 API 產生器可以在 SQL 的 SESSION_CONTEXT中包含宣告資訊。

CREATE PROC GetUser @userId INT AS
BEGIN
    -- Use claims
    IF SESSION_CONTEXT(N'user_role') = 'admin' 
    BEGIN
        RAISERROR('Unauthorized access', 16, 1);
    END

    SELECT Id, Name, Age, IsAdmin
    FROM Users
    WHERE Id = @userId;
END;

範例:Azure Cosmos DB

"data-source": {
  "database-type": "cosmosdb_nosql",
  "connection-string": "@env('SQL_CONNECTION_STRING')",
  "options": {
    "database": "Your_CosmosDB_Database_Name",
    "container": "Your_CosmosDB_Container_Name",
    "schema": "Path_to_Your_GraphQL_Schema_File"
  }
}

Note

指定的 「選項」是 Azure Cosmos DB 特有的 。databasecontainerschema

Environment variables

使用環境變數,將純文本秘密保留在組態檔中。

"data-source": {
  "database-type": "mssql",
  "connection-string": "@env('SQL_CONNECTION_STRING')"
}

Connection resiliency

數據 API 產生器會使用指數輪詢,在暫時性錯誤之後重試資料庫要求。

Attempts First Second Third Fourth Fifth
Seconds 2s 4s 8s 16s 32s

受控服務識別 (MSI)

連結庫中已定義Azure.Identity受控服務識別 (MSI) 支援DefaultAzureCredential。 深入瞭解 Azure SQL Microsoft Entra 中的受控識別

User-Assigned 受控識別 (UAMI)

For User Assigned Managed Identity, append the Authentication and User Id properties to your connection string while substituting in your User Assigned Managed Identity's client id: Authentication=Active Directory Managed Identity; User Id=<UMI_CLIENT_ID>;.

System-Assigned 受控識別 (SAMI)

For System Assigned Managed Identity, append the Authentication property and exclude the UserId and Password arguments from your connection string: Authentication=Active Directory Managed Identity;.

健康情況 (資料來源)

Parent Property 類型 Required Default
data-source health 物件 No

數據 API 產生器支援多個組態檔,每個組態檔都有自己的數據源。 此組態區塊可讓每個數據源有自己的健康情況設定。

Nested properties

Parent Property 類型 Required Default
data-source.health enabled boolean No true
data-source.health name 字串 No database-type
data-source.health threshold-ms 整數 No 1000

Check name

由於多個組態檔可以指向相同類型的數據源,因此無法在健康情況報告中區分這些數據源。 用來 name 指派唯一且可識別的標籤,只用於健康情況報告。

Check behavior

針對指定的數據源執行最簡單的可能查詢,這是針對指定的數據源執行,以驗證是否可以開啟連線。 threshold-ms使用 屬性來設定該查詢完成的最大可接受持續時間(以毫秒為單位)。

Format

{
  "data-source": {
    "health": {
      "enabled": <true> (default) | <false>,
      "name": <string>,
      "threshold-ms": <integer; default: 1000>
    }
  }
}