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
b705e2d
commit 017435c
Showing
11 changed files
with
155 additions
and
136 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
18 changes: 18 additions & 0 deletions
18
src/modules/crm/CRMCore.Module.GraphQL/DbContextExtensions.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,18 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace CRMCore.Module.GraphQL | ||
{ | ||
public static class DbContextExtensions | ||
{ | ||
public static IQueryable Query(this DbContext context, string entityName) => | ||
context.Query(context.Model.FindEntityType(entityName).ClrType); | ||
|
||
static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set)); | ||
|
||
public static IQueryable Query(this DbContext context, Type entityType) => | ||
(IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null); | ||
} | ||
} |
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
12 changes: 1 addition & 11 deletions
12
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 |
---|---|---|
@@ -1,25 +1,15 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace CRMCore.Module.GraphQL.Models | ||
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; | ||
} | ||
} | ||
} |
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
7 changes: 3 additions & 4 deletions
7
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 |
---|---|---|
@@ -1,14 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Collections.Generic; | ||
|
||
namespace CRMCore.Module.GraphQL.Models | ||
{ | ||
public class TableMetadata | ||
{ | ||
[Column("table_name")] | ||
public string TableName { get; set; } | ||
|
||
public string AssemblyFullName { get; set; } | ||
|
||
public List<ColumnMetadata> Columns { get; set; } | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/modules/crm/CRMCore.Module.GraphQL/Resolvers/MyFieldResolver.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,48 @@ | ||
using CRMCore.Module.GraphQL.Models; | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Dynamic.Core; | ||
|
||
namespace CRMCore.Module.GraphQL.Resolvers | ||
{ | ||
public class MyFieldResolver : IFieldResolver | ||
{ | ||
private TableMetadata _tableMetadata; | ||
private DbContext _dbContext; | ||
|
||
public MyFieldResolver(TableMetadata tableMetadata, DbContext dbContext) | ||
{ | ||
_tableMetadata = tableMetadata; | ||
_dbContext = dbContext; | ||
} | ||
|
||
public object Resolve(ResolveFieldContext context) | ||
{ | ||
var queryable = _dbContext.Query(_tableMetadata.FullQualifiedName); | ||
if (context.FieldName.Contains("_list")) | ||
{ | ||
|
||
var first = context.Arguments["first"] != null ? | ||
context.GetArgument("first", int.MaxValue) : | ||
int.MaxValue; | ||
|
||
var offset = context.Arguments["offset"] != null ? | ||
context.GetArgument("offset", 0) : | ||
0; | ||
|
||
return queryable | ||
.Skip(offset) | ||
.Take(first) | ||
.ToDynamicList<object>(); | ||
} | ||
else | ||
{ | ||
var id = context.GetArgument<Guid>("id"); | ||
return queryable.FirstOrDefault($"Id == @0", id); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/modules/crm/CRMCore.Module.GraphQL/Resolvers/NameFieldResolver.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,34 @@ | ||
using GraphQL.Resolvers; | ||
using GraphQL.Types; | ||
using System; | ||
|
||
namespace CRMCore.Module.GraphQL.Resolvers | ||
{ | ||
public class NameFieldResolver : IFieldResolver | ||
{ | ||
public object Resolve(ResolveFieldContext context) | ||
{ | ||
var source = context.Source; | ||
|
||
if (source == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var name = Char.ToUpperInvariant(context.FieldAst.Name[0]) + context.FieldAst.Name.Substring(1); | ||
var value = GetPropValue(source, name); | ||
|
||
if (value == null) | ||
{ | ||
throw new InvalidOperationException($"Expected to find property {context.FieldAst.Name} on {context.Source.GetType().Name} but it does not exist."); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
private static object GetPropValue(object src, string propName) | ||
{ | ||
return src.GetType().GetProperty(propName).GetValue(src, null); | ||
} | ||
} | ||
} |
Oops, something went wrong.