Files
graphql-test/Presentation/GraphQL/DataLoaders/KillsDataLoader.cs
2025-11-26 16:50:06 -03:00

28 lines
1.0 KiB
C#

using Application.Interfaces.Services;
using Domain.Entities;
using GreenDonut.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace GraphQLTEST.GraphQL.DataLoaders;
public class KillsByUserDataLoader : GroupedDataLoader<int, Kill>{
private readonly IServiceProvider _serviceProvider;
public KillsByUserDataLoader(
IServiceProvider serviceProvider,
IBatchScheduler batchScheduler,
DataLoaderOptions options): base(batchScheduler, options){
_serviceProvider = serviceProvider;
}
protected override async Task<ILookup<int, Kill>> LoadGroupedBatchAsync(IReadOnlyList<int> keys, CancellationToken cancellationToken)
{
await using var scope = _serviceProvider.CreateAsyncScope();
var service = scope.ServiceProvider.GetRequiredService<IKillService>();
return await service.GetKillsByUserId(keys)
.ToAsyncEnumerable()
.ToLookupAsync(k => k.UserId, cancellationToken: cancellationToken);
}
}