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

Enhance DataLoader Flexibility by Allowing Access to Filtering and Sorting Contexts or custom variable #7902

Open
ty01314 opened this issue Jan 5, 2025 · 0 comments

Comments

@ty01314
Copy link

ty01314 commented Jan 5, 2025

Product

Hot Chocolate

Is your feature request related to a problem?

Currently, applying filtering and sorting logic directly within a DataLoader is not straightforward. While we can pass PagingArguments, there's no built-in way to access the IFilterContext or a similar ISortingContext within the DataLoader's logic. This limits the ability to create truly dynamic and encapsulated data fetching strategies.

The solution you'd like

The ideal solution would be to allow IFilterContext and potentially ISortingContext (or similar context objects) to be injected as parameters into DataLoader methods. This would enable us to directly leverage the filtering and sorting capabilities provided by Hot Chocolate within the data fetching logic itself, making DataLoaders more flexible and powerful.

Current Situation:

Currently, we are limited to passing PagingArguments to DataLoader methods. Attempting to directly use IFilterContext within the resolver and then passing data to the DataLoader doesn't result in the filter being applied at the data fetching level.

Code:

[UsePaging]
[UseFiltering]
public static async Task<Connection<Session>> GetSessionsAsync(
    [Parent] Track track,
    ISessionsByTrackIdDataLoader sessionsByTrackId,
    PagingArguments pagingArguments,
    IFilterContext filter,
    ISelection selection,
    CancellationToken cancellationToken)
{
    return await sessionsByTrackId
        .Where(filter)  // <- applyed but not work
        .WithPagingArguments(pagingArguments)
        .Select(selection)
        .LoadAsync(track.Id, cancellationToken)
        .ToConnectionAsync();
}

[DataLoader]
public static async Task<IReadOnlyDictionary<int, Page<Session>>> SessionsByTrackIdAsync(
    IReadOnlyList<int> trackIds,
    ApplicationDbContext dbContext,
    ISelectorBuilder selector,
    PagingArguments pagingArguments,
    CancellationToken cancellationToken)
{
    return await dbContext.Sessions
        .AsNoTracking()
        .Where(s => s.TrackId != null && trackIds.Contains((int)s.TrackId))
        .OrderBy(s => s.Id)
        .Select(s => s.TrackId, selector)
        .ToBatchPageAsync(s => (int)s.TrackId!, pagingArguments, cancellationToken);
}

@ty01314 ty01314 changed the title Enhance IFilterContext to Support IDataLoader returning ILookup Enhance DataLoader Flexibility by Allowing Access to Filtering and Sorting Contexts or custom variable Jan 5, 2025
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

1 participant