Skip to content

Commit b554241

Browse files
Fixing parallel dbcontext access error
1 parent f51a773 commit b554241

2 files changed

Lines changed: 22 additions & 29 deletions

File tree

src/Dotnet6.GraphQL4.Store.WebAPI/Graphs/Types/Products/ProductGraphType.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
using Dotnet6.GraphQL4.Store.Domain.Entities.Reviews;
66
using Dotnet6.GraphQL4.Store.WebAPI.Graphs.Types.Reviews;
77
using GraphQL.DataLoader;
8-
using GraphQL.MicrosoftDI;
98
using GraphQL.Types;
9+
using Microsoft.Extensions.DependencyInjection;
1010

1111
namespace Dotnet6.GraphQL4.Store.WebAPI.Graphs.Types.Products
1212
{
1313
public abstract class ProductGraphType<T> : ObjectGraphType<T>
14-
where T: Product
14+
where T : Product
1515
{
1616
protected ProductGraphType()
1717
{
@@ -30,19 +30,16 @@ protected ProductGraphType()
3030
Field<ProductOptionEnumGraphType>("Option");
3131

3232
Field<ListGraphType<ReviewGraphType>, IEnumerable<Review>>()
33-
.Name("Reviews")
34-
.Resolve()
35-
.WithServices<IDataLoaderContextAccessor, IProductService>()
36-
.ResolveAsync((context, dataLoader, service) =>
37-
{
38-
var loaderResult = dataLoader.Context
39-
.GetOrAddCollectionBatchLoader<Guid, Review>(
40-
loaderKey: "getLookupByProductIdsAsync",
41-
fetchFunc: service.GetLookupReviewsByProductIdsAsync)
42-
.LoadAsync(context.Source.Id);
43-
44-
return loaderResult.GetResultAsync();
45-
});
33+
.Name("reviews")
34+
.ResolveAsync(context
35+
=> context.RequestServices
36+
.GetRequiredService<IDataLoaderContextAccessor>().Context
37+
.GetOrAddCollectionBatchLoader<Guid, Review>(
38+
loaderKey: "getLookupReviewsByProductIdsAsync",
39+
fetchFunc: context.RequestServices
40+
.GetRequiredService<IProductService>()
41+
.GetLookupReviewsByProductIdsAsync)
42+
.LoadAsync(context.Source.Id));
4643
}
4744
}
4845
}

src/Dotnet6.GraphQL4.Store.WebAPI/Graphs/Types/Products/ProductInterfaceGraphType.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Dotnet6.GraphQL4.Store.WebAPI.Graphs.Types.Products.Kayaks;
1212
using Dotnet6.GraphQL4.Store.WebAPI.Graphs.Types.Reviews;
1313
using GraphQL.DataLoader;
14-
using GraphQL.MicrosoftDI;
1514
using GraphQL.Types;
1615
using Microsoft.Extensions.DependencyInjection;
1716

@@ -35,19 +34,16 @@ public ProductInterfaceGraphType(IServiceProvider serviceProvider)
3534
Field<ProductOptionEnumGraphType>("option");
3635

3736
Field<ListGraphType<ReviewGraphType>, IEnumerable<Review>>()
38-
.Name("Reviews")
39-
.Resolve()
40-
.WithServices<IDataLoaderContextAccessor, IProductService>()
41-
.ResolveAsync((context, dataLoader, service) =>
42-
{
43-
var loaderResult = dataLoader.Context
44-
.GetOrAddCollectionBatchLoader<Guid, Review>(
45-
loaderKey: "getLookupByProductIdsAsync",
46-
fetchFunc: service.GetLookupReviewsByProductIdsAsync)
47-
.LoadAsync(context.Source.Id);
48-
49-
return loaderResult.GetResultAsync();
50-
});
37+
.Name("reviews")
38+
.ResolveAsync(context
39+
=> context.RequestServices
40+
.GetRequiredService<IDataLoaderContextAccessor>().Context
41+
.GetOrAddCollectionBatchLoader<Guid, Review>(
42+
loaderKey: "getLookupReviewsByProductIdsAsync",
43+
fetchFunc: context.RequestServices
44+
.GetRequiredService<IProductService>()
45+
.GetLookupReviewsByProductIdsAsync)
46+
.LoadAsync(context.Source.Id));
5147

5248
ResolveType = @object =>
5349
{

0 commit comments

Comments
 (0)