graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Make TaskCache Observable by Dataloaders

Open PascalSenn opened this issue 4 years ago • 6 comments

Enables dataloaders to observe the cache. This makes it possible that DataLoaders can build their state based on the result of other dataloaders

An example

public record Person(Guid Id, string Username);

public class PersonByIdDataLoader : BatchDataLoader<Guid, Person?> 
{
    // left out for brevity

    protected override ValueTask FetchAsync(
        IReadOnlyList<TKey> keys,
        Memory<Result<TValue>> results,
        CancellationToken cancellationToken)
        => _service.GetByIds(keys, results, cancellationToken);
    }
}

public class PersonByUserNameDataLoader : BatchDataLoader<string, Person?> 
{
    // left out for brevity

    protected override ValueTask FetchAsync(
        IReadOnlyList<TKey> keys,
        Memory<Result<TValue>> results,
        CancellationToken cancellationToken)
        => _service.GetByUserName(keys, results, cancellationToken);
    }
}

If these two dataloaders are used in the same query, it could be that both dataloaders load the same value from the database. Only one fetch would have been enough though, as the PersonByIdDataLoader could inform the other Dataloader about the fetched person.

In the following example, the data is only fetched once.

public record Person(Guid Id, string Username);

public class PersonByIdDataLoader : BatchDataLoader<Guid, Person?>, ITaskCacheObserver 
{
    // left out for brevity

    protected override ValueTask FetchAsync(
        IReadOnlyList<TKey> keys,
        Memory<Result<TValue>> results,
        CancellationToken cancellationToken)
        => _service.GetByIds(keys, results, cancellationToken);
    }

    public bool CanHandle(object value) => value is Person;

    public void OnNext(object value)
    {
        if(value is Person p) 
        {
            Set(p.Id, p);
        }
    }
}

public class PersonByUserNameDataLoader : BatchDataLoader<string, Person?>, ITaskCacheObserver 
{
    // left out for brevity

    protected override ValueTask FetchAsync(
        IReadOnlyList<TKey> keys,
        Memory<Result<TValue>> results,
        CancellationToken cancellationToken)
        => _service.GetByUserName(keys, results, cancellationToken);
    }
    
    public bool CanHandle(object value) => value is Person;

    public void OnNext(object value)
    {
        if(value is Person p) 
        {
            Set(p.UserName, p);
        }
    }
}

PascalSenn avatar Dec 16 '21 00:12 PascalSenn

@PascalSenn was this one for 13 or 12.x?

michaelstaib avatar Dec 22 '21 20:12 michaelstaib

i guess 13 as its a breaking one? not sure

PascalSenn avatar Dec 22 '21 20:12 PascalSenn

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 19 '22 09:08 CLAassistant

Qodana for .NET

It seems all right 👌

No new problems were found according to the checks applied

💡 Qodana analysis was run in the pull request mode: only the changed files were checked ☁️ View the detailed Qodana report

Contact Qodana team

Contact us at [email protected]

  • Or via our issue tracker: https://jb.gg/qodana-issue
  • Or share your feedback: https://jb.gg/qodana-discussions

github-actions[bot] avatar Nov 06 '23 21:11 github-actions[bot]

Codecov Report

Attention: Patch coverage is 87.50000% with 7 lines in your changes missing coverage. Please review.

Project coverage is 76.60%. Comparing base (6965960) to head (0f0121b). Report is 488 commits behind head on main.

Files Patch % Lines
src/GreenDonut/src/Core/TaskCache.cs 84.09% 7 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4566      +/-   ##
==========================================
- Coverage   78.98%   76.60%   -2.39%     
==========================================
  Files        2903     2541     -362     
  Lines      139771   127086   -12685     
==========================================
- Hits       110397    97348   -13049     
- Misses      29374    29738     +364     
Flag Coverage Δ
unittests 76.60% <87.50%> (-2.39%) :arrow_down:

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

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Nov 06 '23 21:11 codecov[bot]

Replaced by #7382

michaelstaib avatar Aug 19 '24 20:08 michaelstaib