Cleaner options for ResolveWith and static classes/methods
See original GitHub issueIs 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:
- Created a year ago
- Comments:11 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
How would think about this?
you could in this case even do a static delegate.