2021-02-03 19:51:23 -05:00
|
|
|
|
using HotChocolate.Resolvers;
|
|
|
|
|
using HotChocolate.Types;
|
|
|
|
|
using PoweredSoft.CQRS.Abstractions;
|
|
|
|
|
using PoweredSoft.CQRS.Abstractions.Discovery;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.CQRS.GraphQL.HotChocolate
|
|
|
|
|
{
|
|
|
|
|
public class MutationObjectType : ObjectTypeExtension
|
|
|
|
|
{
|
|
|
|
|
private readonly ICommandDiscovery commandDiscovery;
|
|
|
|
|
|
|
|
|
|
public MutationObjectType(ICommandDiscovery commandDiscovery) : base()
|
|
|
|
|
{
|
|
|
|
|
this.commandDiscovery = commandDiscovery;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Configure(IObjectTypeDescriptor desc)
|
|
|
|
|
{
|
|
|
|
|
desc.Name("Mutation");
|
|
|
|
|
foreach (var m in commandDiscovery.GetCommands())
|
|
|
|
|
{
|
2021-02-03 20:28:56 -05:00
|
|
|
|
var mutationField = desc.Field(m.LowerCamelCaseName);
|
2021-02-03 19:51:23 -05:00
|
|
|
|
|
|
|
|
|
Type typeToGet;
|
|
|
|
|
if (m.CommandResultType == null)
|
|
|
|
|
typeToGet = typeof(ICommandHandler<>).MakeGenericType(m.CommandType);
|
|
|
|
|
else
|
|
|
|
|
typeToGet = typeof(ICommandHandler<,>).MakeGenericType(m.CommandType, m.CommandResultType);
|
|
|
|
|
|
|
|
|
|
if (m.CommandResultType == null)
|
2021-02-03 20:28:56 -05:00
|
|
|
|
mutationField.Type(typeof(int?));
|
2021-02-03 19:51:23 -05:00
|
|
|
|
else
|
2021-02-03 20:28:56 -05:00
|
|
|
|
mutationField.Type(m.CommandResultType);
|
2021-02-03 19:51:23 -05:00
|
|
|
|
|
|
|
|
|
//queryField.Use((sp, d) => new MutationAuthorizationMiddleware(m.CommandType, d));
|
|
|
|
|
|
|
|
|
|
if (m.CommandType.GetProperties().Length == 0)
|
|
|
|
|
{
|
2021-02-03 20:28:56 -05:00
|
|
|
|
mutationField.Resolve(async ctx =>
|
2021-02-03 19:51:23 -05:00
|
|
|
|
{
|
|
|
|
|
var queryArgument = Activator.CreateInstance(m.CommandType);
|
|
|
|
|
return await HandleMutation(m.CommandResultType != null, ctx, typeToGet, queryArgument);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 20:28:56 -05:00
|
|
|
|
mutationField.Argument("params", t => t.Type(m.CommandType));
|
2021-02-03 19:51:23 -05:00
|
|
|
|
|
2021-02-03 20:28:56 -05:00
|
|
|
|
mutationField.Resolve(async ctx =>
|
2021-02-03 19:51:23 -05:00
|
|
|
|
{
|
|
|
|
|
var queryArgument = ctx.ArgumentValue<object>("params");
|
|
|
|
|
return await HandleMutation(m.CommandResultType != null, ctx, typeToGet, queryArgument);
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-03 20:28:56 -05:00
|
|
|
|
mutationField.Use<MutationParamRequiredMiddleware>();
|
|
|
|
|
mutationField.Use<MutationValidationMiddleware>();
|
2021-02-03 19:51:23 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async System.Threading.Tasks.Task<object> HandleMutation(bool hasResult, IResolverContext ctx, Type typeToGet, object queryArgument)
|
|
|
|
|
{
|
|
|
|
|
dynamic service = ctx.Service(typeToGet);
|
|
|
|
|
|
|
|
|
|
if (hasResult)
|
|
|
|
|
{
|
|
|
|
|
var result = await service.HandleAsync((dynamic)queryArgument);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await service.HandleAsync((dynamic)queryArgument);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|