Aggregator pattern with Microsoft Fabric and GraphQL
1. About this blog
How can Microsoft Fabric help address common challenges in microservice patterns?
In this blog, I explore how GraphQL APIs in Microsoft Fabric can help address the communication challenges that often arise in service-to-service interactions within microservices or event-driven architectures, particularly for users who are already leveraging Microsoft Fabric to consolidate data from multiple sources. If you’ve been following my blogs, you might already be familiar with this project that I’m currently working on. While I haven’t started explaining the Durable MultiAgent project in detail, this blog stands independent of that series. You don’t need prior knowledge of what I’ve built so far. Instead, I’ll focus entirely on integrating Microsoft Fabric and setting up database mirroring across various data sources.
Here’s what I’ll cover:
- Aggregator Pattern
- GraphQL
- Database Mirroring
- Setting Up GraphQL in Microsoft Fabric
- Demo
Aggregator Pattern
The Aggregator Pattern is a design strategy used in microservices architectures to handle the challenge of aggregating data from multiple services. Instead of a client making multiple API calls to different services, the aggregator acts as a single point of contact.
Ref: https://learn.microsoft.com/en-us/azure/architecture/patterns/gateway-aggregation
Key Benefits:
- Reduced Network Overhead: The aggregator fetches and combines data from multiple sources, reducing the number of API calls the client needs to make.
- Simplified Client Logic: Clients only need to interact with one service instead of managing the complexity of multiple APIs.
- Improved Performance: By consolidating multiple calls into a single request, the overall response time can be improved, especially for high-latency services.
GraphQL
GraphQL is a query language and runtime for APIs, designed to offer clients exactly the data they need, no more and no less. Unlike traditional REST APIs, where fixed endpoints return predefined data structures, GraphQL allows clients to specify the shape of the response they want.
A GraphQL server serves as a middleware that provides a unified interface to interact with diverse data sources like SQL databases, NoSQL stores, REST APIs, or files. It operates on top of an HTTP server and processes requests using a schema that defines the structure of available data and operations. Clients send queries, mutations, or subscriptions in a JSON format to the server, specifying exactly what data they need. The server translates these requests into the appropriate data source-specific calls, fetches the data, and returns the results in a structured JSON response. This approach minimizes over-fetching or under-fetching of data, offering a more efficient and flexible alternative to traditional REST APIs.
The GraphQL API exposes this functionality, allowing developers to interact with multiple data sources through a single endpoint. Its schema provides a clear contract, ensuring clients know what data and operations are available. Developers can use queries to fetch specific data, mutations to modify it, and subscriptions to receive real-time updates. This abstraction eliminates the need for developers to understand the complexities of underlying data sources, enabling faster development with better security and performance. By decoupling the client from backend intricacies, GraphQL simplifies integration and supports modern application needs.
Why GraphQL?
- Flexible Data Retrieval: Clients can request only the fields they need, reducing over-fetching or under-fetching of data.
- Single Endpoint: A single GraphQL endpoint can replace multiple REST endpoints, simplifying API management.
- Strongly Typed Schema: GraphQL APIs are defined by a schema, making them self-documenting and easier to understand for developers.
- GraphQL shines in microservice architectures by serving as a unified API layer. It can query multiple services simultaneously, aggregate their responses, and return a single, cohesive result to the client. This aligns well with the Aggregator Pattern.
Database Mirroring
Database Mirroring in Fabric is a feature that enables the replication of entire databases and tables from various data sources into Microsoft Fabric’s OneLake. This allows organizations to consolidate data from disparate systems into a unified analytics platform, supporting near real-time data availability.
Key Benefits of Database Mirroring:
- Simplified Data Integration: Eliminates the need for complex ETL pipelines by providing seamless data replication into OneLake.
- Near Real-Time Data Sync: Ensures up-to-date data availability for analytics by automatically syncing changes from the source systems.
- Low-Cost and Low-Latency Solution: Offers a cost-efficient and fast mechanism to replicate data into an analytics-ready format.
- Broad Compatibility: Supports various data sources like Azure SQL Database, Cosmos DB, and Snowflake, enabling integration across platforms.
- Analytics-Ready Data: Converts replicated data into Parquet format for easy consumption by Fabric services, such as Power BI, Spark, and data engineering tools.
- Secure and Collaborative Access: Includes features like Row-Level Security (RLS) and Object-Level Security (OLS), ensuring controlled access and secure decision-making.
- End-to-End Usability: Fully managed service with tools like SQL analytics endpoints and semantic models for querying and analysis.
- Cross-Database Queries: Allows T-SQL queries that join data across mirrored databases and warehouses, simplifying analytics workflows.
Microsoft Fabric Integration in ContosoTravelAgency
This architecture make use of database mirroring to synchronize data from microservices, such as Booking, Flight, Weather, and Passenger services, with corresponding SQL databases. Each microservice operates independently in a containerized environment, ensuring modularity and scalability. The mirrored databases allow seamless replication of transactional data, enabling a consistent and reliable source for downstream processing. This mechanism is critical for ensuring real-time availability of data while reducing the load on the primary transactional systems, thus maintaining high performance and fault tolerance.
Microsoft Fabric is integrated into this system to process and analyze the mirrored data using its Landing Zone and Change Data Capture (CDC) capabilities. The CDC processor continuously tracks changes in the mirrored databases and transforms them into Delta Parquet files stored in OneLake, which serves as a unified data lake for analytics. A GraphQL API layer built on top of the processed data provides clients with a flexible query interface, enabling precise data access through queries and mutations. This integration streamlines data flow from transactional systems to analytical endpoints, fostering a robust data ecosystem for real-time insights and decision-making.
Steps to Set Up Database Mirroring with Microsoft Fabric
Authentication Setup for GraphQL
-
Register an application in Microsoft Entra to obtain:
clientId
clientSecret
tenantId
-
Add required API permissions:
- Under Power BI service, select Delegate Permission.
- Choose the Item.ExecuteAll permission.
-
Configure client credentials:
- Use the client credentials flow for backend service consumption.
- Redirect URIs are not required since this is a backend service setup.
Microsoft Fabric Trial and Licensing
- Sign up for a free Microsoft Fabric trial:
- Use your official account associated with the same tenant where your workloads are hosted.
- Ensure you have a Power BI license. If you don’t have one, sign up for a Power BI license before starting the trial.
Create a Workspace in Microsoft Fabric
- In Microsoft Fabric, create a new workspace.
- Under +New Item, select Mirrored Azure SQL Database.
Set Up Mirrored Azure SQL Database
- Configure connection settings:
- Choose SPN (Service Principal Name).
- Provide the
clientId
,tenantId
, andclientSecret
obtained earlier.
- Grant permissions in Azure SQL Database:
- Run the following commands in your Azure SQL database:
1 2 3 4 5
CREATE USER [EntraAppRegisteredName] FROM EXTERNAL PROVIDER; ALTER ROLE db_datareader ADD MEMBER [EntraAppRegisteredName]; ALTER ROLE db_datawriter ADD MEMBER [EntraAppRegisteredName]; GRANT CONTROL TO [EntraAppRegisteredName];
- Run the following commands in your Azure SQL database:
- Once connected, the initial database mirroring process will begin. It may take some time initially, but subsequent replication will be seamless, providing a near real-time integration experience.
Setup GraphQL API in Microsoft Fabric
- Under +New Item, select API for GraphQL.
- Provide a name for the API and create it.
- Once the API is created, start adding data sources.
Configure Data Sources for GraphQL API
- Data sources to mirror:
- Mirror Azure SQL Database.
- Mirror Azure Cosmos DB (ensure the Database Mirroring feature is enabled in the Cosmos DB account before proceeding).
- Steps to add data sources:
- Click on Select Data Source.
- Choose Single Sign-On (SSO) for authentication.
- Avoid using saved credentials as they won’t work when consuming the GraphQL endpoint in a backend application.
Define Relationships for Cross-Table and Cross-Database Queries
Once the data sources are added, you can begin setting up relationships to enable cross-table and cross-database queries.
Steps to Create Relationships
-
Open the Relationship Manager:
- Select the desired table.
- Click on Manage Relationships.
- In the popup window, choose to Create New Relationship.
-
Define the Relationship:
- Choose the Type of Relationship:
- One-to-One
- One-to-Many
- Many-to-Many
- Many-to-One
- Specify the From Type and select the source table.
- Select the fields from the source table.
- Choose the Type of Relationship:
-
Map to the Target Table:
- Specify the To Type and select the target table.
- Map the respective fields between the source and target tables.
-
Finalize the Relationship:
- Once the fields are mapped, click Create Relationship to save.
After defining the relationships, your data model is ready for performing cross-table and cross-database queries seamlessly.
GraphQL query
Example GraphQL Query: GetBookingDetails
|
|
C# code for consuming GraphQL in backend service
Code to get token
|
|
This simplifies authentication with Microsoft Fabric APIs by leveraging Azure AD’s client credentials flow. It securely retrieves client credentials (Client ID, Client Secret, and Tenant ID) from the configuration, uses the Microsoft Authentication Library (MSAL) to acquire an access token for the required API scopes (e.g., Power BI), and provides this token to enable seamless interaction with Microsoft Fabric services for tasks like database mirroring or GraphQL API integration.
Code to consume GraphQL API
|
|
The FetchBookingDetailsAsync method fetches booking-related details for a passenger and a flight by querying a GraphQL API. It first retrieves an access token using the FabricAuthService for secure authentication. It then creates a GraphQL client configured with the API endpoint and sets the authentication token in the HTTP headers. The method sends a GraphQL query that retrieves passenger details (e.g., name, email, phone) and flight details (e.g., flight number, departure time, price, airline information) based on the provided passenger ID and flight ID. The response data is returned dynamically, enabling seamless integration of booking and flight information.