Skip to content

Commit

Permalink
VCST-2241: add UseScopedSchema extension (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Dec 9, 2024
1 parent 0a2bee8 commit eb9844e
Showing 1 changed file with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using GraphQL.Server.Ui.Playground;
using GraphQL.Types;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using VirtoCommerce.Xapi.Core.Infrastructure;
using VirtoCommerce.Xapi.Core.Models;

namespace VirtoCommerce.Xapi.Core.Extensions;

public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseScopedSchema<TMarker>(this IApplicationBuilder builder, string schemaPath)
{
var playgroundOptions = builder.ApplicationServices.GetService<IOptions<GraphQLPlaygroundOptions>>();

return builder.UseSchemaGraphQL<ScopedSchemaFactory<TMarker>>(playgroundOptions?.Value?.Enable ?? true, schemaPath);
}

public static IApplicationBuilder UseSchemaGraphQL<TSchema>(this IApplicationBuilder builder, bool schemaIntrospectionEnabled = true, string schemaPath = null)
where TSchema : ISchema
{
Expand All @@ -15,15 +26,15 @@ public static IApplicationBuilder UseSchemaGraphQL<TSchema>(this IApplicationBui
graphQlPath = $"{graphQlPath}/{schemaPath}";
}

var playgroundPath = "/ui/playground";
if (!string.IsNullOrEmpty(schemaPath))
{
playgroundPath = $"{playgroundPath}/{schemaPath}";
}

builder.UseGraphQL<TSchema>(path: graphQlPath);
if (schemaIntrospectionEnabled)
{
var playgroundPath = "/ui/playground";
if (!string.IsNullOrEmpty(schemaPath))
{
playgroundPath = $"{playgroundPath}/{schemaPath}";
}

builder.UseGraphQLPlayground(new PlaygroundOptions
{
GraphQLEndPoint = graphQlPath,
Expand Down

0 comments on commit eb9844e

Please sign in to comment.