Skip to content

Question regarding conventions and the Entity Framework scoped / async issue #231

Open
@HaroldH76

Description

@HaroldH76

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();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions