basic query and mutation discovery, next dynamic queries get added through extensions :)
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
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())
|
||||
{
|
||||
var queryField = desc.Field(m.LowerCamelCaseName);
|
||||
|
||||
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)
|
||||
queryField.Type(typeof(int?));
|
||||
else
|
||||
queryField.Type(m.CommandResultType);
|
||||
|
||||
//queryField.Use((sp, d) => new MutationAuthorizationMiddleware(m.CommandType, d));
|
||||
|
||||
if (m.CommandType.GetProperties().Length == 0)
|
||||
{
|
||||
queryField.Resolve(async ctx =>
|
||||
{
|
||||
var queryArgument = Activator.CreateInstance(m.CommandType);
|
||||
return await HandleMutation(m.CommandResultType != null, ctx, typeToGet, queryArgument);
|
||||
});
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
queryField.Argument("params", t => t.Type(m.CommandType));
|
||||
|
||||
queryField.Resolve(async ctx =>
|
||||
{
|
||||
var queryArgument = ctx.ArgumentValue<object>("params");
|
||||
return await HandleMutation(m.CommandResultType != null, ctx, typeToGet, queryArgument);
|
||||
});
|
||||
|
||||
// TODO.
|
||||
//if (m.MutationObjectRequired)
|
||||
// queryField.Use<MutationParamRequiredMiddleware>();
|
||||
|
||||
// TODO.
|
||||
//if (m.ValidateMutationObject)
|
||||
// queryField.Use<MutationValidationMiddleware>();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HotChocolate" Version="11.0.9" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PoweredSoft.CQRS.Abstractions\PoweredSoft.CQRS.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,74 @@
|
||||
using HotChocolate.Language;
|
||||
using HotChocolate.Resolvers;
|
||||
using HotChocolate.Types;
|
||||
using PoweredSoft.CQRS.Abstractions;
|
||||
using PoweredSoft.CQRS.Abstractions.Discovery;
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace PoweredSoft.CQRS.GraphQL.HotChocolate
|
||||
{
|
||||
public class QueryObjectType : ObjectTypeExtension
|
||||
{
|
||||
private readonly IQueryDiscovery queryDiscovery;
|
||||
|
||||
public QueryObjectType(IQueryDiscovery queryDiscovery) : base()
|
||||
{
|
||||
this.queryDiscovery = queryDiscovery;
|
||||
}
|
||||
|
||||
protected override void Configure(IObjectTypeDescriptor desc)
|
||||
{
|
||||
desc.Name("Query");
|
||||
foreach (var q in queryDiscovery.GetQueries())
|
||||
{
|
||||
if (q.Category != "BasicQuery")
|
||||
return;
|
||||
|
||||
var queryField = desc.Field(q.LowerCamelCaseName);
|
||||
var typeToGet = typeof(IQueryHandler<,>).MakeGenericType(q.QueryType, q.QueryResultType);
|
||||
|
||||
queryField.Type(q.QueryResultType);
|
||||
|
||||
// TODO.
|
||||
// always required.
|
||||
//queryField.Use((sp, d) => new QueryAuthorizationMiddleware(q.QueryType, d));
|
||||
|
||||
if (q.QueryType.GetProperties().Length == 0)
|
||||
{
|
||||
queryField.Resolve(async ctx =>
|
||||
{
|
||||
var queryArgument = Activator.CreateInstance(q.QueryType);
|
||||
return await HandleQuery(ctx, typeToGet, queryArgument);
|
||||
});
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
queryField.Argument("params", t => t.Type(q.QueryType));
|
||||
|
||||
queryField.Resolve(async ctx =>
|
||||
{
|
||||
var queryArgument = ctx.ArgumentValue<object>("params");
|
||||
return await HandleQuery(ctx, typeToGet, queryArgument);
|
||||
});
|
||||
|
||||
/*
|
||||
if (q.QueryObjectRequired)
|
||||
queryField.Use<QueryParamRequiredMiddleware>();*/
|
||||
|
||||
/* TODO
|
||||
if (q.ValidateQueryObject)
|
||||
queryField.Use<QueryValidationMiddleware>();
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
private async System.Threading.Tasks.Task<object> HandleQuery(IResolverContext resolverContext, Type typeToGet, object queryArgument)
|
||||
{
|
||||
dynamic service = resolverContext.Service(typeToGet);
|
||||
var result = await service.HandleAsync((dynamic)queryArgument);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using HotChocolate;
|
||||
using HotChocolate.Execution.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace PoweredSoft.CQRS.GraphQL.HotChocolate
|
||||
{
|
||||
public static class RequestExecutorBuilderExtensions
|
||||
{
|
||||
public static IRequestExecutorBuilder AddPoweredSoftQueries(this IRequestExecutorBuilder builder)
|
||||
{
|
||||
builder.AddTypeExtension<QueryObjectType>();
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static IRequestExecutorBuilder AddPoweredSoftMutations(this IRequestExecutorBuilder builder)
|
||||
{
|
||||
builder.AddTypeExtension<MutationObjectType>();
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user