GQL dynamic query :)

This commit is contained in:
David Lebee
2021-02-04 13:51:31 -05:00
parent ffcfc60df1
commit edf9258a85
22 changed files with 643 additions and 18 deletions
@@ -0,0 +1,21 @@
using PoweredSoft.DynamicQuery;
using PoweredSoft.DynamicQuery.Core;
using System;
namespace PoweredSoft.CQRS.GraphQL
{
public class GraphQLAdvanceQueryAggregate
{
public string Path { get; set; }
public AggregateType Type { get; set; }
internal IAggregate ToAggregate()
{
return new Aggregate
{
Path = Path,
Type = Type
};
}
}
}
@@ -0,0 +1,49 @@
using PoweredSoft.DynamicQuery;
using PoweredSoft.DynamicQuery.Core;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLAdvanceQueryFilter
{
public bool? And { get; set; }
public FilterType Type { get; set; }
public string Path { get; set; }
public GraphQLVariantInput Value { get; set; }
public bool? Not { get; set; }
public List<GraphQLAdvanceQueryFilter> Filters { get; set; }
internal IFilter ToFilter()
{
if (Type == FilterType.Composite)
{
var ret = new CompositeFilter
{
And = And,
Type = FilterType.Composite
};
if (Filters == null)
ret.Filters = new List<IFilter>();
else
ret.Filters = Filters.Select(t => t.ToFilter()).ToList();
return ret;
}
else
{
return new SimpleFilter
{
And = And,
Type = Type,
Not = Not,
Path = Path,
Value = Value.GetRawObjectValue()
};
}
}
}
}
@@ -0,0 +1,21 @@
using PoweredSoft.DynamicQuery;
using PoweredSoft.DynamicQuery.Core;
using System;
namespace PoweredSoft.CQRS.GraphQL
{
public class GraphQLAdvanceQueryGroup
{
public string Path { get; set; }
public bool? Ascending { get; set; }
internal IGroup ToGroup()
{
return new Group
{
Path = Path,
Ascending = Ascending
};
}
}
}
@@ -0,0 +1,11 @@
using PoweredSoft.DynamicQuery.Core;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLAggregateResult
{
public string Path { get; set; }
public AggregateType Type { get; set; }
public GraphQLVariantResult Value { get; set; }
}
}
@@ -0,0 +1,73 @@
using PoweredSoft.CQRS.DynamicQuery.Abstractions;
using PoweredSoft.CQRS.GraphQL.DynamicQuery;
using PoweredSoft.DynamicQuery.Core;
using System.Collections.Generic;
using System.Linq;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLDynamicQuery<TSource, TDestination> : GraphQLDynamicQuery, IDynamicQuery<TSource, TDestination>
where TSource : class
where TDestination : class
{
}
public class GraphQLDynamicQuery<TSource, TDestination, TParams> : GraphQLDynamicQuery<TSource, TDestination>,
IDynamicQuery<TSource, TDestination, TParams>
where TSource : class
where TDestination : class
where TParams : class
{
public TParams Params { get; set; }
public TParams GetParams() => Params;
}
public class GraphQLDynamicQuery : IDynamicQuery
{
public int? Page { get; set; }
public int? PageSize { get; set; }
public List<GraphQLSort> Sorts { get; set; }
public List<GraphQLAdvanceQueryFilter> Filters { get; set; }
public List<GraphQLAdvanceQueryGroup> Groups { get; set; }
public List<GraphQLAdvanceQueryAggregate> Aggregates { get; set; }
public List<IAggregate> GetAggregates()
{
if (Aggregates == null)
return new List<IAggregate>();
return Aggregates.Select(a => a.ToAggregate()).ToList();
}
public List<IFilter> GetFilters()
{
if (Filters == null)
return new List<IFilter>();
return Filters.Select(t => t.ToFilter()).ToList();
}
public List<IGroup> GetGroups()
{
if (Groups == null)
return new List<IGroup>();
return Groups.Select(t => t.ToGroup()).ToList();
}
public int? GetPage() => Page;
public int? GetPageSize() => PageSize;
public List<ISort> GetSorts()
{
if (Sorts == null)
return new List<ISort>();
return Sorts.Select(t => t.ToSort()).ToList();
}
}
}
@@ -0,0 +1,59 @@
using PoweredSoft.DynamicQuery.Core;
using System.Collections.Generic;
using System.Linq;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLDynamicQueryExecutionResult<TResult> : GraphQLDynamicQueryResult<TResult>
{
public List<GraphQLDynamicQueryGroupResult<TResult>> Groups { get; set; }
public long TotalRecords { get; set; }
public long? NumberOfPages { get; set; }
public void FromResult(IQueryExecutionResult<TResult> queryResult)
{
TotalRecords = queryResult.TotalRecords;
NumberOfPages = queryResult.NumberOfPages;
if (queryResult.Aggregates != null)
Aggregates = queryResult.Aggregates.Select(ConvertAggregateResult).ToList();
if (queryResult.Data != null)
Data = queryResult.Data;
if (queryResult is IQueryExecutionGroupResult<TResult> groupedResult)
Groups = groupedResult.Groups.Select(ConvertGroupResult).ToList();
}
protected virtual GraphQLDynamicQueryGroupResult<TResult> ConvertGroupResult(IGroupQueryResult<TResult> arg)
{
var group = new GraphQLDynamicQueryGroupResult<TResult>();
group.GroupPath = arg.GroupPath;
group.GroupValue = new GraphQLVariantResult(arg.GroupValue);
if (arg.Data != null)
group.Data = arg.Data;
if (arg.Aggregates != null)
group.Aggregates = arg.Aggregates.Select(ConvertAggregateResult).ToList();
if (arg.HasSubGroups)
group.SubGroups = arg.SubGroups.Select(ConvertGroupResult).ToList();
return group;
}
protected virtual GraphQLAggregateResult ConvertAggregateResult(IAggregateResult arg)
{
return new GraphQLAggregateResult
{
Path = arg.Path,
Type = arg.Type,
Value = new GraphQLVariantResult(arg.Value)
};
}
}
}
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Linq;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLDynamicQueryGroupResult<TResult> : GraphQLDynamicQueryResult<TResult>
{
public string GroupPath { get; set; }
public GraphQLVariantResult GroupValue { get; set; }
public bool HasSubGroups => SubGroups?.Any() == true;
public List<GraphQLDynamicQueryGroupResult<TResult>> SubGroups { get; set; }
}
}
@@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLDynamicQueryResult<TResult>
{
public List<TResult> Data { get; set; }
public List<GraphQLAggregateResult> Aggregates { get; set; }
}
}
@@ -0,0 +1,17 @@
using PoweredSoft.DynamicQuery;
using PoweredSoft.DynamicQuery.Core;
using System;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLSort
{
public string Path { get; set; }
public bool? Ascending { get; set; }
internal ISort ToSort()
{
return new Sort(Path, Ascending);
}
}
}
@@ -0,0 +1,90 @@
using System;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public abstract class GraphQLVariant
{
protected virtual string ResolveTypeName(object value)
{
if (value != null)
{
if (value is int)
return "int";
if (value is long)
return "long";
if (value is string)
return "string";
if (value is bool)
return "boolean";
if (value is decimal)
return "decimal";
if (value is DateTime)
return "datetime";
}
return null;
}
public string GetTypeName()
{
var value = GetRawObjectValue();
return ResolveTypeName(value);
}
public virtual void SetVariant(object raw)
{
ClearVariant();
if (raw != null)
{
if (raw is int rawInt)
IntValue = rawInt;
if (raw is long rawLong)
LongValue = rawLong;
if (raw is string rawStr)
StringValue = rawStr;
if (raw is bool rawBool)
BooleanValue = rawBool;
if (raw is decimal rawDec)
DecimalValue = rawDec;
if (raw is DateTime rawDt)
DateTimeValue = rawDt;
}
}
public virtual object GetRawObjectValue()
{
if (IntValue != null && IntValue is int)
return IntValue;
if (LongValue != null && LongValue is long)
return LongValue;
if (StringValue != null && StringValue is string)
return StringValue;
if (BooleanValue != null && BooleanValue is bool)
return BooleanValue;
if (DecimalValue != null && DecimalValue is decimal)
return DecimalValue;
if (DateTimeValue != null && DateTimeValue is DateTime)
return DateTimeValue;
return null;
}
public int? IntValue { get; set; }
public long? LongValue { get; set; }
public string StringValue { get; set; }
public decimal? DecimalValue { get; set; }
public DateTime? DateTimeValue { get; set; }
public bool? BooleanValue { get; set; }
public virtual void ClearVariant()
{
this.IntValue = null;
this.LongValue = null;
this.StringValue = null;
this.DecimalValue = null;
this.DateTimeValue = null;
this.BooleanValue = null;
}
}
}
@@ -0,0 +1,8 @@
using System;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLVariantInput : GraphQLVariant
{
}
}
@@ -0,0 +1,60 @@
using Newtonsoft.Json;
namespace PoweredSoft.CQRS.GraphQL.DynamicQuery
{
public class GraphQLVariantResult : GraphQLVariant
{
public GraphQLVariantResult()
{
}
public GraphQLVariantResult(object raw)
{
SetVariant(raw);
}
protected override string ResolveTypeName(object value)
{
var valueType = base.ResolveTypeName(value);
if (value != null && valueType == null)
return "json";
return valueType;
}
public override object GetRawObjectValue()
{
if (jsonValue != null)
return jsonValue;
return base.GetRawObjectValue();
}
private object jsonValue = null;
public string Json
{
get
{
if (jsonValue != null)
return JsonConvert.SerializeObject(jsonValue, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return null;
}
set
{
jsonValue = JsonConvert.DeserializeObject(value);
}
}
public override void ClearVariant()
{
base.ClearVariant();
this.jsonValue = null;
}
}
}
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PoweredSoft.DynamicQuery" Version="2.1.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PoweredSoft.CQRS.DynamicQuery.Abstractions\PoweredSoft.CQRS.DynamicQuery.Abstractions.csproj" />
<ProjectReference Include="..\PoweredSoft.CQRS.DynamicQuery\PoweredSoft.CQRS.DynamicQuery.csproj" />
</ItemGroup>
</Project>