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 (and any other DB with ADO.NET provider)
  • sort, pagination (first/offset), complex filtering conditions
  • 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 in .NET Framework and .NET Core 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 2Checkout.com is a worldwide leader in online payment services
  • 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

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 complex SQL SELECT templates
  • hooks for modifying database queries for data access control: extra WHERE conditions may be added depening on 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);
}

more components

  • NReco Data Access library

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

  • PivotData Microservice

    .NET Core app that provides web API for PivotData Toolkit functionality: create pivot tables & charts by SQL/MongoDb databases, export to JSON/CSV/Excel/PDF. Can be used as backend for web pivot table builder.