News combit Dev Blog

New GraphQL Data Provider for List & Label

Posted: Tuesday, December 17, 2019 | 0 comment(s)
 
GraphQL was publicly released in 2015 and became quite popular since then. It's used by a number of big web applications like Instagram, Facebook and others.

According to the GraphQL specification, it is "a query language designed to build client applications by providing an intuitive and flexible syntax and system for describing their data requirements and interactions".

Starting with version 25, connecting List & Label with a GraphQL API as data source is as simple as creating a new List & Label GraphQLDataProvider object and passing your API address as URL constructor parameter.

GraphQLDataProvider prov = new GraphQLDataProvider("https://localhost:44375/api/graphql");
First, the data provider will try to extract the types and queries defined in the GraphQL server by looking at the schema and - based on that - it will create tables, rows, columns and relations.

Queries that are created as tables have a "Q_" prefix in their name. The prefix can be changed by giving a new prefix to the optional constructor parameter "queryPrefix" of the GraphQLDataProvider class. These queries are the only elements that can be used on the "root" level to print and design, as calling a query is the only way to get actual data in GraphQL. The type-based tables can only be used as child tables of queries as shown below. Query parameters will automatically be added as report parmeters.

There are two other properties in this data provider:
  • QueriesRootName: GraphQL server packs all the queries as fields of one single type. By default, the name of the queries type is "Query", but if your API is different, you need to inform List & Label about this fact.
  • MutationsRootName: a mutation is a write followed by a fetch. Because writing the database is not needed nor supported from the reporting side, mutations must not be part of the structure in the Designer. GraphQL server packs all of the mutations as fields of one single type. By default, the name of the mutations type is "Mutation", but if your API is different, you need to inform List & Label about it in order to exclude the mutations from the data structure.

If your GraphQL data source is to support images, you can serialize and transfer them using GraphQL via Base64 string encoding. To decode these fields back to Bitmap for ListLabel, you can use ListLabel AutoDefineField event. the following code shows how to do it in a Northwind sample:

LL.AutoDefineField += new AutoDefineElementHandler(LL_AutoDefineField);
 
void LL_AutoDefineField(object sender, AutoDefineElementEventArgs e)
{
    string name = e.Name.ToString();
    string value = e.Value.ToString();
    if (name == "Q_categories.picture")
    {
        ListLabel ll = sender as ListLabel;
        e.Suppress = true;
        if (value.Length > 50 && value.Length % 4 == 0)
        {
            ll.Fields.Add(e.Name, Convert.FromBase64String(value));                   
        }
        else
        {
            ll.Fields.Add(e.Name, string.Empty, LlFieldType.Drawing_hBitmap);
        }               
    }
}
This is what the Designer looks like when connecting to a simple Northwind service. Note that we're using the Query Q_Categories as root table, not the Type "Category".



Of course, you may also add a GraphQL data source to the Report Server. It also supports to pass authentication information to access your API.


Free List & Label trial for 30 days.




Author: Jochen Bartlau
Head of Development List & Label

Jochen Bartlau leads the development at combit as Managing Director. He's a Microsoft .NET enthusiast driving innovation & agile project management. The mobile devices geek who used to be a physicist in his first life loves to spend his spare time with his family.


  

+1 800 256 3608 (toll-free North America only)