This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2739f4a
commit 22dec97
Showing
15 changed files
with
437 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/modules/crm/CRMCore.Module.GraphQL/CRMCore.Module.GraphQL.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<OutputType>Library</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="GraphQL" Version="0.17.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\targets\CRMCore.Module.Targets\CRMCore.Module.Targets.csproj" /> | ||
<ProjectReference Include="..\..\core\CRMCore.Module.Data\CRMCore.Module.Data.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
35 changes: 35 additions & 0 deletions
35
src/modules/crm/CRMCore.Module.GraphQL/GraphQLController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using GraphQL; | ||
using GraphQL.Http; | ||
using GraphQL.Types; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading.Tasks; | ||
|
||
namespace CRMCore.Module.GraphQL | ||
{ | ||
[Area("CRMCore.Module.GraphQL")] | ||
[Route("graphql/api/query")] | ||
public class GraphQLController : Controller | ||
{ | ||
private readonly Schema graphQLSchema; | ||
|
||
public GraphQLController(Schema schema) | ||
{ | ||
graphQLSchema = schema; | ||
} | ||
|
||
[HttpPost("")] | ||
public async Task<string> Get([FromQuery] string query = "{ crm_Tasks_list { id } }") | ||
{ | ||
var result = await new DocumentExecuter().ExecuteAsync( | ||
new ExecutionOptions() | ||
{ | ||
Schema = graphQLSchema, | ||
Query = query | ||
} | ||
).ConfigureAwait(false); | ||
|
||
var json = new DocumentWriter(indent: true).Write(result.Data); | ||
return json; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using CRMCore.Module.Data; | ||
using CRMCore.Module.GraphQL.Models; | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace CRMCore.Module.GraphQL | ||
{ | ||
public class GraphQLQuery : ObjectGraphType<object> | ||
{ | ||
private IDatabaseMetadata _dbMetadata; | ||
private ApplicationDbContext _dbContext; | ||
|
||
public GraphQLQuery(ApplicationDbContext dbContext, IDatabaseMetadata dbMetadata) | ||
{ | ||
_dbMetadata = dbMetadata; | ||
_dbContext = dbContext; | ||
|
||
Name = "Query"; | ||
|
||
foreach (var metaTable in _dbMetadata.GetMetadataTables()) | ||
{ | ||
var tableType = new TableType(metaTable); | ||
/*AddField(new FieldType() | ||
{ | ||
Name = metaTable.TableName, | ||
Type = tableType.GetType(), | ||
ResolvedType = tableType, | ||
Resolver = new MyFieldResolver(metaTable, _dbContext), | ||
Arguments = new QueryArguments( | ||
tableType.TableArgs | ||
) | ||
});*/ | ||
|
||
//lets add key to get list of current table | ||
var listType = new ListGraphType(tableType); | ||
AddField(new FieldType | ||
{ | ||
Name = $"{metaTable.TableName}_list", | ||
Type = listType.GetType(), | ||
ResolvedType = listType, | ||
Resolver = new MyFieldResolver(metaTable, _dbContext), | ||
/*Arguments = new QueryArguments( | ||
tableType.TableArgs | ||
) */ | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public class MyFieldResolver : IFieldResolver | ||
{ | ||
private TableMetadata _tableMetadata; | ||
private ApplicationDbContext _dbContext; | ||
|
||
public MyFieldResolver(TableMetadata tableMetadata, ApplicationDbContext dbContext) | ||
{ | ||
_tableMetadata = tableMetadata; | ||
_dbContext = dbContext; | ||
} | ||
|
||
public object Resolve(ResolveFieldContext context) | ||
{ | ||
var source = context.Source; | ||
|
||
List<Dictionary<string, object>> finalResult = new List<Dictionary<string, object>>(); | ||
if (context.FieldName.Contains("_list")) | ||
{ | ||
|
||
using (var command = _dbContext.Database.GetDbConnection().CreateCommand()) | ||
{ | ||
command.CommandText = "SELECT * FROM " + _tableMetadata.TableName; | ||
_dbContext.Database.OpenConnection(); | ||
using (var result = command.ExecuteReader()) | ||
{ | ||
while (result.Read()) | ||
{ | ||
var temp = new Dictionary<string, object>(); | ||
|
||
for (var index = 0; index < result.FieldCount; index++) | ||
{ | ||
var lowerCase = Char.ToLowerInvariant(result.GetName(index)[0]) + result.GetName(index).Substring(1); | ||
temp.Add(lowerCase, result[result.GetName(index)]); | ||
} | ||
|
||
finalResult.Add(temp); | ||
} | ||
} | ||
|
||
if (_dbContext.Database.GetDbConnection().State == System.Data.ConnectionState.Open) | ||
{ | ||
_dbContext.Database.CloseConnection(); | ||
} | ||
} | ||
} | ||
|
||
return finalResult; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/modules/crm/CRMCore.Module.GraphQL/Models/ColumnMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace CRMCore.Module.GraphQL.Models | ||
{ | ||
public class ColumnMetadata | ||
{ | ||
[Column("name")] | ||
public string ColumnName | ||
{ | ||
get; set; | ||
} | ||
|
||
[Column("type")] | ||
public string DataType | ||
{ | ||
get; set; | ||
} | ||
|
||
[Column("notnull")] | ||
public string IsNullable | ||
{ | ||
get; set; | ||
} | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/modules/crm/CRMCore.Module.GraphQL/Models/DatabaseMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using CRMCore.Module.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CRMCore.Module.GraphQL.Models | ||
{ | ||
public class DatabaseMetadata : IDatabaseMetadata | ||
{ | ||
protected ApplicationDbContext _dbContext; | ||
|
||
public DatabaseMetadata(ApplicationDbContext dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
DatabaseName = _dbContext.Database.GetDbConnection().Database; | ||
if (Tables == null) | ||
LoadMetaData(); | ||
} | ||
|
||
public string DatabaseName { get; set; } | ||
|
||
public List<TableMetadata> Tables { get; set; } | ||
|
||
public void ReloadMetadata() | ||
{ | ||
LoadMetaData(); | ||
} | ||
|
||
public IEnumerable<TableMetadata> GetMetadataTables() | ||
{ | ||
if (Tables == null) | ||
return new List<TableMetadata>(); | ||
|
||
return Tables; | ||
} | ||
|
||
private void LoadMetaData() | ||
{ | ||
// var res = new List<TableMetadata>(); | ||
/*res.Add( | ||
FetchTableMetaData("Customers") | ||
);*/ | ||
|
||
Tables = FetchTableMetaData(); | ||
} | ||
|
||
/*public List<TableMetadata> GetMetadataTables() | ||
{ | ||
if (Tables == null) | ||
return new List<TableMetadata>(); | ||
return Tables; | ||
} */ | ||
|
||
private List<TableMetadata> FetchTableMetaData() | ||
{ | ||
var metaTables = new List<TableMetadata>(); | ||
foreach (var entityType in _dbContext.Model.GetEntityTypes()) | ||
{ | ||
var metaTable = new TableMetadata(); | ||
var relational = entityType.Relational(); | ||
var tableName = relational.TableName; | ||
|
||
metaTable.TableName = tableName; | ||
metaTable.Columns = GetColumnsMetadata(entityType).ToList(); | ||
|
||
metaTables.Add(metaTable); | ||
} | ||
|
||
return metaTables; | ||
} | ||
|
||
private IEnumerable<ColumnMetadata> GetColumnsMetadata(IEntityType entityType) | ||
{ | ||
var tableColumns = new List<ColumnMetadata>(); | ||
|
||
foreach (var propertyType in entityType.GetProperties()) | ||
{ | ||
var relational = propertyType.Relational(); | ||
tableColumns.Add(new ColumnMetadata | ||
{ | ||
ColumnName = relational.ColumnName, | ||
DataType = relational.ColumnType | ||
}); | ||
} | ||
|
||
return tableColumns; | ||
} | ||
} | ||
|
||
public interface IDatabaseMetadata | ||
{ | ||
void ReloadMetadata(); | ||
IEnumerable<TableMetadata> GetMetadataTables(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/modules/crm/CRMCore.Module.GraphQL/Models/TableMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace CRMCore.Module.GraphQL.Models | ||
{ | ||
public class TableMetadata | ||
{ | ||
[Column("table_name")] | ||
public string TableName { get; set; } | ||
|
||
public List<ColumnMetadata> Columns { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
25 changes: 25 additions & 0 deletions
25
src/modules/crm/CRMCore.Module.GraphQL/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using CRMCore.Module.Data; | ||
using CRMCore.Module.GraphQL.Models; | ||
using GraphQL.Types; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace CRMCore.Module.GraphQL | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddMyGraphQL(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IDatabaseMetadata, DatabaseMetadata>(); | ||
services.AddScoped((resolver) => | ||
{ | ||
var dbContext = resolver.GetRequiredService<ApplicationDbContext>(); | ||
var metaDatabase = resolver.GetRequiredService<IDatabaseMetadata>(); | ||
var schema = new Schema { Query = new GraphQLQuery(dbContext, metaDatabase) }; | ||
schema.Initialize(); | ||
return schema; | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} |
Oops, something went wrong.