# GraphQL in Microsoft Fabric

Some days ago, I had penned down an [article](https://www.azureguru.net/graphql-with-azure-functions) on integrating GraphQL with Azure Functions. This let me to wonder if Microsoft Fabric offers any options for integrating with GraphQL .Thankfully it does. At the time of writing this article this feature is in preview status..so expect further enhancement in the future .

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725465632528/6edd8d9a-9451-4da9-8019-7ec0a4363370.png align="center")

I explored further wondering if its possible to expose lakehouse data to an application’s data layer, so I created an C# console application to test it out.

### The Setup

I used the following data that had five columns from one of the lakehouse tables.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725466144550/f689d1af-8517-48da-962f-ebc6ee431a4a.png align="center")

I then created a GraphQL endpoint on Fabric for the above data .Creating a GraphQL on your lakehouse data is pretty straightforward. You have to set name and the datasource for your GraphQL endpoint and that's all. Once completed, the landing page of your Fabric GraphQL would look like this

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725466364918/f16b4551-5422-40e5-b4bd-7bc3fa555398.png align="center")

On the left sidebar you'll find the columns of the underlying data and on the right, there's a query window where you can query the data through GraphQL endpoint.

What you see in the screenshot below is a simple GraphQL query to return the `Value` and `MetricDate` columns from the data source.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725472483001/324732b2-0cbc-49c6-bf12-4771bdd31501.png align="center")

Also there is a Copy endpoint option. This option gives you the end point link of the GraphQL service of the workspace where its created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725472594252/93d67714-8c85-4a13-826c-3a52f203ea7c.png align="center")

The format is :  
`https://api.fabric.microsoft.com/v1/workspaces/{workspaceid}/graphqlapis/{graphqlid}/graphql`

The link will be required to be referenced in the console application.

Here is an example of a GraphQL query in Fabric with a filter parameter.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725485372926/21d31ca5-a616-4d6e-a87a-ae109e1084e6.gif align="center")

You can find a list of major filter parameters used in GraphQL [here](https://learn.microsoft.com/en-us/azure/data-api-builder/graphql#supported-root-types).

### App Registration

Login to [https://entra.microsoft.com](https://entra.microsoft.com) to register your application. I registered a new application under the name `Fabric GraphQL Calls`.For more detailed step by step process on how to register a new app, you can refer to one of my earlier article on Fabric REST API's [https://www.azureguru.net/microsoft-fabric-rest-apis](https://www.azureguru.net/microsoft-fabric-rest-apis)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725471361208/b890c395-a602-4761-9010-7f67c7acbbe8.png align="center")

To set permissions for the registered app, under the `API permissions` select `Delegated permissions` for `Power BI` service.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725471928664/31ee84c2-1708-4a66-8e52-9df87b69c125.png align="center")

and set `GraphQL.Execute.All` option

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725471994808/7c864444-fa43-4894-9537-d2017b0d3fa6.png align="center")

### C# Implementation

Create a new console application and ensure you have the following `Nuget` references installed

```csharp
using Microsoft.Identity.Client;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;
```

Declare a bunch of static variables

```csharp
  private const string ClientId = "This is the Application ID from Entra App Registration";  
  private const string Authority = "https://login.microsoftonline.com/organizations";
  private const string GraphQLUri = "Fabric GraphQL endpoint URI"; 
  private static string[] scopes = new string[] { "https://analysis.windows.net/powerbi/api/Item.Execute.All" };
```

In the `Main` method of `Progarm.cs` of the console application add the following code. This code is derived from the similar code that was used in my other [article](https://www.azureguru.net/microsoft-fabric-rest-apis) on Fabric REST API's. Only difference being that here we use `GraphQLHttpClient`

```csharp
  static async System.Threading.Tasks.Task Main(string[] args)
  {

      try
      {
          PublicClientApplicationBuilder PublicClientAppBuilder =
          PublicClientApplicationBuilder.Create(ClientId)
          .WithAuthority(Authority)
          .WithRedirectUri("http://localhost");

          IPublicClientApplication PublicClientApplication = PublicClientAppBuilder.Build();

          AuthenticationResult result = await PublicClientApplication.AcquireTokenInteractive(scopes)
                                               .ExecuteAsync()
                                              .ConfigureAwait(false);

          var graphQLClient = new GraphQLHttpClient(GraphQLUri, new NewtonsoftJsonSerializer());
          graphQLClient.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
          HttpResponseMessage response = await graphQLClient.HttpClient.GetAsync(GraphQLUri);
          graphQLClient.Options.IsValidResponseToDeserialize = response => response.IsSuccessStatusCode;

          var query = new GraphQLHttpRequest
          {
              Query = @"query {
                                 demo_datas
                                {
                                  items {
                                    Value,
                                    MetricDate
                                  }
                                }
                              }
                             "
          };

          Console.WriteLine((await graphQLClient.SendQueryAsync<dynamic>(query)).Data.ToString());

      }
      catch (Exception ex)
      {
          Console.WriteLine($"Error: {ex.Message}");
      }
  }
```

Executing the application outputs the lakehouse data in the `JSON` format.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725480163345/2344ad87-aff8-4716-b822-1fbd99d9917f.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1725490163836/fd92fad1-1ec3-4084-9280-d0e570bfa7db.gif align="center")

![](https://ibb.co/NYGZyBG align="center")

### Closing Notes

I personally think that GraphQL feature in Fabric is a cool and fantastic feature. It offers a powerful and flexible way to query and interact with your data in lakehouse making it easier to access data on an application and help to create robust data-driven applications backed up by the Fabric ecosystem.  
  
Thanks for reading !!!
