GraphQL API for SQL DB C# graphql-to-sql engine


features C# graphql server

  • GraphQL for .NET is used to parse a query. Engine translates it to one or several SQL queries that are executed efficiently with NReco.Data library.
  • simple graphql schema configuration with JSON
  • supports SQL Server, Mysql, PostgreSQL (+ any other DB that has an ADO.NET provider)
  • pagination (first/offset), sort, complex filters
  • aggregate queries (translated to SQL GROUP BY)
  • batch data load of related objects (no N+1 problem)
  • graphql schema can be automatically generated by existing SQL database and always remains up-to-date! Tables and columns names may be customized/excluded from Graphql API.
  • extremely easy to use: you can setup graphql backend to your database in minutes with several lines of C# code.
  • can be used both in .NET Framework and .NET Core / NET6+ apps.
  • GraphQL backend may be deployed as .NET Core microservice (can be hosted on Linux).
It's time to add GraphQL API to your ASP.NET application!

download

quick purchase process

  • 1 Choose a package
  • 2 Pay online Online payment methods
  • 3 Download the package

how to use get started

  1. Install NReco.GraphQL nuget package
  2. Initialize the engine (graphqlCfg can be deserialized from JSON):
    var dataSource = ConfigureDataSource();
    var graphqlCfg = ConfigureGraphqlMetadata();
    var graphqlAdapter = new GraphqlDbAdapter(dataSource, graphqlCfg);
    
  3. Execute graphql query in C#:
    var resultJson = graphqlAdapter.ExecuteToJsonAsync(graphQueryStr);
  4. Next steps:

online demo

Graphql query #1 | Graphql query #2 | Graphql query #3
Run GraphQL Query

Have a question? Feel free to ask.

frequently asked questions

NReco.GraphQL provides its own model for Graphql schema configuration. It can be dynamic (defined in run-time) and it is suitable for database-first usage scenario:

NReco.GraphQL.Schemas.GraphqlConfiguration ComposeGraphqlSchema() {
  var graphqlCfg = new NReco.GraphQL.Schemas.GraphqlConfiguration();
  graphqlCfg.SchemaObjects = new [] {
    new ObjectSchema {
      SingleName = "customer",   // name of single-object in graphql query
      ListName = "customers",    // name of list in graphql query
      Table = "Customers",       // real database table name (or dataview)
      Fields = new FieldSchema[] {
        new FieldSchema {
          Name = "id",           // field name in graphql query
          Column = "CustomerID", // real table's column name
          DataType = TypeCode.String
        },
        new FieldSchema { Name = "CompanyName", DataType = TypeCode.String },
      },
      RelatedObjects = new RelatedSchema[] {
        new RelatedSchema {
          Name = "orders",       // refers to another ObjectSchema.Name 
          // query that determines how to load sub-selection objects in context of parent table columns
          Relex = @"orders(CustomerID=""id"":var)[*]"
        }
      }
    },
  };
}

Graphql-to-SQL engine uses open-source NReco.Data (MIT license) for SQL queries generation; this library can be used with existing ADO.NET providers:

  • MS SQL Server, Azure SQL
  • MySql, MariaDB and other mysql protocol compatible databases
  • PostgreSql, Amazon Redshift
  • Oracle, IBM DB2, Sqlite and many others (any SQL DB that has ADO.NET provider)

In fact it is possible to use a database even if it has no ADO.NET provider with help of ODBC data provider (which is already available for .NET Core apps).

In addition to SQL generation NReco.Data supports:

  • application-level data views: you can define templates with complex SELECTs to load Graphql objects
  • hooks for modifying database queries for data access control: extra WHERE conditions may be added depening on the security context
  • access log: all queries may be logged
  • data caching: in case of heavy graphql API load it is possible to cache SQL query result in respect to parameters and security context

The following code snippet illustrates how to configure a database adapter (SQL Server):

private NReco.Data.DbDataAdapter ConfigureDataSource() {
	var connStr = "Server=localhost;Database=northwind;";
	// configure ADO.NET and NReco.Data components
	var dbFactory = new NReco.Data.DbFactory(System.Data.SqlClient.SqlClientFactory.Instance) {
		LastInsertIdSelectText = "SELECT @@IDENTITY"
	};
	var dbConnection = dbFactory.CreateConnection();
	dbConnection.ConnectionString = connStr;
	var dbCmdBuilder = new NReco.Data.DbCommandBuilder(dbFactory);
	return new NReco.Data.DbDataAdapter(dbConnection, dbCmdBuilder);
}
NReco.Graphql library has 2 types of license:
  • Standard: applicable for all project types except SaaS apps. Its price takes into account project's development team size (all tech people who works on the project). This license allows component DLL redistribution as part of your product - say, if this is on-premise software that has multiple installations. It is possible to purchase enterprise license of this type which allows unlimited number of developers.
  • SaaS: applicable only for cloud SaaS apps. Takes into account max number of servers (VMs, docker containers) used by SaaS app in production. It is possible to purchase enterprise license of this type which allows unlimited number of SaaS-app servers.
Sometimes the product uses mixed sales model - available both as cloud SaaS service and on-premise installations. In this case both kind of licenses are needed.

Commercial license itself is perpetual and your license key will not expire. However, free upgrades are limited by 1 year - this means that your key will work for all new releases published in a year after purchase date. After this period you can use the latest version that accepts your key, or purchase a new license key to upgrade.

more components

  • NReco Data Access library

    High-performance schemaless data access library for NET6+, .NET Core and .NET Framework. Can be used with any ADO.NET provider (SQL Server, Mysql, PostgreSQL, SQLite etc).

  • PivotData Microservice

    On-premises NET8 app that provides web API for PivotData Toolkit functionality: create pivot tables & charts by SQL/MongoDb/ElasticSearch databases, export reports to Excel/PDF/JSON/CSV/Image. Can be used as a backend for web pivot table builder.