mirror of
https://github.com/DrMboga/WotBlitzStatisticsBlazor.git
synced 2026-10-23 11:20:05 +02:00
Some Data loader stuff
This commit is contained in:
1 parent
d643c323b3
commit
83628d21bd
5 files changed
+61
-14
No files matched your search
@@ -0,0 +1,32 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using GreenDonut;
|
||||
using WotBlitzStatisticsPro.WgApiClient;
|
||||
using WotBlitzStatisticsPro.WgApiClient.Model;
|
||||
|
||||
namespace WotBlitzStatistics.GraphQl.DataLoaders
|
||||
{
|
||||
public class ClanDataLoader : DataLoaderBase<long, ClanInfo>
|
||||
{
|
||||
private readonly IWargamingApiClient _wgApiClient;
|
||||
|
||||
public ClanDataLoader(IWargamingApiClient wgApiClient)
|
||||
{
|
||||
_wgApiClient = wgApiClient;
|
||||
}
|
||||
|
||||
protected override async Task<IReadOnlyList<Result<ClanInfo>>> FetchAsync(
|
||||
IReadOnlyList<long> keys,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new List<Result<ClanInfo>>();
|
||||
foreach (var key in keys)
|
||||
{
|
||||
result.Add(await _wgApiClient.GetClanInfo(key));
|
||||
}
|
||||
|
||||
return result.AsReadOnly();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using GreenDonut;
|
||||
using HotChocolate.Resolvers;
|
||||
using HotChocolate.Types;
|
||||
using WotBlitzStatistics.GraphQl.Resolvers;
|
||||
using WotBlitzStatisticsPro.WgApiClient;
|
||||
using WotBlitzStatisticsPro.WgApiClient.Model;
|
||||
|
||||
@@ -11,17 +12,7 @@ namespace WotBlitzStatistics.GraphQl.ObjectTypes
|
||||
{
|
||||
protected override void Configure(IObjectTypeDescriptor<ClanAccountInfo> descriptor)
|
||||
{
|
||||
// ToDo: Cache clan info!
|
||||
// https://hotchocolate.io/docs/dataloaders
|
||||
descriptor.Field("clanInfo")
|
||||
.Resolver(c =>
|
||||
{
|
||||
var warGamingClient = c.Service<IWargamingApiClient>();
|
||||
var clanDataLoader = c.CacheDataLoader<long, ClanInfo>(
|
||||
"clanInfoById",
|
||||
clanId => warGamingClient.GetClanInfo(clanId));
|
||||
return clanDataLoader.LoadAsync(c.Parent<ClanAccountInfo>().ClanId.Value);
|
||||
});
|
||||
descriptor.Include<ClanInfoResolver>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using HotChocolate;
|
||||
using WotBlitzStatistics.GraphQl.DataLoaders;
|
||||
using WotBlitzStatisticsPro.WgApiClient;
|
||||
using WotBlitzStatisticsPro.WgApiClient.Model;
|
||||
|
||||
namespace WotBlitzStatistics.GraphQl.Resolvers
|
||||
{
|
||||
[GraphQLResolverOf(typeof(ClanInfo))]
|
||||
public class ClanInfoResolver
|
||||
{
|
||||
public async Task<ClanInfo> GetClanInfo(
|
||||
[Parent] ClanAccountInfo clanAccountInfo,
|
||||
[DataLoader] ClanDataLoader clanDataLoader)
|
||||
{
|
||||
return (await clanDataLoader.LoadAsync(new[] {clanAccountInfo.ClanId.Value}, CancellationToken.None))[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using HotChocolate;
|
||||
using HotChocolate.AspNetCore;
|
||||
using HotChocolate.AspNetCore.Voyager;
|
||||
using HotChocolate.DataLoader;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@@ -30,8 +31,10 @@ namespace WotBlitzStatistics.GraphQl
|
||||
services.AddSingleton<IWargamingApiSettings>(wgApiConfig);
|
||||
services.AddHttpClient<IWargamingApiClient, WargamingApiClient>();
|
||||
|
||||
// this enables you to use DataLoader in your resolvers.
|
||||
services.AddDataLoaderRegistry();
|
||||
// Global data loader - caches data between requests
|
||||
//services.AddSingleton<IDataLoaderRegistry, DataLoaderRegistry>();
|
||||
// this enables you to use DataLoader in your resolvers.
|
||||
services.AddDataLoaderRegistry();
|
||||
|
||||
// Add GraphQL Services
|
||||
services.AddGraphQL(sp => SchemaBuilder.New()
|
||||
|
||||
@@ -55,7 +55,6 @@ namespace WotBlitzStatisticsPro.WgApiClient
|
||||
RealmType realmType = RealmType.Ru,
|
||||
RequestLanguage language = RequestLanguage.En)
|
||||
{
|
||||
// https://api.wotblitz.ru/wotb/clans/info/?application_id=adc1387489cf9fc8d9a1d85dbd27763d&clan_id=40493
|
||||
var clanInfo = await GetFromBlitzApi<Dictionary<string, ClanInfo>>(
|
||||
realmType,
|
||||
language,
|
||||
@@ -68,5 +67,7 @@ namespace WotBlitzStatisticsPro.WgApiClient
|
||||
return null;
|
||||
}
|
||||
|
||||
// https://api.wotblitz.ru/wotb/encyclopedia/achievements/?application_id=adc1387489cf9fc8d9a1d85dbd27763d
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user