'GraphQL .NET parallel execution strategy doesn't fully run in parallel
I have a graphQL query where I have 10 parallel queries + 2 properties on the second level. This results in 12 queries (10 queries + 2 batched properties).
{
entries0(pageSize: 10) {
id
project {
id
}
partner {
id
}
}
entries1(pageSize: 10) {
id
project {
id
}
partner {
id
}
}
}
This is when I forced a serial execution strategy:
And this is when I use the parallel execution strategy:
As you can see, concurrent DB requests do happen now, but they do not start at the same time. What could be the cause of this?
I use GraphQL.NET 4.7.1 with .NET 6.0 and EF Core 6.0.2.
This is the query (duplicated 10 times: entries0, entries1, etc...):
Field<ListGraphType<Entry>, List<EntryEntity>>()
.Name("entries0")
.Argument<NonNullGraphType<IntGraphType>>("pageSize")
.ResolveScopedAsync(ctx =>
{
int pageSize = ctx.GetArgument<int>("pageSize");
var manager = ctx.RequestServices.GetRequiredService<ITestManager>();
return manager.GetMultiple(pageSize);
});
And this is the code from the manager
public Task<List<EntryEntity>> GetMultiple(int take)
{
return _dbContext.EntryEntities
.OrderByDescending(x => x.CreatedOn)
.Take(take)
.ToListAsync();
}
So as I see it the query does execute in parallel, but I don't see why it doesn't fully run in parallel. Each dbContext runs as separate instance in its own scope thanks to ResolveScopedAsync.
Solution 1:[1]
use FieldAsync instead of Field and await inside ResolveScopedAsync and asynce/await inside GetMultiple(int take). Anywhere you use asynce, you should return await to work Asynchronously. if you forget await, it works synchronously.
FieldAsync<ListGraphType<Entry>, List<EntryEntity>>()
.Name("entries0")
.Argument<NonNullGraphType<IntGraphType>>("pageSize")
.ResolveScopedAsync(ctx =>
{
// your code
return await manager.GetMultiple(pageSize);
});
public async Task<List<EntryEntity>> GetMultiple(int take)
{
return await _dbContext.EntryEntities
.OrderByDescending(x => x.CreatedOn)
.Take(take)
.ToListAsync();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


