Microsoft Fabric Updates Blog

Smart Mutations in Microsoft Fabric API for GraphQL with Stored Procedures

Overview

Microsoft Fabric API for GraphQL makes it easy to query and mutate data from a Fabric- SQL database and other Fabric data sources such as Data Warehouse and Lakehouse, with strongly typed schemas and a rich query language allowing developers to create an intuitive API without writing custom server code. While you can’t customize resolvers or define new mutations directly since they are automatically generated, you can use stored procedures to encapsulate and reuse complex business logic — including input validation and data transformation.

In this post, you learn how to use a stored procedure to register new products, with server-side logic for validation, formatting, and ID generation — all exposed through a GraphQL mutation in Fabric.

Getting started

We get started by creating a SQL database in Fabric:

  1. In your Fabric workspace, select New Item then SQL database (preview).
  2. Give your database a name, then select Sample data to quickly create all the required tables and data in your database.

Scenario: registering a new product

Suppose you want to create a new product with:

  • Validation of pricing logic (e.g. ListPrice > StandardCost)
  • Transformation (e.g. capitalize the first letter of product name, trim and uppercase product number)
  • ProductID generation (by incrementing the latest ProductID)

Step 1: Create the Stored Procedure

Here’s a T-SQL stored procedure that encapsulates all the business logic we need. In your SQL database, click on New Query and use the following statement:

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;

Click Run to test the execution. You’ll notice a new stored procedure RegisterProduct under the Stored Procedures folder in the SalesLT database. Use the following query to test the procedure logic:

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

Step 2: Create a GraphQL API

Creating an API from your SQL table is fast, easy, and straightforward. You just need to click the New API for GraphQL button in the ribbon and give your API a name.

Next select the SalesLT tables in your database and the stored procedure we just created, then click Load:

Get data screen to select tables and procedures in API for GraphQL

The GraphQL API, schema and all resolvers are automatically generated in seconds based on the SQL tables and stored procedure.

Step 3: Call the Procedure from GraphQL

Once the API is ready, the stored procedure becomes available as a mutation in the Fabric GraphQL schema. Go to the query editor and execute the following mutation:

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
   }
}

Mutation in the GraphQL API portal displaying the results

Tips

  • Fabric GraphQL auto-generates mutation fields for stored procedures that return a result set defined in the output of the procedure.
  • Business logic lives inside the procedure, not the client.
  • Use deterministic ID generation only if you don’t rely on identity columns.

Summary

With this article you learnt how to combine:

✅ Microsoft Fabric API for GraphQL
✅ Microsoft Fabric SQL database
✅ Stored procedures for business logic

This gives you the power to define robust, consistent rules in SQL for your data — and access it cleanly via GraphQL.

🔗 Resources

Related blog posts

Smart Mutations in Microsoft Fabric API for GraphQL with Stored Procedures

June 17, 2025 by Dan Liu

Have you ever found yourself frustrated by inconsistent item creation? Maybe you’ve struggled to select the right workspace or folder when creating a new item or ended up with a cluttered workspace due to accidental item creation. We hear you—and we’re excited to introduce the new item creation experience in Fabric! This update is designed … Continue reading “Introducing new item creation experience in Fabric”

June 12, 2025 by Ed Lima

Introduction Integrating Azure API Management (APIM) with Microsoft Fabric’s API for GraphQL can significantly enhance your API’s capabilities by providing robust scalability and security features such as identity management, rate limiting, and caching. This post will guide you through the process of setting up and configuring these features. You may not be familiar with API … Continue reading “Integrating Azure API Management with Fabric API for GraphQL”