conventions icon indicating copy to clipboard operation
conventions copied to clipboard

Question regarding conventions and the Entity Framework scoped / async issue

Open HaroldH76 opened this issue 3 years ago • 0 comments

We use GraphQL.Conventions and we have the issue that is described over here: https://github.com/graphql-dotnet/graphql-dotnet/blob/master/docs2/site/docs/guides/known-issues.md#entity-framework-concurrency-issues

We would like to apply this suggestion:

Finally, you can create a scope within each field resolver that relies on Entity Framework or your other scoped services. Please see the section on this in the dependency injection documentation.

Are there any tips / examples / guidelines how this can be done with conventions and the inject attribute?

So basically what we need is that in each field we can create a new scope and derive our dependencies from there. Currently we fixed it in the schema this way:

async Task<MyContent> Test([Inject] IServiceScopeFactory serviceScopeFactory)
{ 
  using var scope = serviceScopeFactory.CreateScope();
  var database = scope.GetService<MyDbContext>();
  return await database.MyContents.FirstAsync();
}

But would be nice if we can somehow change GraphQL conventions so that we can do this:

async Task<MyContent> Test([Inject] MyDbContext myDbContext)
{ 
  return await myDbContext.MyContents.FirstAsync();
}

HaroldH76 avatar May 18 '22 11:05 HaroldH76