167 lines
6.1 KiB
C#
167 lines
6.1 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 {
|
|
//
|
|
private readonly XDataServiceConfiguration configuration;
|
|
private readonly IXBaseRepository<TEntity, TKey> repository;
|
|
private 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,
|
|
XGraphQLHelper.ContainsDetailArgument
|
|
),
|
|
resolve : async context =>
|
|
await repository
|
|
.GetAsync (
|
|
id: context.GetIdArgument<TKey> (),
|
|
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument (),
|
|
containsDetail: context.GetContainsDetailArgument ()
|
|
)
|
|
.ToDynamicObject ()
|
|
);
|
|
|
|
//
|
|
// GetAll ...
|
|
FieldAsync<ListGraphType<TGraph>> (
|
|
name: helper.GetInQueryCollectionName (),
|
|
arguments: new QueryArguments (
|
|
XGraphQLHelper.IgnoreSoftDeletedArgument,
|
|
XGraphQLHelper.ContainsDetailArgument
|
|
),
|
|
resolve : async context =>
|
|
await repository
|
|
.GetAllAsync (
|
|
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument (),
|
|
containsDetail: context.GetContainsDetailArgument ()
|
|
)
|
|
.ToDynamicObject ()
|
|
);
|
|
|
|
//
|
|
// FindOne ...
|
|
FieldAsync<TGraph> (
|
|
name: helper.GetFindOneName (),
|
|
arguments: new QueryArguments (
|
|
XGraphQLHelper.SearchQueryArgument,
|
|
XGraphQLHelper.IgnoreSoftDeletedArgument,
|
|
XGraphQLHelper.ContainsDetailArgument
|
|
),
|
|
resolve : async (context) => {
|
|
//
|
|
var searchQuery = context.GetSearchQueryArgument ();
|
|
Expression<Func<TEntity, bool>> whereClase = pe => pe
|
|
.PropValuesContains (searchQuery);
|
|
|
|
//
|
|
return await repository
|
|
.FindOneAsync (
|
|
whereClause: whereClase,
|
|
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument (),
|
|
containsDetail: context.GetContainsDetailArgument ()
|
|
)
|
|
.ToDynamicObject ();
|
|
}
|
|
);
|
|
|
|
//
|
|
// FindMany ...
|
|
FieldAsync<ListGraphType<TGraph>> (
|
|
name: helper.GetFindManyName (),
|
|
arguments: new QueryArguments (
|
|
XGraphQLHelper.SearchQueryArgument,
|
|
XGraphQLHelper.IgnoreSoftDeletedArgument,
|
|
XGraphQLHelper.ContainsDetailArgument
|
|
),
|
|
resolve : async (context) => {
|
|
//
|
|
var searchQuery = context.GetSearchQueryArgument ();
|
|
Expression<Func<TEntity, bool>> whereClase = pe => pe
|
|
.PropValuesContains (searchQuery);
|
|
|
|
//
|
|
return await repository
|
|
.FindManyAsync (
|
|
whereClause: whereClase,
|
|
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument (),
|
|
containsDetail: context.GetContainsDetailArgument ()
|
|
)
|
|
.ToDynamicObject ();
|
|
}
|
|
);
|
|
|
|
//
|
|
// Query ...
|
|
FieldAsync<XGraphQueryResult<TEntity, TGraph>> (
|
|
name: helper.GetQueryName (),
|
|
arguments: new QueryArguments (
|
|
XGraphQLHelper.QueryArgument,
|
|
XGraphQLHelper.IgnoreSoftDeletedArgument
|
|
),
|
|
resolve : async context =>
|
|
await repository
|
|
.QueryAsync (
|
|
query: context.GetQueryArgument (),
|
|
ignoreSoftDeleteds: context.GetIgnoreSoftDeletedArgument ()
|
|
)
|
|
.ToDynamicObject ()
|
|
);
|
|
|
|
//
|
|
// Count ...
|
|
FieldAsync<IntGraphType> (
|
|
name: helper.GetCountName (),
|
|
resolve: async context =>
|
|
await repository
|
|
.CountAsync ()
|
|
);
|
|
|
|
//
|
|
// Exists ...
|
|
FieldAsync<BooleanGraphType> (
|
|
name: helper.GetExistsName (),
|
|
arguments: new QueryArguments (
|
|
XGraphQLHelper.GetIdArgument<TGraphKey> ()
|
|
),
|
|
resolve : async context =>
|
|
await repository.IsExistsAsync (
|
|
context.GetIdArgument<TKey> ()
|
|
)
|
|
);
|
|
#endregion
|
|
}
|
|
}
|
|
} |