-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VCST-872: add non-functional subscription (#542)
- Loading branch information
1 parent
4af42dd
commit b2dd09a
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/VirtoCommerce.ExperienceApiModule.Core/Schemas/SubscriptionSchema.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} | ||
} |