Skip to content

Adding computed fields (Entity Framework) #2365

@aradalvand

Description

@aradalvand

Hi there, thank you for this great framework!
I'm intending to use Hot Chocolate with Entity Framework, and I faced a scenario which I would say is pretty common, but I realized it's apparently not possible to implement with Hot Chocolate.

It's very straight-forward. Let's say you have a Book domain class, and a Rating domain class, and there is a one-to-many relationship between these two. I think it's pretty self-explanatory so far.

Now, I want to add a field on my Book type called rating, which is obviously the average of all the ratings given to a particular book.
I thought this would work (this is in the Configure method of the book schema type, as you know)

descriptor.Field(book => book.Ratings.Average(r => r.Rate)).Name("rating");

But it doesn't, and when I query the rating field, I get an Unexpected Execution Error, and it says the Ratings property is null (and I'm aware that this is because it hasn't been fetched from the database).

Any guidance on how I should implement scenarios like this would be highly appreciated.
Also, I should mention that since I don't want to face the n+1 problem, I don't want Hot Chocolate to execute book.Ratings.Average(r => r.Rate) separately after it retrieved the book from the database, but preferably it should do this in the single query it creates as well.

Metadata

Metadata

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions