From 99dc78ee4ba1ccc488d58f604ba3d45a7eea6e87 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Wed, 13 May 2026 23:14:37 +0330 Subject: [PATCH] Fix GraphQL Implementation and also UI Playground and add supports for Oracle Provider using EF Core ... --- Configuration/XDataBaseConfiguration.cs | 2 +- Constants/XDbProviders.cs | 4 +- DI/XDIHelperExtension.cs | 52 +- GraphQL/XBaseGraphQLQuery.cs | 25 +- GraphQL/XBaseGraphQLTypeHelper.cs | 67 +-- Helpers/XDataServiceHelper.cs | 646 +++++++++++++++++------- Interfaces/IXBaseDbSeeder.cs | 14 + Interfaces/IXBaseGraphQLQuery.cs | 8 +- Interfaces/IXBaseGraphQLTypeHelper.cs | 9 +- Providers/XBaseDbSeeder.cs | 40 +- xDataService.csproj | 5 +- 11 files changed, 576 insertions(+), 296 deletions(-) diff --git a/Configuration/XDataBaseConfiguration.cs b/Configuration/XDataBaseConfiguration.cs index 2fa6357..866e975 100644 --- a/Configuration/XDataBaseConfiguration.cs +++ b/Configuration/XDataBaseConfiguration.cs @@ -24,6 +24,6 @@ namespace xDataService.Configuration /// Holds Seeding items ... /// nameof(TEntity) => TEntity /// - public IDictionary> SeedItems { get; set; } = new Dictionary>(); + public IDictionary> SeedItems { get; set; } = new Dictionary>(); } } \ No newline at end of file diff --git a/Constants/XDbProviders.cs b/Constants/XDbProviders.cs index 91f059a..db90c06 100644 --- a/Constants/XDbProviders.cs +++ b/Constants/XDbProviders.cs @@ -6,6 +6,7 @@ namespace xDataService.Constants { None, MySQL, SQLite, + Oracle, SQLServer, MongoDB, } @@ -16,8 +17,9 @@ namespace xDataService.Constants { public partial struct ProviderType { public const string MySQL = "MYSQL"; public const string SQLite = "SQLITE"; - public const string SQLServer = "SQLSERVER"; + public const string Oracle = "ORACLE"; public const string MongoDB = "MONGODB"; + public const string SQLServer = "SQLSERVER"; } } \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 5a7bdf5..f8e6cc5 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -1,6 +1,7 @@ using System; +using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; +using GraphQL.Server.Ui.Playground; using Microsoft.AspNetCore.Builder; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.Extensions.Configuration; @@ -45,10 +46,24 @@ namespace xDataService.DI .ForEach(db => { // - // TODO: Fix this ... - var sectionName = $"{ConfigurationNodeNames.DATA_SERVICE_NODE}:{nameof(XDataServiceConfiguration.Databases)}:{db.Key}:{nameof(XDataBaseConfiguration.SeedItems)}"; - // var seedItems = source.GetSectionAsDictionary(sectionName); - // db.Value.SeedItems = seedItems; + var seedItemsSectionName = $"{ConfigurationNodeNames.DATA_SERVICE_NODE}:{nameof(XDataServiceConfiguration.Databases)}:{db.Key}:{nameof(XDataBaseConfiguration.SeedItems)}"; + var section = source.GetSection(seedItemsSectionName); + var sectionJson = source.GetSectionAsJson(seedItemsSectionName); + var sectionDict = sectionJson.FromJSON>>(); + if (!sectionDict.IsNull() && + sectionDict.Values.HasChild()) + { + // + sectionDict + .Keys + .ToList() + .ForEach(entity => + { + // + // Setting Values to Result ... + result.Databases[db.Key].SeedItems[entity] = sectionDict[entity]; + }); + } }); } @@ -248,7 +263,8 @@ namespace xDataService.DI public static void UseXDatabase( this IApplicationBuilder source, XDatabaseDescriptor descriptor, - bool isDevelopmentEnvironment = false + bool isDevelopmentEnvironment = false, + GraphQLPlaygroundOptions options = null ) { // @@ -265,9 +281,9 @@ namespace xDataService.DI // var withPlayground = false; + var isGraphConfigured = false; if (isDevelopmentEnvironment) { - // withPlayground = true; } @@ -297,6 +313,7 @@ namespace xDataService.DI // // Check Seeder Exists ... + // Execute Seeding Data Task if Exists ... isValid = r.HasSeeder(); if (isValid) { @@ -312,7 +329,8 @@ namespace xDataService.DI // Seeding ... seeder .Seed() - .RunTask(); + .GetAwaiter() + .GetResult(); } } @@ -323,7 +341,10 @@ namespace xDataService.DI { // // Use WebSockets ... - source.UseWebSockets(); + if (!isGraphConfigured) + { + source.UseWebSockets(); + } // // Inject GraphQL Type Helper ... @@ -332,7 +353,9 @@ namespace xDataService.DI if (isValid) { // - string graphPath = helper.GetGraphQLPath(); + string graphPath = $"{helper.GetGraphQLPath()}"; + // string graphPath = $"{dataServiceConfiguration.GraphQLBasePath}{helper.GetGraphQLPath()}"; + graphPath = $"{helper.GetGraphQLPath()}"; typeof(XDataServiceHelper).InvokeGenericMethod( methodName: "UseXGraphQL", runtimeTypes: [ @@ -352,6 +375,15 @@ namespace xDataService.DI null ] ); + + // + Log($"Register GraphQL: {graphPath}"); + } + + // + if (!isGraphConfigured) + { + isGraphConfigured = true; } } }); diff --git a/GraphQL/XBaseGraphQLQuery.cs b/GraphQL/XBaseGraphQLQuery.cs index fec6d58..d45ac0b 100644 --- a/GraphQL/XBaseGraphQLQuery.cs +++ b/GraphQL/XBaseGraphQLQuery.cs @@ -11,16 +11,17 @@ using xModels.Base; namespace xDataService.GraphQL { - public abstract class XBaseGraphQLQuery : ObjectGraphType, IXBaseGraphQLQuery { } - - public abstract class XBaseGraphQLQuery : XBaseGraphQLQuery, IXBaseGraphQLQuery + public abstract class XBaseGraphQLQuery : ObjectGraphType, IXBaseGraphQLQuery where TEntity : XBaseEntity + where TGraph : IGraphType + where TGraphKey : IGraphType { // protected readonly XDataServiceConfiguration configuration; protected readonly IXBaseRepository repository; protected readonly IXBaseGraphQLTypeHelper helper; + // public XBaseGraphQLQuery( XDataServiceConfiguration configuration, IXBaseRepository repository, @@ -34,25 +35,7 @@ namespace xDataService.GraphQL // Name = GetType().Name; - } - } - public abstract class XBaseGraphQLQuery : XBaseGraphQLQuery, IXBaseGraphQLQuery - where TEntity : XBaseEntity - where TGraph : IGraphType - where TGraphKey : IGraphType - { - // - public XBaseGraphQLQuery( - XDataServiceConfiguration configuration, - IXBaseRepository repository, - IXBaseGraphQLTypeHelper helper - ) : base( - helper: helper, - repository: repository, - configuration: configuration - ) - { // #region Retrieve ... // diff --git a/GraphQL/XBaseGraphQLTypeHelper.cs b/GraphQL/XBaseGraphQLTypeHelper.cs index 1873451..fa06eda 100644 --- a/GraphQL/XBaseGraphQLTypeHelper.cs +++ b/GraphQL/XBaseGraphQLTypeHelper.cs @@ -5,94 +5,52 @@ using xModels.Base; namespace xDataService.GraphQL { - public abstract class XBaseGraphQLTypeHelper : IXBaseGraphQLTypeHelper + public abstract class XBaseGraphQLTypeHelper : IXBaseGraphQLTypeHelper + where TEntity : XBaseEntity { // - protected readonly XDataServiceConfiguration configuration; + private readonly XDataServiceConfiguration configuration; // public XBaseGraphQLTypeHelper( - XDataServiceConfiguration configuration + XDataServiceConfiguration configuration = null ) { this.configuration = configuration; } - public abstract string GetInQuerySingleName(); - + // public abstract string GetInQueryCollectionName(); - // - #region Actions ... - public virtual string GetCountName() - { - return string.Empty; - } - - public virtual string GetExistsName() - { - return string.Empty; - } - - public virtual string GetFindManyName() - { - return string.Empty; - } - - public virtual string GetFindOneName() - { - return string.Empty; - } - - public virtual string GetGraphQLPath(string baseGraphPath = null) - { - return string.Empty; - } - - public virtual string GetQueryName() - { - return string.Empty; - } - #endregion - } - - public abstract class XBaseGraphQLTypeHelper : XBaseGraphQLTypeHelper, IXBaseGraphQLTypeHelper - where TEntity : XBaseEntity - { - // - public XBaseGraphQLTypeHelper( - XDataServiceConfiguration configuration = null - ) : base(configuration) - { } + public abstract string GetInQuerySingleName(); // - #region Actions ... - public override string GetFindOneName() + public string GetFindOneName() { return $"find{GetInQuerySingleName().ToNormalString().Capitalize()}"; } - public override string GetFindManyName() + public string GetFindManyName() { return $"find{GetInQueryCollectionName().ToNormalString().Capitalize()}"; } - public override string GetQueryName() + public string GetQueryName() { return $"query{GetInQueryCollectionName().ToNormalString().Capitalize()}"; } - public override string GetCountName() + public string GetCountName() { return $"count{GetInQueryCollectionName().ToNormalString().Capitalize()}"; } - public override string GetExistsName() + public string GetExistsName() { return $"exists{GetInQuerySingleName().ToNormalString().Capitalize()}"; } - public override string GetGraphQLPath(string baseGraphPath = null) + public string GetGraphQLPath(string baseGraphPath = null) { // baseGraphPath = baseGraphPath.IsNullOrEmpty() ? @@ -111,6 +69,5 @@ namespace xDataService.GraphQL // return $"{basePath}/{GetInQueryCollectionName().ToNormalString()}"; } - #endregion } } \ No newline at end of file diff --git a/Helpers/XDataServiceHelper.cs b/Helpers/XDataServiceHelper.cs index 58b4c39..755cf55 100644 --- a/Helpers/XDataServiceHelper.cs +++ b/Helpers/XDataServiceHelper.cs @@ -1,4 +1,5 @@ using System; +using GraphQL; using GraphQL.Server; using GraphQL.Server.Ui.Playground; using GraphQL.Types; @@ -83,6 +84,21 @@ namespace xDataService.Helpers }, lifetime); break; + // + // Oracle ... + case XDbProviders.Oracle: + // + source.AddEntityFrameworkOracle(); + source.AddDbContext(cfg => + { + // + cfg.UseOracle( + databaseDescriptor.ConnectionString, + databaseDescriptor.OptionsBuilder + ); + }, lifetime); + break; + // // SQLServer ... case XDbProviders.SQLServer: @@ -153,6 +169,7 @@ namespace xDataService.Helpers isValid = databaseDescriptor.Provider == XDbProviders.MySQL || databaseDescriptor.Provider == XDbProviders.SQLite || + databaseDescriptor.Provider == XDbProviders.Oracle || databaseDescriptor.Provider == XDbProviders.SQLServer; if (isValid) { @@ -1223,22 +1240,118 @@ namespace xDataService.Helpers where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper { // + // Validate Args ... var isValid = repositoryDescriptor.IsValid(); if (!isValid) { return; } + // + // Chck Seeders ... + isValid = repositoryDescriptor.HasSeeder(); + if (isValid) + { + } + // // GraphQL ... isValid = repositoryDescriptor.HasGraphSupport(); if (isValid) { // - UseXGraphQL( - source, - repositoryDescriptor - ); + // UseXGraphQL( + // source, + // repositoryDescriptor + // ); + } + } + + /// + /// Register Repository ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void UseXRepository< + TEntity, + TKey, + TKeyGeneratorService, + TKeyGeneratorImplementation, + TEventsService, + TEventsImplementation, + TGraph, + TGraphKey, + TGraphQuery, + TGraphTypeHelperService, + TGraphTypeHelperImplementation, + TGraphSchema, + TRepositoryService, + TRepositoryImplementation, + TSeederService, + TSeederImplementation + >( + IApplicationBuilder source, + XInMemoryRepositoryDescriptor repositoryDescriptor + ) + where TGraphSchema : Schema + where TGraphKey : IGraphType + where TEntity : XBaseEntity + where TGraph : XBaseGraphObjectType + where TSeederService : IXBaseDbSeeder + where TEventsService : IXBaseRepositoryEvents + where TSeederImplementation : XBaseDbSeeder + where TKeyGeneratorService : IXKeyGenerator + where TRepositoryService : IXBaseRepository + where TEventsImplementation : XBaseRepositoryEvents + where TKeyGeneratorImplementation : XKeyGenerator + where TGraphTypeHelperService : IXBaseGraphQLTypeHelper + where TRepositoryImplementation : XBaseInMemoryRepository + where TGraphQuery : XBaseGraphQLQuery + where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper + { + // + // Validate Args ... + var isValid = repositoryDescriptor.IsValid(); + if (!isValid) + { + return; + } + + // + // Chck Seeders ... + isValid = repositoryDescriptor.HasSeeder(); + if (isValid) + { + } + + // + // GraphQL ... + isValid = repositoryDescriptor.HasGraphSupport(); + if (isValid) + { + // + // UseXGraphQL( + // source, + // repositoryDescriptor + // ); } } @@ -1297,101 +1410,311 @@ namespace xDataService.Helpers where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper { // + // Validate Args ... var isValid = repositoryDescriptor.IsValid(); if (!isValid) { return; } + // + // Chck Seeders ... + isValid = repositoryDescriptor.HasSeeder(); + if (isValid) + { + } + // // GraphQL ... isValid = repositoryDescriptor.HasGraphSupport(); if (isValid) { // - UseXGraphQL( - source, - repositoryDescriptor - ); + // UseXGraphQL( + // source, + // repositoryDescriptor + // ); } } - // /// - // /// Register Repository ... - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // /// - // public static void UseXRepository< - // TEntity, - // TKey, - // TKeyGeneratorService, - // TKeyGeneratorImplementation, - // TEventsService, - // TEventsImplementation, - // TGraph, - // TGraphKey, - // TGraphQuery, - // TGraphTypeHelperService, - // TGraphTypeHelperImplementation, - // TGraphSchema, - // TRepositoryService, - // TRepositoryImplementation, - // TContext - // >( - // IApplicationBuilder source, - // XEFRepositoryDescriptor repositoryDescriptor - // ) - // where TGraphSchema : Schema - // where TContext : XDbContext - // where TGraphKey : IGraphType - // where TEntity : XBaseEntity - // where TGraph : XBaseGraphObjectType - // where TEventsService : IXBaseRepositoryEvents - // where TKeyGeneratorService : IXKeyGenerator - // where TRepositoryService : IXBaseRepository - // where TEventsImplementation : XBaseRepositoryEvents - // where TKeyGeneratorImplementation : XKeyGenerator - // where TGraphTypeHelperService : IXBaseGraphQLTypeHelper - // where TGraphQuery : XBaseGraphQLQuery - // where TRepositoryImplementation : XBaseEFRepository - // where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper - // { - // // - // var isValid = repositoryDescriptor.IsValid(); - // if (!isValid) - // { - // return; - // } + /// + /// Register Repository ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void UseXRepository< + TEntity, + TKey, + TKeyGeneratorService, + TKeyGeneratorImplementation, + TEventsService, + TEventsImplementation, + TGraph, + TGraphKey, + TGraphQuery, + TGraphTypeHelperService, + TGraphTypeHelperImplementation, + TGraphSchema, + TRepositoryService, + TRepositoryImplementation, + TSeederService, + TSeederImplementation + >( + IApplicationBuilder source, + XMongoRepositoryDescriptor repositoryDescriptor + ) + where TGraphSchema : Schema + where TGraphKey : IGraphType + where TEntity : XBaseEntity + where TGraph : XBaseGraphObjectType + where TSeederService : IXBaseDbSeeder + where TEventsService : IXBaseRepositoryEvents + where TSeederImplementation : XBaseDbSeeder + where TKeyGeneratorService : IXKeyGenerator + where TRepositoryService : IXBaseRepository + where TEventsImplementation : XBaseRepositoryEvents + where TKeyGeneratorImplementation : XKeyGenerator + where TRepositoryImplementation : XBaseMongoRepository + where TGraphTypeHelperService : IXBaseGraphQLTypeHelper + where TGraphQuery : XBaseGraphQLQuery + where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper + { + // + // Validate Args ... + var isValid = repositoryDescriptor.IsValid(); + if (!isValid) + { + return; + } - // // - // // GraphQL ... - // isValid = repositoryDescriptor.HasGraphSupport(); - // if (isValid) - // { - // // - // UseXGraphQL( - // source, - // repositoryDescriptor - // ); - // } - // } + // + // Chck Seeders ... + isValid = repositoryDescriptor.HasSeeder(); + if (isValid) + { + } + // + // GraphQL ... + isValid = repositoryDescriptor.HasGraphSupport(); + if (isValid) + { + // + // UseXGraphQL( + // source, + // repositoryDescriptor + // ); + } + } + + /// + /// Register Repository ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void UseXRepository< + TEntity, + TKey, + TKeyGeneratorService, + TKeyGeneratorImplementation, + TEventsService, + TEventsImplementation, + TGraph, + TGraphKey, + TGraphQuery, + TGraphTypeHelperService, + TGraphTypeHelperImplementation, + TGraphSchema, + TRepositoryService, + TRepositoryImplementation, + TContext + >( + IApplicationBuilder source, + XEFRepositoryDescriptor repositoryDescriptor + ) + where TGraphSchema : Schema + where TContext : XDbContext + where TGraphKey : IGraphType + where TEntity : XBaseEntity + where TGraph : XBaseGraphObjectType + where TEventsService : IXBaseRepositoryEvents + where TKeyGeneratorService : IXKeyGenerator + where TRepositoryService : IXBaseRepository + where TEventsImplementation : XBaseRepositoryEvents + where TKeyGeneratorImplementation : XKeyGenerator + where TGraphTypeHelperService : IXBaseGraphQLTypeHelper + where TGraphQuery : XBaseGraphQLQuery + where TRepositoryImplementation : XBaseEFRepository + where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper + { + // + // Validate Args ... + var isValid = repositoryDescriptor.IsValid(); + if (!isValid) + { + return; + } + + // + // Chck Seeders ... + isValid = repositoryDescriptor.HasSeeder(); + if (isValid) + { + } + + // + // GraphQL ... + isValid = repositoryDescriptor.HasGraphSupport(); + if (isValid) + { + // // + // UseXGraphQL( + // source, + // repositoryDescriptor + // ); + } + } + + /// + /// Register Repository ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public static void UseXRepository< + TEntity, + TKey, + TKeyGeneratorService, + TKeyGeneratorImplementation, + TEventsService, + TEventsImplementation, + TGraph, + TGraphKey, + TGraphQuery, + TGraphTypeHelperService, + TGraphTypeHelperImplementation, + TGraphSchema, + TRepositoryService, + TRepositoryImplementation, + TContext, + TSeederService, + TSeederImplementation + >( + IApplicationBuilder source, + XEFRepositoryDescriptor repositoryDescriptor + ) + where TGraphSchema : Schema + where TContext : XDbContext + where TGraphKey : IGraphType + where TEntity : XBaseEntity + where TGraph : XBaseGraphObjectType + where TSeederService : IXBaseDbSeeder + where TEventsService : IXBaseRepositoryEvents + where TSeederImplementation : XBaseDbSeeder + where TKeyGeneratorService : IXKeyGenerator + where TRepositoryService : IXBaseRepository + where TEventsImplementation : XBaseRepositoryEvents + where TKeyGeneratorImplementation : XKeyGenerator + where TGraphTypeHelperService : IXBaseGraphQLTypeHelper + where TGraphQuery : XBaseGraphQLQuery + where TRepositoryImplementation : XBaseEFRepository + where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper + { + // + // Validate Args ... + var isValid = repositoryDescriptor.IsValid(); + if (!isValid) + { + return; + } + + // + // Chck Seeders ... + isValid = repositoryDescriptor.HasSeeder(); + if (isValid) + { + } + + // + // GraphQL ... + isValid = repositoryDescriptor.HasGraphSupport(); + if (isValid) + { + // // + // UseXGraphQL( + // source, + // repositoryDescriptor + // ); + } + } + + + /// + /// Use GraphQL ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public static void UseXGraphQL< TEntity, TKey, @@ -1422,6 +1745,14 @@ namespace xDataService.Helpers // if (withPlayGround) { + // + if (options.IsNull()) + { + options = new GraphQLPlaygroundOptions(); + } + options.Path = "/ui/playground"; + + // source.UseGraphQLPlayground(options); } } @@ -1600,43 +1931,13 @@ namespace xDataService.Helpers return; } - // - // Register Graph Schema ... - source.Add( - new ServiceDescriptor( - repositoryDescriptor.GraphSchema, - repositoryDescriptor.GraphSchema, - lifetime - ) - ); - - // - // Register Graph Query ... - source.Add( - new ServiceDescriptor( - repositoryDescriptor.GraphQuery, - repositoryDescriptor.GraphQuery, - lifetime - ) - ); - - // - // Register Graph Type Helper ... - source.Add( - new ServiceDescriptor( - repositoryDescriptor.GraphTypeHelperService, - repositoryDescriptor.GraphTypeHelperImplementation, - lifetime: ServiceLifetime.Singleton - ) - ); - // // Register Graph Type ... source.Add( new ServiceDescriptor( - repositoryDescriptor.GraphType, - repositoryDescriptor.GraphType, - ServiceLifetime.Singleton + lifetime: ServiceLifetime.Singleton, + serviceType: repositoryDescriptor.GraphType, + implementationType: repositoryDescriptor.GraphType ) ); @@ -1644,19 +1945,60 @@ namespace xDataService.Helpers // Register Graph Events ... source.Add( new ServiceDescriptor( - typeof(XBaseGraphQLEventType), - typeof(XBaseGraphQLEventType), - ServiceLifetime.Singleton + lifetime: ServiceLifetime.Singleton, + serviceType: typeof(XBaseGraphQLEventType), + implementationType: typeof(XBaseGraphQLEventType) ) ); + // + // Register Graph Type Helper ... + source.Add( + new ServiceDescriptor( + lifetime: ServiceLifetime.Singleton, + serviceType: repositoryDescriptor.GraphTypeHelperService, + implementationType: repositoryDescriptor.GraphTypeHelperImplementation + ) + ); + + // + // Register Graph Query ... + source.Add( + new ServiceDescriptor( + lifetime: lifetime, + serviceType: repositoryDescriptor.GraphQuery, + implementationType: repositoryDescriptor.GraphQuery + ) + ); + + // + // Register Graph Schema ... + source.Add( + new ServiceDescriptor( + serviceType: repositoryDescriptor.GraphSchema, + implementationType: repositoryDescriptor.GraphSchema, + lifetime: lifetime + ) + ); + + // + source.AddSingleton(); + // // Register Graph Type Schema ... var builder = source.GetRegisteredService(); if (builder.IsNull()) { // - Action options = x => { }; + Action options = x => + { + // + x.EnableMetrics = true; + x.UnhandledExceptionDelegate = context => + { + Console.WriteLine("XGraphQL Error: " + context.OriginalException.Message); + }; + }; builder = source.AddGraphQL(options) .AddNewtonsoftJson(deserializerSettings => { }, serializerSettings => { }) .AddWebSockets() @@ -1665,72 +2007,6 @@ namespace xDataService.Helpers } builder.AddGraphTypes(repositoryDescriptor.GraphSchema); } - - /// - /// Use GraphQL Middleware ... - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public static void UseXGraphQL< - TEntity, - TKey, - TGraph, - TGraphKey, - TGraphQuery, - TGraphTypeHelperService, - TGraphTypeHelperImplementation, - TGraphSchema - >( - IApplicationBuilder source, - XRepositoryDescriptor repositoryDescriptor - ) - where TGraphSchema : Schema - where TGraphKey : IGraphType - where TEntity : XBaseEntity - where TGraph : XBaseGraphObjectType - where TGraphTypeHelperService : IXBaseGraphQLTypeHelper - where TGraphQuery : XBaseGraphQLQuery - where TGraphTypeHelperImplementation : XBaseGraphQLTypeHelper - { - // - // Validate ... - var isValid = - !source.IsNull() && - !repositoryDescriptor.IsNull() && - !repositoryDescriptor.GraphType.IsNull() && - !repositoryDescriptor.GraphQuery.IsNull() && - !repositoryDescriptor.GraphSchema.IsNull() && - !repositoryDescriptor.GraphTypeKey.IsNull() && - !repositoryDescriptor.GraphTypeHelperService.IsNull() && - !repositoryDescriptor.GraphTypeHelperImplementation.IsNull(); - if (!isValid) - { - return; - } - - // - // Create an Scope for Services Acces ... - using (var scope = source.ApplicationServices.CreateScope()) - { - // - // Inject GraphQL Type Helper ... - var helper = scope.ServiceProvider.GetService(); - - // - source.UseGraphQL(helper.GetGraphQLPath()); - source.UseGraphQLWebSockets(); - } - } #endregion } } \ No newline at end of file diff --git a/Interfaces/IXBaseDbSeeder.cs b/Interfaces/IXBaseDbSeeder.cs index 30742e3..953dcf9 100644 --- a/Interfaces/IXBaseDbSeeder.cs +++ b/Interfaces/IXBaseDbSeeder.cs @@ -10,10 +10,24 @@ namespace xDataService.Interfaces public interface IXBaseDbSeeder where TEntity : XBaseEntity { + /// + /// Holds Entity Name ... + /// + string Entity { get; } + + /// + /// Holds Database Name ... + /// string Database { get; } + /// + /// Repository Service for Data Manipulation ... + /// IXBaseRepository Repository { get; } + /// + /// DataServiceConfiguration ... + /// XDataServiceConfiguration DataServiceConfiguration { get; } /// diff --git a/Interfaces/IXBaseGraphQLQuery.cs b/Interfaces/IXBaseGraphQLQuery.cs index 9426ab2..8e0aa70 100644 --- a/Interfaces/IXBaseGraphQLQuery.cs +++ b/Interfaces/IXBaseGraphQLQuery.cs @@ -3,13 +3,7 @@ using xModels.Base; namespace xDataService.Interfaces { - public interface IXBaseGraphQLQuery { } - - public interface IXBaseGraphQLQuery : IXBaseGraphQLQuery - where TEntity : XBaseEntity - { } - - public interface IXBaseGraphQLQuery : IXBaseGraphQLQuery + public interface IXBaseGraphQLQuery where TEntity : XBaseEntity where TGraph : IGraphType where TGraphKey : IGraphType diff --git a/Interfaces/IXBaseGraphQLTypeHelper.cs b/Interfaces/IXBaseGraphQLTypeHelper.cs index 940570b..7626d16 100644 --- a/Interfaces/IXBaseGraphQLTypeHelper.cs +++ b/Interfaces/IXBaseGraphQLTypeHelper.cs @@ -2,10 +2,11 @@ using xModels.Base; namespace xDataService.Interfaces { - public interface IXBaseGraphQLTypeHelper + public interface IXBaseGraphQLTypeHelper + where TEntity : XBaseEntity { string GetInQuerySingleName(); - + string GetInQueryCollectionName(); string GetFindOneName(); @@ -20,8 +21,4 @@ namespace xDataService.Interfaces string GetGraphQLPath(string baseGraphPath = null); } - - public interface IXBaseGraphQLTypeHelper : IXBaseGraphQLTypeHelper - where TEntity : XBaseEntity - { } } \ No newline at end of file diff --git a/Providers/XBaseDbSeeder.cs b/Providers/XBaseDbSeeder.cs index a25c4d9..036c81b 100644 --- a/Providers/XBaseDbSeeder.cs +++ b/Providers/XBaseDbSeeder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -12,29 +13,48 @@ namespace xDataService.Providers public abstract class XBaseDbSeeder : IXBaseDbSeeder where TEntity : XBaseEntity { + // + #region Properties ... + /// + /// Holds Entity Name ... + /// + public string Entity { get; } + + /// + /// Holds Database Name ... + /// public string Database { get; } + /// + /// Repository Service for Data Manipulation ... + /// public IXBaseRepository Repository { get; } + /// + /// DataServiceConfiguration ... + /// public XDataServiceConfiguration DataServiceConfiguration { get; } + #endregion + // + #region Constructor ... public XBaseDbSeeder( + string entity, string database, IXBaseRepository repository, XDataServiceConfiguration dataServiceConfiguration ) { // + this.Entity = entity; this.Database = database; this.Repository = repository; this.DataServiceConfiguration = dataServiceConfiguration; } + #endregion public async Task Seed() { - // - var entityName = nameof(TEntity); - // // Validate Seed ... var isValid = @@ -45,7 +65,7 @@ namespace xDataService.Providers db.Key == Database && db.Value.SeedItems .Any(si => - si.Key == entityName && + si.Key == Entity && si.Value.HasChild())); if (!isValid) { @@ -56,7 +76,7 @@ namespace xDataService.Providers // Extract Seeding Items ... var seedItems = DataServiceConfiguration .Databases[Database] - .SeedItems[entityName] + .SeedItems[Entity] .ToList(); foreach (var item in seedItems) { @@ -64,8 +84,9 @@ namespace xDataService.Providers try { // - // Try to convert Item to Entity ... - var entity = ((object)item).FromDynamicObject(); + var entity = item + .ToJSON() + .FromJSON(); // // Check Item Exists or not ... @@ -100,7 +121,10 @@ namespace xDataService.Providers if (!isExist) { // - exists = await Repository.AddAsync(entity); + exists = await Repository.AddAsync( + item: entity, + saveChanges: true + ); } break; diff --git a/xDataService.csproj b/xDataService.csproj index 659bd2d..72d36f2 100644 --- a/xDataService.csproj +++ b/xDataService.csproj @@ -31,12 +31,12 @@ - + - + @@ -52,6 +52,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive