ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.16k stars 736 forks source link

Mutation error mapping with constructor throws with more than one exception type #5991

Open hahn-kev opened 1 year ago

hahn-kev commented 1 year ago

Is there an existing issue for this?

Product

Hot Chocolate

Describe the bug

I've declared a custom error type that I would like to use in a mutation. When I define 2 constructors for different exceptions I get this error when trying to generate a schema.

1. Expression of type 'System.Void' cannot be used for assignment to type 'System.Object' (HotChocolate.Types.ObjectType<LexBoxApi.GraphQL.LexMutations>)
         at HotChocolate.Configuration.TypeInitializer.DiscoverTypes()
         at HotChocolate.Configuration.TypeInitializer.Initialize()

Steps to reproduce

https://github.com/glen-84/hc-5991 (.NET 8, HC 13.8.1)

  1. Create a mutation method with attributes [Error<GqlDbError>] [UseMutationConvention]
  2. declare GqlDbError with 2 constructors each taking a different exception type
  3. Try to get the schema

Relevant log output

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      HotChocolate.SchemaException: For more details look at the `Errors` property.

      1. Expression of type 'System.Void' cannot be used for assignment to type 'System.Object' (HotChocolate.Types.ObjectType<LexBoxApi.GraphQL.LexMutations>)

         at HotChocolate.Configuration.TypeInitializer.DiscoverTypes()
         at HotChocolate.Configuration.TypeInitializer.Initialize()
         at HotChocolate.SchemaBuilder.Setup.InitializeTypes(SchemaBuilder builder, IDescriptorContext context, IReadOnlyList`1 types)
         at HotChocolate.SchemaBuilder.Setup.Create(SchemaBuilder builder, LazySchema lazySchema, IDescriptorContext context)
         at HotChocolate.SchemaBuilder.Create(IDescriptorContext context)
         at HotChocolate.SchemaBuilder.HotChocolate.ISchemaBuilder.Create(IDescriptorContext context)
         at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaAsync(String schemaName, RequestExecutorSetup options, RequestExecutorOptions executorOptions, IServiceProvider serviceProvider, TypeModule
ChangeMonitor typeModuleChangeMonitor, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaServicesAsync(String schemaName, RequestExecutorSetup options, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorNoLockAsync(String schemaName, CancellationToken cancellationToken)
         at Microsoft.Extensions.DependencyInjection.HotChocolateStitchingRequestExecutorExtensions.<>c__DisplayClass6_0.<<AddLocalSchema>b__0>d.MoveNext()
      --- End of stack trace from previous location ---
         at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaAsync(String schemaName, RequestExecutorSetup options, RequestExecutorOptions executorOptions, IServiceProvider serviceProvider, TypeModule
ChangeMonitor typeModuleChangeMonitor, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.CreateSchemaServicesAsync(String schemaName, RequestExecutorSetup options, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorNoLockAsync(String schemaName, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorResolver.GetRequestExecutorAsync(String schemaName, CancellationToken cancellationToken)
         at HotChocolate.Execution.RequestExecutorProxy.GetRequestExecutorAsync(CancellationToken cancellationToken)
         at HotChocolate.AspNetCore.HttpPostMiddlewareBase.HandleRequestAsync(HttpContext context)
         at HotChocolate.AspNetCore.HttpPostMiddlewareBase.InvokeAsync(HttpContext context)

Additional Context?

as a workaround I'm using static factory methods instead.

Version

13.0.5

michaelstaib commented 1 year ago

Can you create a small project that demonstrates the issue and upload it to this issue?

hahn-kev commented 1 year ago

here you go. Just create a project and install hot chocolate aspnet core.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGraphQLServer()
    .AddMutationType<MyMutations>()
    .AddMutationConventions(false)
    .InitializeOnStartup();
var app = builder.Build();
app.Run();

public class GqlDbError
{
    public GqlDbError(NullReferenceException e)
    {
    }

    public GqlDbError(ApplicationException e)
    {
    }

    public string Message { get; set; }
}

public class MyMutations
{
    [Error<GqlDbError>]
    [UseMutationConvention]
    public string Test()
    {
        return "test";
    }
}