My Product
entity consists of:
- templateId
- parameterIds
- categoryIds
I am getting 100 products in one batch.
I've created data loaders and resolvers for each of references. Now I get template, parameters and categories in three single calls (get categories, then get parameters and at the end get product template). The problem is that I see in the logs, that all requests are one after one.
I would like to do something like this await
Task.WhenAll(getTemplatesTask, getParametersTask, getCategoriesTask)
in order to save some time.
Is it possible in hot chocolate integration with mongodb? It causes that it is slower than my current REST endpoint.
EDIT: This is part of my code:
public class ProductType : ObjectType<ProductDocument>
{
protected override void Configure(IObjectTypeDescriptor<ProductDocument> descriptor)
{
descriptor.BindFieldsExplicitly();
descriptor.Name("Product");
descriptor.Field("productTemplate")
.Type<ProductTemplateType>()
.ResolveWith<ProductTemplatesResolver>(r => r.GetProductTemplate(default!, default!, default));
descriptor.Field("categories")
.Type<ListType<CategoryType>>()
.ResolveWith<CategoriesResolver>(...); //Resolver use GroupedDataLoader
descriptor.Field("parameters")
.Type<ListType<ParameterType>>()
.ResolveWith<ParametersResolver>(...); //Resolver use GroupedDataLoader
}
}
public class ProductTemplatesResolver
{
public Task<ProductTemplateDocument> GetProductTemplate([Parent] ProductDocument product,
ProductTemplatesBatchDataLoader loader,
CancellationToken cancellationToken)
{
return loader.LoadAsync(product.ProductTemplateId, cancellationToken);
}
}
public class ProductTemplatesBatchDataLoader : BatchDataLoader<int, ProductTemplateDocument>
{
private readonly ProductsDbContext _dbContext;
public ProductTemplatesBatchDataLoader(IBatchScheduler batchScheduler, ProductsDbContext dbContext, DataLoaderOptions? options = null) : base(batchScheduler, options)
{
_dbContext = dbContext;
}
protected override async Task<IReadOnlyDictionary<int, ProductTemplateDocument>> LoadBatchAsync(IReadOnlyList<int> keys, CancellationToken cancellationToken)
{
var pts = await _dbContext.ProductTemplates
.AsQueryable()
.Where(x => keys.Contains(x.Id))
.ToListAsync(cancellationToken);
return pts.ToDictionary(x => x.Id, x => x);
}
}
It executes all data loaders not in one time, but when first is finished then second is started
Thanks