ChilliCream / graphql-workshop

Getting started with GraphQL on ASP.NET Core and Hot Chocolate - Workshop
465 stars 199 forks source link

Track queries throws DbContext exceptions with paginations #23

Closed geraldmaale closed 1 month ago

geraldmaale commented 3 years ago

I have been battling with the Track queries with pagination in Session 6 for several hours without success. Later I got to realize that the Field descriptor for Sessions needed to be provided with "UseDbContext" middleware in the TrackType.

However the tutorials lack that middleware, is it an error or I was not doing it right.

image

macke800 commented 3 years ago

I think that I have bumped into the same error:

The specified key `ConferencePlanner.GraphQL.Data.ApplicationDbContext` does not exist on `context.ScopedContextData`

... and also wonder what is the right solution. In the session 6 example code the DI of ApplicationDbContext is handled differently in the different <Name>Type.cs-files. For example in the SpeakerType.cs it does add the ApplicationDbContext in the chain of middlewares by doing what you do @geraldmaale, by adding: .UseDbContext<ApplicationDbContext>() ... that seems to work fine. I can trigger the same problem you have in session 6 by running the query:

mutation AddSession {
  addSession(input: {
    speakerIds: ["U3BlYWtlcgppMw=="]
    title: "Session #1"
  }) {
    session {
      speakers {
        name
        id
      }
    }
  }
}

The session part will trigger the code in SessionType.cs:

descriptor
    .Field(t => t.SessionSpeakers)
    .ResolveWith<SessionResolvers>(t => t.GetSpeakersAsync(default!, default!, default!, default))
    .Name("speakers");

and in that file [UseApplicationDbContext] is used in the SessionResolvers private class however that does not seem to work. The only way I get it to work is with the solution of adding .UseDbContext<ApplicationDbContext>() on each descriptor that use the SessionResolvers-functions (that needs ApplicationDbContext). This is the solution used in the same example code for SpeakerType.cs.

I am more then happy to aid with a PR if the solution is correct and .UseDbContext<ApplicationDbContext>() should be used.

Would be good for me at least to understand why adding [UseApplicationDbContext] do not work, such as used on the public async Task<IEnumerable<Speaker>> GetSpeakersAsync in the SessionType.cs. I thought that that would be the same result as by adding .UseDbContext<ApplicationDbContext>() explicitly.

Thanks!

gojanpaolo commented 3 years ago

It looks like adding UseDbContext is the expected solution as that's what is done in the complete code

https://github.com/ChilliCream/graphql-workshop/blob/c07b36885d7e2a1822171c8a1c2bfad393cf946e/code/complete/GraphQL/Types/TrackType.cs#L23-L28

https://github.com/ChilliCream/graphql-workshop/blob/c07b36885d7e2a1822171c8a1c2bfad393cf946e/code/complete/GraphQL/Types/SessionType.cs#L24-L28

I think we could start with fixing the session code which would be especially helpful for new devs that will try the workshop. @macke800 Are you still available to do a PR for that?

I'm also curious why [UseApplicationDbContext] doesn't work. @michaelstaib Would you be able to shed some light on @macke800's question?

Would be good for me at least to understand why adding [UseApplicationDbContext] do not work, such as used on the public async Task<IEnumerable<Speaker>> GetSpeakersAsync in the SessionType.cs. I thought that that would be the same result as by adding .UseDbContext<ApplicationDbContext>() explicitly.

macke800 commented 3 years ago

@gojanpaolo, absolutely, more then happy if I can help. It may take a day or two.

macke800 commented 3 years ago

Pull request created: https://github.com/ChilliCream/graphql-workshop/pull/43 Not sure how to ask for review, but please let me know if something is not right. I have compiled each changed project and all compiles and seems to work.

glen-84 commented 1 month ago

Fixed in #43.