From e8de83166778f35336b1ddab56bcba60cc8b46b7 Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Tue, 19 Nov 2024 15:40:21 +0200 Subject: [PATCH 1/5] feat: prepare schema for partition --- .../Infrastructure/SchemaFactory.cs | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs b/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs index 37a49d8..47866f3 100644 --- a/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs +++ b/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using GraphQL; using GraphQL.Conversion; using GraphQL.Instrumentation; @@ -61,14 +62,9 @@ public SchemaFactory(IEnumerable schemaBuilders, IServiceProvide public ISchema GetSchema() { - var schema = new Schema(_services) - { - Query = new ObjectGraphType { Name = "Query" }, - Mutation = new ObjectGraphType { Name = "Mutations" }, - Subscription = new ObjectGraphType { Name = "Subscriptions" }, - Filter = _schemaFilter, - }; + var schema = CreateSchema(_services, _schemaFilter); + var schemaBuilders = GetSchemaBuilders(); foreach (var builder in _schemaBuilders) { builder.Build(schema); @@ -99,9 +95,30 @@ public ISchema GetSchema() schema.Mutation = null; } + if (schema.Subscription.Fields.Count == 0) + { + schema.Subscription = null; + } + return schema; } + protected virtual Schema CreateSchema(IServiceProvider services, ISchemaFilter schemaFilter) + { + return new Schema(services) + { + Query = new ObjectGraphType { Name = "Query" }, + Mutation = new ObjectGraphType { Name = "Mutations" }, + Subscription = new ObjectGraphType { Name = "Subscriptions" }, + Filter = schemaFilter, + }; + } + + protected virtual List GetSchemaBuilders() + { + return _schemaBuilders.ToList(); + } + public void Initialize() { _schema.Value.Initialize(); From cfa95cfa12e649aeb15496a8aa8b49862bd650ba Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Tue, 19 Nov 2024 16:33:27 +0200 Subject: [PATCH 2/5] feat: add scoped schema factory --- .../Infrastructure/SchemaFactory.cs | 2 +- .../Infrastructure/ScopedSchemaFactory.cs | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs diff --git a/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs b/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs index 47866f3..a62f929 100644 --- a/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs +++ b/src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs @@ -65,7 +65,7 @@ public ISchema GetSchema() var schema = CreateSchema(_services, _schemaFilter); var schemaBuilders = GetSchemaBuilders(); - foreach (var builder in _schemaBuilders) + foreach (var builder in schemaBuilders) { builder.Build(schema); } diff --git a/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs b/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs new file mode 100644 index 0000000..abd2e31 --- /dev/null +++ b/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using GraphQL.Introspection; + +namespace VirtoCommerce.Xapi.Core.Infrastructure +{ + public class ScopedSchemaFactory : SchemaFactory + { + public ScopedSchemaFactory( + IEnumerable schemaBuilders, + IServiceProvider services, + ISchemaFilter schemaFilter) + : base(schemaBuilders, services, schemaFilter) + { + } + + protected override List GetSchemaBuilders() + { + var schemaBuilders = base.GetSchemaBuilders(); + + // find all builders with inside this project + var currentAssembly = typeof(TMarker).Assembly; + + var subSchemaBuilders = schemaBuilders + .Where(p => p.GetType().Assembly == currentAssembly) + .ToList(); + + return subSchemaBuilders; + } + } +} From 2d03c1fb07fe334faede944c984173b4c3ccc1be Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Tue, 19 Nov 2024 17:20:46 +0200 Subject: [PATCH 3/5] feat: add extension for registering schema and playground --- .../ApplicationBuilderExtensions.cs | 45 +++++++++++++++++++ .../ModuleConstants.cs | 7 +++ .../VirtoCommerce.Xapi.Core.csproj | 1 + src/VirtoCommerce.Xapi.Web/Module.cs | 22 +++------ .../VirtoCommerce.Xapi.Web.csproj | 1 - 5 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs diff --git a/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs b/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs new file mode 100644 index 0000000..d20a9b5 --- /dev/null +++ b/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs @@ -0,0 +1,45 @@ +using GraphQL.Server.Ui.Playground; +using GraphQL.Types; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using VirtoCommerce.Xapi.Core.Models; +using static VirtoCommerce.Xapi.Core.ModuleConstants; + +namespace VirtoCommerce.Xapi.Core.Extensions; + +public static class ApplicationBuilderExtensions +{ + public static IApplicationBuilder UseSchemaGraphQL(this IApplicationBuilder builder, IConfiguration configuration = null, string schemaPath = null) + where TSchema : ISchema + { + var graphQlPath = "/graphql"; + if (!string.IsNullOrEmpty(schemaPath)) + { + graphQlPath = $"{graphQlPath}/{schemaPath}"; + } + + var playgroundPath = "/ui/playground"; + if (!string.IsNullOrEmpty(schemaPath)) + { + playgroundPath = $"{playgroundPath}/{schemaPath}"; + } + + var isSchemaIntrospectionEnabled = false; + if (configuration != null) + { + isSchemaIntrospectionEnabled = configuration.GetValue($"{ConfigKeys.GraphQlPlayground}:{nameof(GraphQLPlaygroundOptions.Enable)}"); + } + + builder.UseGraphQL(path: graphQlPath); + if (isSchemaIntrospectionEnabled) + { + builder.UseGraphQLPlayground(new PlaygroundOptions + { + GraphQLEndPoint = graphQlPath, + }, + path: playgroundPath); + } + + return builder; + } +} diff --git a/src/VirtoCommerce.Xapi.Core/ModuleConstants.cs b/src/VirtoCommerce.Xapi.Core/ModuleConstants.cs index 7ee5ddf..70e91ab 100644 --- a/src/VirtoCommerce.Xapi.Core/ModuleConstants.cs +++ b/src/VirtoCommerce.Xapi.Core/ModuleConstants.cs @@ -5,6 +5,13 @@ namespace VirtoCommerce.Xapi.Core { public static class ModuleConstants { + public static class ConfigKeys + { + public const string GraphQlPlayground = "VirtoCommerce:GraphQLPlayground"; + public const string GraphQlWebSocket = "VirtoCommerce:GraphQLWebSocket"; + public const string Stores = "VirtoCommerce:Stores"; + } + public static class Connections { public const int DefaultPageSize = 20; diff --git a/src/VirtoCommerce.Xapi.Core/VirtoCommerce.Xapi.Core.csproj b/src/VirtoCommerce.Xapi.Core/VirtoCommerce.Xapi.Core.csproj index 5d8faa3..24d523b 100644 --- a/src/VirtoCommerce.Xapi.Core/VirtoCommerce.Xapi.Core.csproj +++ b/src/VirtoCommerce.Xapi.Core/VirtoCommerce.Xapi.Core.csproj @@ -25,6 +25,7 @@ + diff --git a/src/VirtoCommerce.Xapi.Web/Module.cs b/src/VirtoCommerce.Xapi.Web/Module.cs index 919b1ad..06083d9 100644 --- a/src/VirtoCommerce.Xapi.Web/Module.cs +++ b/src/VirtoCommerce.Xapi.Web/Module.cs @@ -16,6 +16,7 @@ using VirtoCommerce.Xapi.Core.Subscriptions; using VirtoCommerce.Xapi.Data.Extensions; using VirtoCommerce.Xapi.Web.Extensions; +using static VirtoCommerce.Xapi.Core.ModuleConstants; namespace VirtoCommerce.Xapi.Web { @@ -24,15 +25,11 @@ public class Module : IModule, IHasConfiguration public ManifestModuleInfo ModuleInfo { get; set; } public IConfiguration Configuration { get; set; } - private const string _graphQlPlaygroundConfigKey = "VirtoCommerce:GraphQLPlayground"; - private const string _graphQlWebSocketConfigKey = "VirtoCommerce:GraphQLWebSocket"; - private const string _storesConfigKey = "VirtoCommerce:Stores"; - private bool IsSchemaIntrospectionEnabled { get { - return Configuration.GetValue($"{_graphQlPlaygroundConfigKey}:{nameof(GraphQLPlaygroundOptions.Enable)}"); + return Configuration.GetValue($"{ConfigKeys.GraphQlPlayground}:{nameof(GraphQLPlaygroundOptions.Enable)}"); } } @@ -72,9 +69,9 @@ public void Initialize(IServiceCollection serviceCollection) serviceCollection.AddAutoMapper(ModuleInfo.Assembly); - serviceCollection.Configure(Configuration.GetSection(_graphQlPlaygroundConfigKey)); - serviceCollection.Configure(Configuration.GetSection(_graphQlWebSocketConfigKey)); - serviceCollection.Configure(Configuration.GetSection(_storesConfigKey)); + serviceCollection.Configure(Configuration.GetSection(ConfigKeys.GraphQlPlayground)); + serviceCollection.Configure(Configuration.GetSection(ConfigKeys.GraphQlWebSocket)); + serviceCollection.Configure(Configuration.GetSection(ConfigKeys.Stores)); } public void PostInitialize(IApplicationBuilder appBuilder) @@ -88,13 +85,8 @@ public void PostInitialize(IApplicationBuilder appBuilder) appBuilder.UseGraphQLWebSockets(); // add http for Schema at default url /graphql - appBuilder.UseGraphQL(); - - if (IsSchemaIntrospectionEnabled) - { - // Use GraphQL Playground at default URL /ui/playground - appBuilder.UseGraphQLPlayground(); - } + // use GraphQL Playground at default URL /ui/playground + appBuilder.UseSchemaGraphQL(Configuration); // settings var settingsRegistrar = serviceProvider.GetRequiredService(); diff --git a/src/VirtoCommerce.Xapi.Web/VirtoCommerce.Xapi.Web.csproj b/src/VirtoCommerce.Xapi.Web/VirtoCommerce.Xapi.Web.csproj index 4ab9951..bea56c9 100644 --- a/src/VirtoCommerce.Xapi.Web/VirtoCommerce.Xapi.Web.csproj +++ b/src/VirtoCommerce.Xapi.Web/VirtoCommerce.Xapi.Web.csproj @@ -8,7 +8,6 @@ Library - From 5d19392ec1fd57649d63e1511093b4fbe8ca9e73 Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Tue, 19 Nov 2024 20:15:35 +0200 Subject: [PATCH 4/5] refactor --- .../Infrastructure/ScopedSchemaFactory.cs | 2 +- src/VirtoCommerce.Xapi.Web/Module.cs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs b/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs index abd2e31..ba96788 100644 --- a/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs +++ b/src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs @@ -19,7 +19,7 @@ protected override List GetSchemaBuilders() { var schemaBuilders = base.GetSchemaBuilders(); - // find all builders with inside this project + // find all builders inside this assembly var currentAssembly = typeof(TMarker).Assembly; var subSchemaBuilders = schemaBuilders diff --git a/src/VirtoCommerce.Xapi.Web/Module.cs b/src/VirtoCommerce.Xapi.Web/Module.cs index 06083d9..2f32290 100644 --- a/src/VirtoCommerce.Xapi.Web/Module.cs +++ b/src/VirtoCommerce.Xapi.Web/Module.cs @@ -8,7 +8,6 @@ using VirtoCommerce.Platform.Core.Modularity; using VirtoCommerce.Platform.Core.Settings; using VirtoCommerce.TaxModule.Core.Model; -using VirtoCommerce.Xapi.Core; using VirtoCommerce.Xapi.Core.Extensions; using VirtoCommerce.Xapi.Core.Infrastructure; using VirtoCommerce.Xapi.Core.Infrastructure.Validation; @@ -90,8 +89,8 @@ public void PostInitialize(IApplicationBuilder appBuilder) // settings var settingsRegistrar = serviceProvider.GetRequiredService(); - settingsRegistrar.RegisterSettings(ModuleConstants.Settings.General.AllSettings, ModuleInfo.Id); - settingsRegistrar.RegisterSettingsForType(ModuleConstants.Settings.StoreLevelSettings, nameof(Store)); + settingsRegistrar.RegisterSettings(Settings.General.AllSettings, ModuleInfo.Id); + settingsRegistrar.RegisterSettingsForType(Settings.StoreLevelSettings, nameof(Store)); } public void Uninstall() From 299b291a1632f26da132df5570bcd394630cddee Mon Sep 17 00:00:00 2001 From: Konstantin Savosteev Date: Wed, 20 Nov 2024 10:18:54 +0200 Subject: [PATCH 5/5] refactor --- .../Extensions/ApplicationBuilderExtensions.cs | 13 ++----------- src/VirtoCommerce.Xapi.Web/Module.cs | 2 +- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs b/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs index d20a9b5..c352258 100644 --- a/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs +++ b/src/VirtoCommerce.Xapi.Core/Extensions/ApplicationBuilderExtensions.cs @@ -1,15 +1,12 @@ using GraphQL.Server.Ui.Playground; using GraphQL.Types; using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Configuration; -using VirtoCommerce.Xapi.Core.Models; -using static VirtoCommerce.Xapi.Core.ModuleConstants; namespace VirtoCommerce.Xapi.Core.Extensions; public static class ApplicationBuilderExtensions { - public static IApplicationBuilder UseSchemaGraphQL(this IApplicationBuilder builder, IConfiguration configuration = null, string schemaPath = null) + public static IApplicationBuilder UseSchemaGraphQL(this IApplicationBuilder builder, bool schemaIntrospectionEnabled = true, string schemaPath = null) where TSchema : ISchema { var graphQlPath = "/graphql"; @@ -24,14 +21,8 @@ public static IApplicationBuilder UseSchemaGraphQL(this IApplicationBui playgroundPath = $"{playgroundPath}/{schemaPath}"; } - var isSchemaIntrospectionEnabled = false; - if (configuration != null) - { - isSchemaIntrospectionEnabled = configuration.GetValue($"{ConfigKeys.GraphQlPlayground}:{nameof(GraphQLPlaygroundOptions.Enable)}"); - } - builder.UseGraphQL(path: graphQlPath); - if (isSchemaIntrospectionEnabled) + if (schemaIntrospectionEnabled) { builder.UseGraphQLPlayground(new PlaygroundOptions { diff --git a/src/VirtoCommerce.Xapi.Web/Module.cs b/src/VirtoCommerce.Xapi.Web/Module.cs index 2f32290..7d39165 100644 --- a/src/VirtoCommerce.Xapi.Web/Module.cs +++ b/src/VirtoCommerce.Xapi.Web/Module.cs @@ -85,7 +85,7 @@ public void PostInitialize(IApplicationBuilder appBuilder) // add http for Schema at default url /graphql // use GraphQL Playground at default URL /ui/playground - appBuilder.UseSchemaGraphQL(Configuration); + appBuilder.UseSchemaGraphQL(IsSchemaIntrospectionEnabled); // settings var settingsRegistrar = serviceProvider.GetRequiredService();