Skip to content

Added DataLoader Projections - #7389

Merged
michaelstaib merged 27 commits into
mainfrom
mst/dataloader-projections
Aug 21, 2024
Merged

michaelstaib merged 27 commits into
mainfrom
mst/dataloader-projections

Conversation

@michaelstaib

@michaelstaib michaelstaib commented Aug 20, 2024 •

Copy link
Copy Markdown
Member

This PR builds on top of the new transient state in DataLoader. With this PR you can now use inferred projections (inferred from a ISelection and exlicit projections)

This is how it works:

Let's say we have a resolver that fetches an author that uses a DataLoader.

public async Task<Author?> GetAuthorById(
    int id,
    ISelection selection,
    IAuthorByIdDataLoader authorById,
    CancellationToken ct)
    => await authorById.Select(selection).LoadAsync(id, ct);

On the DataLoader when we are in a resolver we can do a Select on the current selected field ... which will traverse the subfields and build a selection expression.

If we have a more layered approach we still can do this as we also are able to build the expression explicitly from the ISelection.

var selector = selection.CreateSelector();
await authorById.Select(selector).LoadAsync(id, ct);
  • The selector is an expression tree representing the selected fields which can be passed around.
  • If we have two resolvers using selections on the same DataLoader then both selectors are combined into a single selector.

We can also manually include things as opposed to using a GraphQL field selection.

await authorById.Include(t => t.Books).LoadAsync(id, ct);

On the DataLoader side we do not have any dependencies on HotChocolate itself and can create the DataLoader in isolation.

[DataLoader]
public static async Task<IDictionary<int, Author>> GetAuthorById(
    IReadOnlyList<int> ids,
    MyDbContext context,
    ISelectorContext selector,
    CancellationToken ct)
    => await context.Where(t => ids.Contains(t.Id)).Select(selector).ToDictionaryAsync(t => t.Id, ct) 

The DataLoader can inject the ISelectorContext from its current state and with that build the combined expression to project with an IQueryable<T>.

--> This feature is marked as experimental for 14.0. <--

Todos:

  • DataLoader state plumbing
  • Source Generator integration (ISelectorContext injections)
  • Class DataLoader helpers
  • ISelection to LambdaExpression (selector) rewriter
  • Include(t => t.Foo) to LambdaExpression (selector) rewriter
  • Integration tests

Fixes #6191

@codecov

codecov Bot commented Aug 21, 2024 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 68.80734% with 170 lines in your changes missing coverage. Please review.

Project coverage is 77.27%. Comparing base (059a6bc) to head (bfbdd75).
Report is 1 commits behind head on main.

Files Patch % Lines
.../Core/Projections/SelectionDataLoaderExtensions.cs 50.79% 31 Missing ⚠️
src/GreenDonut/src/Core/CacheDataLoader.cs 30.00% 21 Missing ⚠️
...eenDonut/src/Core/Projections/ExpressionHelpers.cs 67.74% 20 Missing ⚠️
...ocolate/Core/src/Execution/Processing/Operation.cs 60.78% 20 Missing ⚠️
src/GreenDonut/src/Core/DataLoaderFetchContext.cs 54.83% 14 Missing ⚠️
src/GreenDonut/src/Core/GroupedDataLoader.cs 0.00% 14 Missing ⚠️
src/GreenDonut/src/Core/DataLoaderExtensions.cs 50.00% 10 Missing ⚠️
.../Core/src/Types.Analyzers/Models/DataLoaderInfo.cs 38.46% 8 Missing ⚠️
...es.Analyzers/FileBuilders/DataLoaderFileBuilder.cs 78.12% 7 Missing ⚠️
...nut/src/Core/Projections/DefaultSelectorBuilder.cs 76.00% 6 Missing ⚠️
... and 6 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7389      +/-   ##
==========================================
+ Coverage   76.29%   77.27%   +0.98%     
==========================================
  Files        2492     2813     +321     
  Lines      124112   140864   +16752     
==========================================
+ Hits        94686   108849   +14163     
- Misses      29426    32015    +2589     
Flag Coverage Δ
unittests 77.27% <68.80%> (+0.98%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@michaelstaib
michaelstaib marked this pull request as ready for review August 21, 2024 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Projected batching / dataloader projections

1 participant