Skip to content

Commit

Permalink
VCST-872: add non-functional subscription (#542)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksavosteev authored Mar 27, 2024
1 parent 4af42dd commit b2dd09a
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Reactive.Linq;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Resolvers;
using GraphQL.Subscription;
using GraphQL.Types;
using Microsoft.AspNetCore.Authorization;
using VirtoCommerce.ExperienceApiModule.Core.Extensions;
using VirtoCommerce.ExperienceApiModule.Core.Infrastructure;
using VirtoCommerce.ExperienceApiModule.Core.Infrastructure.Authorization;
using VirtoCommerce.Platform.Security.Authorization;

namespace VirtoCommerce.ExperienceApiModule.Core.Schemas
{
public class SubscriptionSchema(IAuthorizationService authorizationService) : ISchemaBuilder
{
public void Build(ISchema schema)
{
var pingType = new EventStreamFieldType
{
Name = "ping",
Type = typeof(StringGraphType),
Resolver = new FuncFieldResolver<string>(Resolve),
AsyncSubscriber = new AsyncEventStreamResolver<string>(Subscribe),
};
schema.Subscription.AddField(pingType);
}

private string Resolve(IResolveFieldContext context)
{
return context.Source as string;
}

private async Task<IObservable<string>> Subscribe(IResolveEventStreamContext context)
{
var authorizationResult = await authorizationService.AuthorizeAsync(context.GetCurrentPrincipal(), null, new PermissionAuthorizationRequirement(string.Empty));
if (!authorizationResult.Succeeded)
{
throw AuthorizationError.Forbidden();
}

// reserved for future use
return Observable.Never<string>();
}
}
}

0 comments on commit b2dd09a

Please sign in to comment.