2021-02-03 19:51:23 -05:00
|
|
|
|
using HotChocolate.Language;
|
|
|
|
|
using HotChocolate.Resolvers;
|
|
|
|
|
using HotChocolate.Types;
|
|
|
|
|
using PoweredSoft.CQRS.Abstractions;
|
|
|
|
|
using PoweredSoft.CQRS.Abstractions.Discovery;
|
|
|
|
|
using System;
|
2021-02-04 21:00:24 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-02-03 19:51:23 -05:00
|
|
|
|
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);
|
|
|
|
|
|
2021-02-04 21:00:24 -05:00
|
|
|
|
queryField.Use((sp, d) => new QueryAuthorizationMiddleware(q.QueryType, d));
|
2021-02-03 19:51:23 -05:00
|
|
|
|
|
2021-02-04 21:00:24 -05:00
|
|
|
|
// if its a IQueryable.
|
|
|
|
|
if (q.QueryResultType.Namespace == "System.Linq" && q.QueryResultType.Name.Contains("IQueryable"))
|
|
|
|
|
{
|
|
|
|
|
//waiting on answer to be determined.
|
2021-02-05 11:59:53 -05:00
|
|
|
|
//this does not work
|
|
|
|
|
//var genericArgument = q.QueryResultType.GetGenericArguments().First();
|
|
|
|
|
//var objectTypeOfAargument = typeof(ObjectType<>).MakeGenericType(genericArgument);
|
|
|
|
|
//var listType = typeof(ListType<>).MakeGenericType(objectTypeOfAargument);
|
|
|
|
|
//queryField.Type(objectTypeOfAargument);
|
|
|
|
|
//queryField.UseSingleOrDefault();
|
|
|
|
|
//queryField.UseProjection();
|
|
|
|
|
////queryField.UsePaging(listType);
|
2021-02-04 21:00:24 -05:00
|
|
|
|
queryField.Type(q.QueryResultType);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
queryField.Type(q.QueryResultType);
|
|
|
|
|
|
|
|
|
|
}
|
2021-02-03 19:51:23 -05:00
|
|
|
|
|
|
|
|
|
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>();*/
|
|
|
|
|
|
2021-02-03 20:28:56 -05:00
|
|
|
|
queryField.Use<QueryValidationMiddleware>();
|
2021-02-03 19:51:23 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|