Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/VirtoCommerce.Xapi.Core/BaseQueries/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ public abstract class CommandBuilder<TCommand, TResult, TCommandGraphType, TResu
{
private const string _argumentName = "command";

protected CommandBuilder(IAuthorizationService authorizationService)
: base(authorizationService)
{
}

[Obsolete("Use the constructor without IMediator. The mediator is resolved from context.RequestServices per request.", DiagnosticId = "VC0015", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
protected CommandBuilder(IMediator mediator, IAuthorizationService authorizationService)
: base(mediator, authorizationService)
: this(authorizationService)
{
}

Expand Down Expand Up @@ -47,8 +53,14 @@ public abstract class CommandBuilder<TCommand, TResult, TResultGraphType>
where TCommand : IRequest<TResult>
where TResultGraphType : IGraphType
{
protected CommandBuilder(IAuthorizationService authorizationService)
: base(authorizationService)
{
}

[Obsolete("Use the constructor without IMediator. The mediator is resolved from context.RequestServices per request.", DiagnosticId = "VC0015", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
protected CommandBuilder(IMediator mediator, IAuthorizationService authorizationService)
: base(mediator, authorizationService)
: this(authorizationService)
{
}

Expand All @@ -60,7 +72,7 @@ public override void Build(ISchema schema)
protected override IEnumerable<QueryArgument> GetArguments()
{
// no arguments needed for this type of command builder
return Array.Empty<QueryArgument>();
return [];
}

protected override TCommand GetRequest(IResolveFieldContext<object> context)
Expand Down
9 changes: 8 additions & 1 deletion src/VirtoCommerce.Xapi.Core/BaseQueries/QueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using GraphQL;
using GraphQL.Types;
Expand All @@ -13,8 +14,14 @@ public abstract class QueryBuilder<TQuery, TResult, TResultGraphType>
where TQuery : IQuery<TResult>, IExtendableQuery, IHasArguments
where TResultGraphType : IGraphType
{
protected QueryBuilder(IAuthorizationService authorizationService)
: base(authorizationService)
{
}

[Obsolete("Use the constructor without IMediator. The mediator is resolved from context.RequestServices per request.", DiagnosticId = "VC0015", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
protected QueryBuilder(IMediator mediator, IAuthorizationService authorizationService)
: base(mediator, authorizationService)
: this(authorizationService)
{
}

Expand Down
17 changes: 11 additions & 6 deletions src/VirtoCommerce.Xapi.Core/BaseQueries/RequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using GraphQL;
Expand All @@ -16,17 +17,19 @@ public abstract class RequestBuilder<TRequest, TResponse, TResponseGraphType> :
where TRequest : IRequest<TResponse>
where TResponseGraphType : IGraphType
{
private readonly IMediator _mediator;
private readonly IAuthorizationService _authorizationService;

protected abstract string Name { get; }

protected RequestBuilder(
IMediator mediator,
IAuthorizationService authorizationService)
protected RequestBuilder(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
_mediator = mediator;
}

[Obsolete("Use the constructor without IMediator. The mediator is resolved from context.RequestServices per request.", DiagnosticId = "VC0015", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
protected RequestBuilder(IMediator mediator, IAuthorizationService authorizationService)
: this(authorizationService)
{
}

public abstract void Build(ISchema schema);
Expand All @@ -38,6 +41,7 @@ protected virtual FieldType GetFieldType()
.ResolveAsync(async context =>
{
var (_, response) = await Resolve(context);

return response;
});

Expand Down Expand Up @@ -83,7 +87,8 @@ protected virtual Task AfterMediatorSend(IResolveFieldContext<object> context, T

protected virtual async Task<TResponse> GetResponseAsync(IResolveFieldContext<object> context, TRequest request)
{
return await _mediator.Send(request);
// Not ctor-injected: builders are singletons, a ctor-captured mediator would be root-bound (see GetMediator docs).
return await context.GetMediator().Send(request);
}

protected virtual async Task Authorize(IResolveFieldContext context, object resource, IAuthorizationRequirement requirement)
Expand Down
10 changes: 9 additions & 1 deletion src/VirtoCommerce.Xapi.Core/BaseQueries/SearchQueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using GraphQL.Types;
using MediatR;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -16,8 +17,14 @@ public abstract class SearchQueryBuilder<TQuery, TResult, TItem, TItemGraphType>
{
protected virtual int DefaultPageSize => Connections.DefaultPageSize;

protected SearchQueryBuilder(IAuthorizationService authorizationService)
: base(authorizationService)
{
}

[Obsolete("Use the constructor without IMediator. The mediator is resolved from context.RequestServices per request.", DiagnosticId = "VC0015", UrlFormat = "https://docs.virtocommerce.org/products/products-virto3-versions")]
protected SearchQueryBuilder(IMediator mediator, IAuthorizationService authorizationService)
: base(mediator, authorizationService)
: this(authorizationService)
{
}

Expand All @@ -31,6 +38,7 @@ protected override FieldType GetFieldType()
builder.ResolveAsync(async context =>
{
var (query, response) = await Resolve(context);

return new PagedConnection<TItem>(response.Results, query.Skip, query.Take, response.TotalCount);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Xapi.Core.Services;

namespace VirtoCommerce.Xapi.Core.Extensions;

public static class RequestScopedCacheExtensions
{
/// <summary>
/// By-id form for items keyed by <see cref="IEntity.Id"/>: delegates to
/// <see cref="IRequestScopedCache.GetOrLoadByIdsAsync{T}(string, ICollection{string}, Func{T, string}, Func{ICollection{string}, Task{IList{T}}})"/>.
/// </summary>
public static Task<IDictionary<string, T>> GetOrLoadByIdsAsync<T>(
this IRequestScopedCache cache,
string keyPrefix,
ICollection<string> ids,
Func<ICollection<string>, Task<IList<T>>> loadMissing)
where T : class, IEntity
{
return cache.GetOrLoadByIdsAsync(keyPrefix, ids, static x => x.Id, loadMissing);
}
}
Loading
Loading