Microsoft GraphQL 的 Fabric API 可讓您輕鬆地從 Fabric SQL 資料庫和其他 Fabric 數據源查詢和變動數據,例如數據倉儲和 Lakehouse,具有強型別的架構和豐富的查詢語言,讓開發人員不需要撰寫自定義伺服器程式代碼,就能建立直覺式 API。 您可以使用預存程式來封裝及重複使用複雜的商業規則,包括輸入驗證和數據轉換。
在此範例中,我們將瞭解如何使用預存程式來註冊新產品,以及伺服器端邏輯來驗證、格式化和標識符產生,這些邏輯都是透過 Fabric 中的 GraphQL 突變公開。
開始吧
我們首先在 Fabric 中建立 SQL 資料庫:
- 在 [網狀架構] 工作區中,選取 [ 新增專案 ],然後選取 [SQL 資料庫] (預覽)。
- 為您的資料庫指定名稱,然後選取 [ 範例數據 ] 以快速建立資料庫中所有必要的數據表和數據。
案例:註冊新產品
假設您想要使用下列專案建立新產品:
- 定價邏輯的驗證(例如。ListPrice > StandardCost)
- 轉換(例如,將產品名稱的第一個字母大寫,將產品編號修剪後轉為大寫)
- ProductID 的生成(通過遞增最新的 ProductID)
步驟 1:建立預存程式
以下是封裝我們需要的所有商業規則的 T-SQL 預存程式。 在您的 SQL 資料庫中,按兩下 [ 新增查詢 ],並使用下列語句:
CREATE PROCEDURE SalesLT.RegisterProduct
@Name nvarchar(50),
@ProductNumber nvarchar(25),
@StandardCost money,
@ListPrice money,
@SellStartDate datetime
AS
BEGIN
SET NOCOUNT ON;
SET IDENTITY\_INSERT SalesLT.Product ON;
-- Validate pricing logic
IF @ListPrice <= @StandardCost
THROW 50005, 'ListPrice must be greater than StandardCost.', 1;
-- Transform product name: capitalize first letter only
DECLARE @CleanName nvarchar(50);
SET @CleanName = UPPER(LEFT(LTRIM(RTRIM(@Name)), 1)) + LOWER(SUBSTRING(LTRIM(RTRIM(@Name)), 2, 49));
-- Trim and uppercase product number
DECLARE @CleanProductNumber nvarchar(25);
SET @CleanProductNumber = UPPER(LTRIM(RTRIM(@ProductNumber)));
-- Generate ProductID by incrementing the latest existing ID
DECLARE @ProductID int;
SELECT @ProductID = ISNULL(MAX(ProductID), 0) + 1 FROM SalesLT.Product;
INSERT INTO SalesLT.Product (
ProductID,
Name,
ProductNumber,
StandardCost,
ListPrice,
SellStartDate
)
OUTPUT
inserted.ProductID,
inserted.Name,
inserted.ProductNumber,
inserted.StandardCost,
inserted.ListPrice,
inserted.SellStartDate
VALUES (
@ProductID,
@CleanName,
@CleanProductNumber,
@StandardCost,
@ListPrice,
@SellStartDate
);
END;
按兩下 [執行 ] 以測試執行。 您會注意到 SalesLT 資料庫中 [預存程式] 資料夾下的新預存程式 RegisterProduct。 使用下列查詢來測試程式邏輯:
DECLARE @RC int
DECLARE @Name nvarchar(50)
DECLARE @ProductNumber nvarchar(25)
DECLARE @StandardCost money
DECLARE @ListPrice money
DECLARE @SellStartDate datetime
-- TODO: Set parameter values here.
Set @Name = 'test product'
Set @ProductNumber = 'tst-0012'
Set @StandardCost = '10.00'
Set @ListPrice = '9.00'
Set @SellStartDate = '2025-05-01T00:00:00Z'
EXECUTE @RC = \[SalesLT\].\[RegisterProduct\]
@Name
,@ProductNumber
,@StandardCost
,@ListPrice
,@SellStartDate
GO
步驟 2:建立 GraphQL API
從 SQL 資料表建立 API 是快速、簡單且直接的。 您只需要按下 SQL 資料庫功能區中的 [ 適用於 GraphQL 的新 API ] 按鈕,併為您的 API 命名。
接下來,選取您資料庫中的 SalesLT 數據表,以及我們剛才建立的預存程式,然後按兩下 [載入]:
GraphQL API、架構和所有解析程式都會根據 SQL 資料表和預存程式,以秒為單位自動產生。
步驟 3:從 GraphQL 呼叫程式
一旦 API 準備就緒,預存程式就會在 Fabric GraphQL 架構中變成突變。 移至查詢編輯器,然後執行下列突變:
mutation {
executeRegisterProduct (
Name: " graphQL swag ",
ProductNumber: "gql-swag-001",
StandardCost: 10.0,
ListPrice: 15.0,
SellStartDate: "2025-05-01T00:00:00Z"
) {
ProductID
Name
ProductNumber
StandardCost
ListPrice
SellStartDate
}
}
秘訣
- Fabric GraphQL 會自動為預存程式產生突變字段,這些預存程式會傳回程序輸出中定義的結果集。
- 商業規則存在於程式內,而不是用戶端。
- 只有在您不依賴識別數據行時,才使用具決定性標識符產生。
透過網狀架構 API 公開預存程式可讓您為數據定義健全且一致的 SQL 規則,並透過 GraphQL 完全存取它。
相關內容
- 適用於 GraphQL 編輯器的 Fabric API
- Microsoft Fabric 中的 SQL 資料庫