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

Add an optional property parameter to DataLoaders methods. #7081

Open
orlandommb opened this issue Apr 29, 2024 · 3 comments
Open

Add an optional property parameter to DataLoaders methods. #7081

orlandommb opened this issue Apr 29, 2024 · 3 comments
Labels
🎉 enhancement New feature or request 🌶️ green donut 🔍 investigate Indicates that an issue or pull request needs more information.

Comments

@orlandommb
Copy link

Product

Green Donut

Is your feature request related to a problem?

I don't think its a problem is more of a inconvenience, if an Entity has relationships with more than one Entity, and i want to able to include those entities in my query i have to build one DataLoader for each relationship (im using EF Core) because there's no way to pass the property that i need to query for in the where clause of ef core.

For example, the models Products, Category and Company. Products has a CategoryId and CompanyId foreign keys, and Company and Category models have List Products navigation properties,

If i query for Company and want to include Products in my query i got a build a GroupedDataLoader that searches for products like:

context.Products.Where(p=> keys.Contains( p.CompanyId ))

If i query for Category and want to include Products in my query i got a build a GroupedDataLoader that searches for products like:

context.Products.Where(p=> keys.Contains( p.CategoryId ))

Right now my Products model has relationships with:

  1. Company
  2. Branch
  3. Category
  4. SubCategory
  5. Unit
  6. Tax

this means i have to make one GroupedDataLoader for each, at least thats how i know how to do it right now, don't know if theres a better way to do it.

The solution you'd like

Passing the property as a parameter in some way is the best solution that comes to my mind. I know that HotChocolate its made to work with multiple database providers but maybe with an optional parameter in the LoadBatchAsync method might work, something like Product.CategoryId.

@orlandommb orlandommb added the 🎉 enhancement New feature or request label Apr 29, 2024
@michaelstaib
Copy link
Member

@PascalSenn would the observable cache solve this?

@michaelstaib michaelstaib added the 🔍 investigate Indicates that an issue or pull request needs more information. label May 8, 2024
@PascalSenn
Copy link
Member

@michaelstaib
Observable cache would not reduce the number of data loaders, but the total number of fetches.

You still need to have a DataLoader from each direction, but once fetched, the order data loaders could subscribe to the cache and also get prefilled.

Meaning, if I load the 2 via the ProductByCompanyIdDataLoader and I get { id: 1, companyId: 2, categoryId: 3}, then a call to ProductByCategoryIdDataLoader with id 3 would already be in the cache.

On another note, the behaviour that @orlandommb describes could theoretically already done with a datalaoder that has a polymorphic key.

public record ProductKey() 
{
     public record CustomerId(Guid CustomerId) : ProductKey
     public record BranchId(Guid CustomerId) : ProductKey
     public record CategoryId(Guid CustomerId) : ProductKey
     ...
}

@PascalSenn
Copy link
Member

PascalSenn commented May 9, 2024

Thinking about it more, the observable cache would not help at all with GroupedDataLoaders, as there is no way to know if you have fetched all Products of a Category already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎉 enhancement New feature or request 🌶️ green donut 🔍 investigate Indicates that an issue or pull request needs more information.
Projects
None yet
Development

No branches or pull requests

4 participants