question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cleaner options for ResolveWith and static classes/methods

See original GitHub issue

Is your feature request related to a problem?

I started with something like this (a nested resolver class):

public sealed class ArticleRelatedContentType : ObjectType<ArticleRelatedContent>
{
    protected override void Configure(IObjectTypeDescriptor<ArticleRelatedContent> descriptor)
    {
        descriptor.BindFieldsExplicitly();

        descriptor
            .Field("players")
            .ResolveWith<Resolvers>(_ => Resolvers.Players(default!, default!));
    }

    private class Resolvers
    {
        [UsePaging]
        [UseProjection]
        [UseFiltering]
        [UseSorting]
        public static IQueryable<Player> Players(
            [Service] PlayerService playerService,
            [Parent] ArticleRelatedContent relatedContent)
        {
            return playerService.GetPlayersByArticleId(relatedContent.Article.Id);
        }
    }
}

But the default! stuff bothers me. It seems like there should be some way to reference a method without a lambda. Maybe a Delegate?

I was also unable to make the class static, since the generic type argument cannot be static.

I’m now thinking of switching to something like this instead (just a method on the type class):

public sealed class ArticleRelatedContentType : ObjectType<ArticleRelatedContent>
{
    protected override void Configure(IObjectTypeDescriptor<ArticleRelatedContent> descriptor)
    {
        descriptor.BindFieldsExplicitly();

        descriptor
            .Field("players")
            .ResolveWith<ArticleRelatedContentType>(_ => Players(default!, default!));
    }

    [UsePaging]
    [UseProjection]
    [UseFiltering]
    [UseSorting]
    private static IQueryable<Player> Players(
        [Service] PlayerService playerService,
        [Parent] ArticleRelatedContent relatedContent)
    {
        return playerService.GetPlayersByArticleId(relatedContent.Article.Id);
    }
}

Again we have the lambda, and it also feels unnecessary to specify the current type in the generic.

The solution you’d like

Something simpler/cleaner, like:

   descriptor
        .Field("players")
        .ResolveWith(Players);

If ResolveWith took a Delegate, could that work?

(I know that Resolve can also be used, but it can get messy to put all of the code into the Configure method.)

Product

Hot Chocolate

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
michaelstaibcommented, Sep 29, 2022

its probably what I suggested … the above, which also the minimal API uses compiles delegates…

I will have a look at it … It only works since .NET 6. But I do not know… if it can infer if from the method.

1reaction
michaelstaibcommented, Sep 29, 2022

How would think about this?

public sealed class ArticleRelatedContentType : ObjectType<ArticleRelatedContent>
{
    protected override void Configure(IObjectTypeDescriptor<ArticleRelatedContent> descriptor)
    {
        descriptor.BindFieldsExplicitly();

        descriptor
            .Field("players")
            .Resolve(
                [UsePaging]
                [UseProjection]
                [UseFiltering]
                [UseSorting]
                ([Service] PlayerService playerService,
                [Parent] ArticleRelatedContent relatedContent) 
                => playerService.GetPlayersByArticleId(relatedContent.Article.Id));
    }
}

you could in this case even do a static delegate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding static methods to facilitate cleaner unit tests
To unit test myMethod() I need to create the entire object (with many parameters that need constructed, etc), while the method only uses...
Read more >
Static Classes and Static Class Members - C# guide
A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not...
Read more >
Static methods considered evil?
Since it is a static method I cannot just validate it was called. So as I see it, my options are: 1. make...
Read more >
Python's Instance, Class, and Static Methods Demystified
This tutorial helps demystify what's behind class, static, and instance methods in Python.
Read more >
Thoughts and Best Practices on Static Classes and Members
What are the general thoughts and industry best practices on this? See the below example classes and members to illustrate what I am...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found