[英]GraphQL Parallel async queries c# EF Core 3.0
我正在尝试使用 2 个(或更多)异步子查询执行查询。 遗憾的是,这是不可能的,因为DbContext
只能处理 1 个请求。
该项目正在使用:
错误
在前一个操作完成之前,在此上下文中启动了第二个操作。 这通常是由使用相同 DbContext 实例的不同线程引起的。
类型:
Field<ListGraphType<A>>()
.Name("A")
.Description("query of A")
.ResolveAsync(async context =>
await repro.GetAllAAsync(context)
);
Field<ListGraphType<A>>()
.Name("B")
.Description("query of B")
.ResolveAsync(async context =>
await repro.GetAllBAsync(context)
);
询问:
query {
A {
id
}
B {
id
}
}
DbContextPool
没有解决问题,我不想使用 StructureMap。
有没有优雅的使用方式
我最近创建了一个解决 EF 并行执行的项目。 你可以在这里看到实现
https://github.com/fenomeno83/graphql-dotnet-globalization-demo
特别是,您可以在这里找到一个查询示例:
public class TestGroupQueries : ObjectGraphType
{
public TestGroupQueries(IHttpContextAccessor _httpContextAccessor)
{
Name = "testQueries";
FieldAsync<TestResponseType>(
"demoQuery",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IntGraphType>> { Name = "input" }),
resolve: async context =>
{
//extension method that creates scope (IServiceScope) from IServiceProvider
using (var scope = _httpContextAccessor.CreateScope())
{
//call DemoQuery method of TestService; GetService is an extension method that use GetRequiredService from scope serviceprovider
return await scope.GetService<ITestService>().DemoQuery(context.GetArgument<int>("input"));
}
});
}
您在解析器中创建一个新范围并使用此范围的服务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.