Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using EF-Core with FieldAsync and dep injection #204

Closed
S0PEX opened this issue Feb 23, 2019 · 5 comments
Closed

Using EF-Core with FieldAsync and dep injection #204

S0PEX opened this issue Feb 23, 2019 · 5 comments
Labels
question Further information is requested

Comments

@S0PEX
Copy link

S0PEX commented Feb 23, 2019

Hey,

we are currently working with GrapQL and Asp.Net Core at our University. And I ran into an issue when I dependency inject the DatabaseContext into my query and then use async fields. When I try to query something ef core throws an exception that the current connection is already used. My guess would be that the fields are evaluated at the same time and they all share the same injected ef context.
Changing the fields to non-async ones fixes the issue as expected.
I am aware that this issue is probably related to the internal workings of the EF but I was wondering if somebody experienced the same issue was able to fix this behavior. Or if somebody has a clue how to fix it.

Regards Artur

@KreativJos
Copy link

KreativJos commented Feb 25, 2019

This should not be a problem, DI should work with async fields if async / await are used correctly. We have this in development currently, but it seems to be working fine with queries and mutations.

Are you injecting the DbContext into the classes that extend ObjectGraphType? We do it via the userContext (insert the ServiceProvider from the httpContext into the user context class in the userContextBuilder) and then call 'Services.GetService()'.

@S0PEX
Copy link
Author

S0PEX commented Feb 25, 2019

This should not be a problem, DI should work with async fields if async / await are used correctly. We have this in development currently, but it seems to be working fine with queries and mutations.

Are you injecting the DbContext into the classes that extend ObjectGraphType? We do it via the userContext (insert the ServiceProvider from the httpContext into the user context class in the userContextBuilder) and then call 'Services.GetService()'.

Hey I was directly injecting it into the query like this:

public RootQuery(PanelContext panelContext)
 {
    // User and Users
    FieldAsync<UserType>(
        "user",
        arguments: new QueryArguments(
            new QueryArgument<IdGraphType> {Name = "id", Description = "The ID of the user."}),
        resolve: async context =>
        {
            var id = context.GetArgument<int>("id");
            var user = await panelContext.Users.AsNoTracking()
                            .FirstOrDefaultAsync(x => x.Id == id);
            return user;
        });
    FieldAsync<ListGraphType<UserType>>(
        "users",
        resolve: async context => await panelContext.Users.AsNoTracking().ToListAsync()
        );
}

When I access it through the service as you mentioned it seems to work without any issues.

@KreativJos
Copy link

Injecting it into the query probably will not work indeed, as it is resolved by the DI as a Singleton and the DbContext Scoped (I hope you do not use a Singleton for a DbContext).

@medeirosmarcus
Copy link

You can check this issue:
graphql-dotnet/graphql-dotnet#648

@sungam3r
Copy link
Member

Yes, @S0PEX you can find a comprehensive answer for your question in graphql-dotnet/graphql-dotnet#648

@sungam3r sungam3r added the question Further information is requested label Sep 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants