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

Allow defining a Relay [NodeResolver] in a [ObjectType<T>]

Open PHILLIPS71 opened this issue 10 months ago • 0 comments

Product

Hot Chocolate

Is your feature request related to a problem?

In v15.0.3, I was able to define all relay node resolvers within my object types, which seemed to work fine and kept type configurations centralized.

After upgrading to v15.1.3, this approach now results in a schema exception: There is no object type implementing interface 'Node'. (HotChocolate.Types.Relay.NodeType)

The solution you'd like

It would be great if this were supported, allowing node resolvers to be inferred within object type definitions. For example, here is an object type I was using in v15.0.3 that didn't raise any schema exceptions:

[ObjectType<User>]
public static partial class UserType
{
    static partial void Configure(IObjectTypeDescriptor<User> descriptor)
    {
        descriptor
            .Field(f => f.Id)
            .ID();

        // ...
    }

    [NodeResolver]
    internal static Task<User?> GetUserByIdAsync(
        Guid id,
        QueryContext<User> query,
        IUserByIdDataLoader dataloader,
        CancellationToken cancellation)
    {
        return dataloader.With(query).LoadAsync(id, cancellation);
    }

    [DataLoader]
    internal static Task<Dictionary<Guid, User>> GetUserByIdAsync(
        IReadOnlyList<Guid> keys,
        QueryContext<User> query,
        ApplicationDbContext database,
        CancellationToken cancellation = default)
    {
        return database
            .Users
            .AsNoTracking()
            .Where(x => keys.Contains(x.Id))
            .With(query)
            .ToDictionaryAsync(x => x.Id, cancellation);
    }
}

This was also discussed in this slack discussion, which might provide additional context.

PHILLIPS71 avatar Apr 03 '25 10:04 PHILLIPS71