Configure Data Source NReco.GraphQL documentation


GraphQL API's configuration takes place in two stages. The first stage is configuring DbAdapter to your database (this is component from NReco.Data open-source lib).

// configure ADO.NET and NReco.Data components for SQL Server
var dbFactory = new DbFactory(System.Data.SqlClient.SqlClientFactory.Instance) {
	LastInsertIdSelectText = "SELECT @@IDENTITY"
};
var dbConnection = dbFactory.CreateConnection();
dbConnection.ConnectionString = "Data Source=mssql.customers.com,1433;Database=dbo.customers;User=graphqlUser;Password=user1;";

var dbCmdBuilder = new DbCommandBuilder(dbFactory);
dbCmdBuilder.SelectTemplate = "SELECT @columns FROM @table@where[ WHERE {0}]@orderby[ ORDER BY {0}] @recordcount[LIMIT {0}] @recordoffset[OFFSET {0}]";
dbAdapter = new DbDataAdapter(dbConnection, dbCmdBuilder);
dbAdapter.ApplyOffset = false;

Second stage is defining Schema based on json configuration file:

var graphqlAdapter = new GraphqlDbAdapter(
	dbAdapter, JsonConvert.DeserializeObject<GraphqlConfiguration>(
		System.IO.File.ReadAllText(Path.Combine(ApplicationPath, "metaDbData.json"))
	)
);

or in code:

var graphqlConfiguration = new GraphqlConfiguration();
graphqlConfiguration.SchemaObjects = new ObjectSchema[] {
	new ObjectSchema {
		SingleName = "customer",
		ListName = "customers",
		Table = "db.customers",
		Fields = new FieldSchema[] {
			new FieldSchema {
				Name = "id",
				Column = "ID",
				DataType = TypeCode.Int32
			},
			new FieldSchema {
				Name = "name",
				DataType = TypeCode.String
			}
		}
	};
var graphqlAdapter = new GraphqlDbAdapter(dbAdapter, graphqlConfiguration);