Files
xDataService/GraphQL/XBaseGraphQLQuery.cs

180 lines
6.3 KiB
C#

using System;
using System.Linq.Expressions;
using GraphQL.Types;
using xCommons.Extensions;
using xDataService.Configuration;
using xDataService.Constants;
using xDataService.Extensions;
using xDataService.Interfaces;
using xDataService.Models;
using xModels.Base;
namespace xDataService.GraphQL
{
public abstract class XBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey> : ObjectGraphType, IXBaseGraphQLQuery<TEntity, TKey, TGraph, TGraphKey>
where TEntity : XBaseEntity<TKey>
where TGraph : IGraphType
where TGraphKey : IGraphType
{
//
protected readonly XDataServiceConfiguration configuration;
protected readonly IXBaseRepository<TEntity, TKey> repository;
protected readonly IXBaseGraphQLTypeHelper<TEntity, TKey> helper;
//
public XBaseGraphQLQuery(
XDataServiceConfiguration configuration,
IXBaseRepository<TEntity, TKey> repository,
IXBaseGraphQLTypeHelper<TEntity, TKey> helper
)
{
//
this.helper = helper;
this.repository = repository;
this.configuration = configuration;
//
Name = GetType().Name;
//
#region Retrieve ...
//
// Get ...
FieldAsync<TGraph>(
name: helper.GetInQuerySingleName(),
arguments: new QueryArguments(
XGraphQLHelper.GetIdArgument<TGraphKey>(),
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async context =>
await repository
.GetAsync(
includeBuilder: null,
id: context.GetIdArgument<TKey>(),
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
.ToDynamicObject()
);
//
// GetAll ...
FieldAsync<ListGraphType<TGraph>>(
name: helper.GetInQueryCollectionName(),
arguments: new QueryArguments(
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async context =>
await repository
.GetAllAsync(
orderBuilder: null,
includeBuilder: null,
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
.ToDynamicObject()
);
//
// FindOne ...
FieldAsync<TGraph>(
name: helper.GetFindOneName(),
arguments: new QueryArguments(
XGraphQLHelper.SearchQueryArgument,
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async (context) =>
{
//
var searchQuery = context.GetSearchQueryArgument();
Expression<Func<TEntity, bool>> whereClase = pe => pe
.PropValuesContains(searchQuery);
//
return await repository
.FindOneAsync(
includeBuilder: null,
predicate: whereClase,
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
.ToDynamicObject();
}
);
//
// FindMany ...
FieldAsync<ListGraphType<TGraph>>(
name: helper.GetFindManyName(),
arguments: new QueryArguments(
XGraphQLHelper.SearchQueryArgument,
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async (context) =>
{
//
var searchQuery = context.GetSearchQueryArgument();
Expression<Func<TEntity, bool>> whereClase = pe => pe
.PropValuesContains(searchQuery);
//
return await repository
.FindManyAsync(
orderBuilder: null,
includeBuilder: null,
predicate: whereClase,
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
.ToDynamicObject();
}
);
//
// Query ...
FieldAsync<XGraphQueryResult<TEntity, TGraph>>(
name: helper.GetQueryName(),
arguments: new QueryArguments(
XGraphQLHelper.QueryArgument,
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async context =>
await repository
.QueryAsync(
predicate: null,
orderBuilder: null,
includeBuilder: null,
query: context.GetQueryArgument(),
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
.ToDynamicObject()
);
//
// Count ...
FieldAsync<IntGraphType>(
name: helper.GetCountName(),
arguments: new QueryArguments(
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async context =>
await repository
.CountAsync(
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
);
//
// Exists ...
FieldAsync<BooleanGraphType>(
name: helper.GetExistsName(),
arguments: new QueryArguments(
XGraphQLHelper.GetIdArgument<TGraphKey>(),
XGraphQLHelper.IgnoreSoftDeletedArgument
),
resolve: async context =>
await repository.IsExistsAsync(
id: context.GetIdArgument<TKey>(),
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument()
)
);
#endregion
}
}
}