Skip to content

Commit

Permalink
VCST-2241: prepare schema for partition (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Dec 5, 2024
1 parent e3bf4a5 commit 8e8a463
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using GraphQL.Server.Ui.Playground;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;

namespace VirtoCommerce.Xapi.Core.Extensions;

public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseSchemaGraphQL<TSchema>(this IApplicationBuilder builder, bool schemaIntrospectionEnabled = true, 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}";
}

builder.UseGraphQL<TSchema>(path: graphQlPath);
if (schemaIntrospectionEnabled)
{
builder.UseGraphQLPlayground(new PlaygroundOptions
{
GraphQLEndPoint = graphQlPath,
},
path: playgroundPath);
}

return builder;
}
}
33 changes: 25 additions & 8 deletions src/VirtoCommerce.Xapi.Core/Infrastructure/SchemaFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GraphQL;
using GraphQL.Conversion;
using GraphQL.Instrumentation;
Expand Down Expand Up @@ -61,15 +62,10 @@ public SchemaFactory(IEnumerable<ISchemaBuilder> 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);

foreach (var builder in _schemaBuilders)
var schemaBuilders = GetSchemaBuilders();
foreach (var builder in schemaBuilders)
{
builder.Build(schema);
}
Expand Down Expand Up @@ -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<ISchemaBuilder> GetSchemaBuilders()
{
return _schemaBuilders.ToList();
}

public void Initialize()
{
_schema.Value.Initialize();
Expand Down
32 changes: 32 additions & 0 deletions src/VirtoCommerce.Xapi.Core/Infrastructure/ScopedSchemaFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GraphQL.Introspection;

namespace VirtoCommerce.Xapi.Core.Infrastructure
{
public class ScopedSchemaFactory<TMarker> : SchemaFactory
{
public ScopedSchemaFactory(
IEnumerable<ISchemaBuilder> schemaBuilders,
IServiceProvider services,
ISchemaFilter schemaFilter)
: base(schemaBuilders, services, schemaFilter)
{
}

protected override List<ISchemaBuilder> GetSchemaBuilders()
{
var schemaBuilders = base.GetSchemaBuilders();

// find all builders inside this assembly
var currentAssembly = typeof(TMarker).Assembly;

var subSchemaBuilders = schemaBuilders
.Where(p => p.GetType().Assembly == currentAssembly)
.ToList();

return subSchemaBuilders;
}
}
}
7 changes: 7 additions & 0 deletions src/VirtoCommerce.Xapi.Core/ModuleConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/VirtoCommerce.Xapi.Core/VirtoCommerce.Xapi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="GraphQL" Version="4.6.0" />
<PackageReference Include="GraphQL.Authorization" Version="4.0.0" />
<PackageReference Include="GraphQL.Relay" Version="0.6.2" />
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="5.0.2" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore" Version="5.0.2" />
<PackageReference Include="GraphQL.Server.Transports.AspNetCore.NewtonsoftJson" Version="5.0.2" />
<PackageReference Include="PipelineNet" Version="0.9.0" />
Expand Down
27 changes: 9 additions & 18 deletions src/VirtoCommerce.Xapi.Web/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
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;
using VirtoCommerce.Xapi.Core.Models;
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
{
Expand All @@ -24,15 +24,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<bool>($"{_graphQlPlaygroundConfigKey}:{nameof(GraphQLPlaygroundOptions.Enable)}");
return Configuration.GetValue<bool>($"{ConfigKeys.GraphQlPlayground}:{nameof(GraphQLPlaygroundOptions.Enable)}");
}
}

Expand Down Expand Up @@ -72,9 +68,9 @@ public void Initialize(IServiceCollection serviceCollection)

serviceCollection.AddAutoMapper(ModuleInfo.Assembly);

serviceCollection.Configure<GraphQLPlaygroundOptions>(Configuration.GetSection(_graphQlPlaygroundConfigKey));
serviceCollection.Configure<GraphQLWebSocketOptions>(Configuration.GetSection(_graphQlWebSocketConfigKey));
serviceCollection.Configure<StoresOptions>(Configuration.GetSection(_storesConfigKey));
serviceCollection.Configure<GraphQLPlaygroundOptions>(Configuration.GetSection(ConfigKeys.GraphQlPlayground));
serviceCollection.Configure<GraphQLWebSocketOptions>(Configuration.GetSection(ConfigKeys.GraphQlWebSocket));
serviceCollection.Configure<StoresOptions>(Configuration.GetSection(ConfigKeys.Stores));
}

public void PostInitialize(IApplicationBuilder appBuilder)
Expand All @@ -88,18 +84,13 @@ public void PostInitialize(IApplicationBuilder appBuilder)
appBuilder.UseGraphQLWebSockets<ISchema>();

// add http for Schema at default url /graphql
appBuilder.UseGraphQL<ISchema>();

if (IsSchemaIntrospectionEnabled)
{
// Use GraphQL Playground at default URL /ui/playground
appBuilder.UseGraphQLPlayground();
}
// use GraphQL Playground at default URL /ui/playground
appBuilder.UseSchemaGraphQL<ISchema>(IsSchemaIntrospectionEnabled);

// settings
var settingsRegistrar = serviceProvider.GetRequiredService<ISettingsRegistrar>();
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()
Expand Down
1 change: 0 additions & 1 deletion src/VirtoCommerce.Xapi.Web/VirtoCommerce.Xapi.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GraphQL.Server.Ui.Playground" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 8e8a463

Please sign in to comment.