Initial Commit ...

This commit is contained in:
2024-01-25 04:41:17 +03:30
commit ff4d0d5dcb
51 changed files with 5949 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
using GraphQL.Types;
using xModels.Base;
namespace xDataService.GraphQL
{
/// <summary>
/// Base Entity GraphType Object ...
/// </summary>
/// <typeparam name="TKey"></typeparam>
public abstract class XBaseGraphObjectType<T, TKey> : ObjectGraphType<T>
where T : XBaseEntity<TKey> {
public XBaseGraphObjectType () {
//
Name = $"{typeof(T).Name}GraphType";
//
Field (x => x.Id);
Field (x => x.Deleted);
}
}
}
+17
View File
@@ -0,0 +1,17 @@
using GraphQL.Types;
using xDataService.Models;
using xModels.Base;
namespace xDataService.GraphQL {
public class XBaseGraphQLEventType<TEntity, TKey, TGraphType> : ObjectGraphType<XBaseEventModel<TEntity>>
where TGraphType : IGraphType
where TEntity : XBaseEntity<TKey> {
public XBaseGraphQLEventType () {
//
Name = nameof (XBaseGraphQLEventType<TEntity, TKey, TGraphType>);
//
Field (x => x.Model, type : typeof (TGraphType));
}
}
}
+167
View File
@@ -0,0 +1,167 @@
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
}
}
}
+107
View File
@@ -0,0 +1,107 @@
using System.Collections.Generic;
using GraphQL.Resolvers;
using GraphQL.Types;
using xCommons.Extensions;
using xDataService.Events;
using xDataService.Interfaces;
using xDataService.Models;
using xModels.Base;
namespace xDataService.GraphQL {
public abstract class XBaseGraphQLSubscription<TEntity, TKey, TGraph> : ObjectGraphType
where TGraph : IGraphType
where TEntity : XBaseEntity<TKey> {
public XBaseGraphQLSubscription (IXBaseRepositoryEvents<TEntity> eventProvider = null) {
//
Name = GetType ().Name;
//
// Check if base repository patter registered ...
if (!eventProvider.IsNull ()) {
//
// Add ...
AddField (new EventStreamFieldType {
//
Name = nameof (XBaseRepositoryEvents<TEntity>.AddEvent),
Type = typeof (XBaseGraphQLEventType<TEntity, TKey, TGraph>),
Resolver = new FuncFieldResolver<XBaseEventModel<TEntity>> (
context => context.Source as XBaseEventModel<TEntity>
),
Subscriber = new EventStreamResolver<XBaseEventModel<TEntity>> (context => {
return eventProvider.OnAddObservable;
})
});
//
// Add Many ...
AddField (new EventStreamFieldType {
//
Name = nameof (XBaseRepositoryEvents<TEntity>.AddManyEvent),
Type = typeof (XBaseGraphQLEventType<TEntity, TKey, TGraph>),
Resolver = new FuncFieldResolver<XBaseEventModel<IEnumerable<TEntity>>> (
context => context.Source as XBaseEventModel<IEnumerable<TEntity>>
),
Subscriber = new EventStreamResolver<XBaseEventModel<IEnumerable<TEntity>>> (context => {
return eventProvider.OnAddManyObservable;
})
});
//
// Update ...
AddField (new EventStreamFieldType {
//
Name = nameof (XBaseRepositoryEvents<TEntity>.UpdateEvent),
Type = typeof (XBaseGraphQLEventType<TEntity, TKey, TGraph>),
Resolver = new FuncFieldResolver<XBaseEventModel<TEntity>> (
context => context.Source as XBaseEventModel<TEntity>
),
Subscriber = new EventStreamResolver<XBaseEventModel<TEntity>> (context => {
return eventProvider.OnUpdateObservable;
})
});
//
// Update Mnay ...
AddField (new EventStreamFieldType {
//
Name = nameof (XBaseRepositoryEvents<TEntity>.UpdateManyEvent),
Type = typeof (XBaseGraphQLEventType<TEntity, TKey, TGraph>),
Resolver = new FuncFieldResolver<XBaseEventModel<IEnumerable<TEntity>>> (
context => context.Source as XBaseEventModel<IEnumerable<TEntity>>
),
Subscriber = new EventStreamResolver<XBaseEventModel<IEnumerable<TEntity>>> (context => {
return eventProvider.OnUpdateManyObservable;
})
});
//
// Remove ...
AddField (new EventStreamFieldType {
//
Name = nameof (XBaseRepositoryEvents<TEntity>.RemoveEvent),
Type = typeof (XBaseGraphQLEventType<TEntity, TKey, TGraph>),
Resolver = new FuncFieldResolver<XBaseEventModel<TEntity>> (
context => context.Source as XBaseEventModel<TEntity>
),
Subscriber = new EventStreamResolver<XBaseEventModel<TEntity>> (context => {
return eventProvider.OnRemoveObservable;
})
});
//
// Remove Many ...
AddField (new EventStreamFieldType {
//
Name = nameof (XBaseRepositoryEvents<TEntity>.RemoveManyEvent),
Type = typeof (XBaseGraphQLEventType<TEntity, TKey, TGraph>),
Resolver = new FuncFieldResolver<XBaseEventModel<IEnumerable<TEntity>>> (
context => context.Source as XBaseEventModel<IEnumerable<TEntity>>
),
Subscriber = new EventStreamResolver<XBaseEventModel<IEnumerable<TEntity>>> (context => {
return eventProvider.OnRemoveManyObservable;
})
});
}
}
}
}
+64
View File
@@ -0,0 +1,64 @@
using xCommons.Extensions;
using xDataService.Configuration;
using xDataService.Interfaces;
using xModels.Base;
namespace xDataService.GraphQL {
public abstract class XBaseGraphQLTypeHelper<TEntity, TKey> : IXBaseGraphQLTypeHelper<TEntity, TKey>
where TEntity : XBaseEntity<TKey> {
//
private readonly XDataServiceConfiguration configuration;
//
public XBaseGraphQLTypeHelper (
XDataServiceConfiguration configuration = null
) {
this.configuration = configuration;
}
//
public abstract string GetInQueryCollectionName ();
public abstract string GetInQuerySingleName ();
//
public string GetFindOneName () {
return $"find{GetInQuerySingleName ().ToNormalString().Capitalize()}";
}
public string GetFindManyName () {
return $"find{GetInQueryCollectionName ().ToNormalString().Capitalize()}";
}
public string GetQueryName () {
return $"query{GetInQueryCollectionName().ToNormalString().Capitalize()}";
}
public string GetCountName () {
return $"count{GetInQueryCollectionName().ToNormalString().Capitalize()}";
}
public string GetExistsName () {
return $"exists{GetInQuerySingleName().ToNormalString().Capitalize()}";
}
public string GetGraphQLPath (string baseGraphPath = null) {
//
baseGraphPath = baseGraphPath.IsNullOrEmpty () ?
configuration.IsNull () || configuration.GraphQLBasePath.IsNullOrEmpty () ?
"" :
configuration.GraphQLBasePath :
baseGraphPath;
//
var basePath = baseGraphPath
.EndsWith ("/") ?
baseGraphPath
.Substring (0, baseGraphPath.Length - 1) :
baseGraphPath;
//
return $"{basePath}/{GetInQueryCollectionName().ToNormalString()}";
}
}
}