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

many-to-many dataloader + selection + pagination empty result #7615

Open
PHILLIPS71 opened this issue Oct 17, 2024 · 0 comments
Open

many-to-many dataloader + selection + pagination empty result #7615

PHILLIPS71 opened this issue Oct 17, 2024 · 0 comments

Comments

@PHILLIPS71
Copy link
Contributor

PHILLIPS71 commented Oct 17, 2024

Product

Hot Chocolate

Version

14.0.0

Link to minimal reproduction

https://github.com/PHILLIPS71/hc-7615

Steps to reproduce

I have a many-to-many relationship between User and Location, and I’m using EF Core to manage the bridging table between them.

I’ve been trying to create a DataLoader that uses the ISelectorBuilder to project locations as a paged result when querying users, with some help from Michael in the Slack channel.

However, when I run it with the selection commented out, it returns an empty collection. But when I uncomment it, I get an error due to the dynamic type.

[DataLoader]
internal static ValueTask<Dictionary<Guid, Page<Location>>> GetLocationsByUserIdAsync(
    IReadOnlyList<Guid> keys,
    PagingArguments paging,
    ISelectorBuilder selector,
    ApplicationDbContext database,
    CancellationToken cancellation = default)
{
    return database
        .Users
        .AsNoTracking()
        .Where(x => keys.Contains(x.Id))
        .SelectMany(x => x.Locations.Select(l => new { Id = l.Id, Location = l }))
        .OrderBy(x => x.Location.Name)
        // uncommenting this will result is type errors
        // .Select(x => x.Id, x => x.Location, selector)
        .ToBatchPageAsync(x => x.Id, x => x.Location, paging, cancellation);
 }

If you execute the following query using the reproduction repository you will encounter and be able to reproduce the issue:

{
  user(take: 5) {
    id
    name
    locations(take: 5) {
      nodes {
        id
        name
      }
    }
  }
}

What is expected?

I'm looking for a way to use the new ISelectorBuilder with many-to-many relationships without having to manually introduce bridging tables, leaving that responsibility to EF Core instead.

What is actually happening?

If you're not using the ISelectorBuilder, you'll get an empty result. Trying to use the .Select function will cause the issue below due to the required dynamic type.

Relevant log output

The type arguments for method 'IQueryable<KeyValueResult<TKey,IEnumerable<TValue>>> GreenDonut.Selectors.SelectionDataLoaderExtensions.Select<T,TKey,TValue>(this IQueryable<T>, Expression<Func<T,TKey?>>, Expression<Func<T,IEnumerable<TValue>>>, ISelectorBuilder)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants