Search code examples
c#hotchocolate

C# Hotchocolate mutation without input param does not work


I have the following mutation type

[MutationType]
public static class FooMutation
{
    public static async Task<Guid> CreateFoo(CreateFooCommand request, [FromServices] ICommandBus commandBus) => await commandBus.Send(request);
}

I have the following command (which does not work)


public record CreateFooCommand : ICommand<Guid>;

And I have the following handler

public class CreateFooCommandHandler : ICommandHandler<CreateFooCommand, Guid>
{

    public async Task<Guid> Handle(CreateFooCommand command, CancellationToken cancellationToken)
    {
        var foo = Domain.Foo.Create(userId);
        
        return foo.Id;
    }
}

If I do something like the following in the Command, It will works fine


public record CreateFooCommand(string? IDoNotNeedAnyParam) : ICommand<Guid>;

But I do not need any param, and when I remove that param I get:

  1. InputObject CreateFooCommandInput has no fields declared. (GraphQL.Foo.Mapping.RequestTypes.CreateFooInput)

Which should be totally fine by GraphQl standards.

The poor hotchocolate documentation does not tell anything about mutation types without parameters, so I think this should work, but it does not.


Solution

  • HotChocolate supports mutations without arguments. But your mutation has argument! request is treated as mutation argument. That's why HotChocolate tries to expose CreateFooCommand class as CreateFooCommandInput GraphQL type in the output schema. And you get the exception because GraphQL spec doesn't support types without any field.

    To fix that just, simply create the command manually:

    public static async Task<Guid> CreateFoo([FromServices] ICommandBus commandBus)
        => await commandBus.Send(new CreateFooCommand());