Skip to content

Cleaner options for ResolveWith and static classes/methods #5445

@glen-84

Description

@glen-84

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

Metadata

Metadata

Assignees

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions