Skip to content
This repository has been archived by the owner on Apr 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #49 from arduosoft/develop
Browse files Browse the repository at this point in the history
publish client
  • Loading branch information
zeppaman authored Mar 13, 2019
2 parents d6bcbd3 + f85ffa7 commit f72102a
Show file tree
Hide file tree
Showing 37 changed files with 1,501 additions and 89 deletions.
10 changes: 8 additions & 2 deletions Plugins/RawCMS.Plugin.Core/CorePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ public override void ConfigureServices(IServiceCollection services)

Logger.LogInformation(configuration["MongoSettings:ConnectionString"]);


var envConnectionString = Environment.GetEnvironmentVariable("MongoSettings:ConnectionString") ?? Environment.GetEnvironmentVariable("MongoSettingsConnectionString") ?? configuration["MongoSettings:ConnectionString"];
var envDBName = Environment.GetEnvironmentVariable("MongoSettings:DBName") ?? Environment.GetEnvironmentVariable("MongoSettingsDBName")?? configuration["MongoSettings:DBName"];



MongoSettings instance = new MongoSettings
{
ConnectionString = configuration["MongoSettings:ConnectionString"],
DBName = configuration["MongoSettings:DBName"]
ConnectionString = envConnectionString,
DBName = envDBName
};

IOptions<MongoSettings> settingsOptions = Options.Create<MongoSettings>(instance);
Expand Down
10 changes: 9 additions & 1 deletion Plugins/RawCMS.Plugin.Core/Data/UserPostSaveLambda.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using Newtonsoft.Json;
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Min�</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RawCMS.Library.Core;
using RawCMS.Library.Core.Interfaces;
Expand Down
10 changes: 9 additions & 1 deletion Plugins/RawCMS.Plugin.Core/Data/UserPresaveLambda.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using Newtonsoft.Json.Linq;
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Min�</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using Newtonsoft.Json.Linq;
using RawCMS.Library.Core;
using RawCMS.Library.Core.Interfaces;
using RawCMS.Library.Service;
Expand Down
3 changes: 2 additions & 1 deletion Plugins/RawCMS.Plugins.GraphQL/Classes/GraphQLQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public GraphQLQuery(GraphQLService graphQLService)
foreach (var key in graphQLService.Collections.Keys)
{
Library.Schema.CollectionSchema metaColl = graphQLService.Collections[key];
CollectionType type = new CollectionType(metaColl);
CollectionType type = new CollectionType(metaColl, graphQLService.Collections, graphQLService);
ListGraphType listType = new ListGraphType(type);

AddField(new FieldType
{
Name = metaColl.CollectionName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,61 @@
using GraphQL.Http;
using GraphQL.Instrumentation;
using GraphQL.Types;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RawCMS.Library.Core.Attributes;
using RawCMS.Plugins.GraphQL.Classes;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace RawCMS.Plugins.GraphQL.Classes
namespace RawCMS.Plugins.GraphQL.Controllers
{
public class GraphQLMiddleware
[AllowAnonymous]
[RawAuthentication]
[Route("api/graphql")]
public class GraphQLController : Controller
{
private readonly RequestDelegate _next;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
private readonly GraphQLService _service;
private readonly ISchema _schema;

public GraphQLMiddleware(
RequestDelegate next,
IDocumentExecuter executer,
public GraphQLController(IDocumentExecuter executer,
IDocumentWriter writer,
GraphQLService graphQLService
)
GraphQLService graphQLService,
ISchema schema)
{
_next = next;
_executer = executer;
_writer = writer;
_service = graphQLService;
_schema = schema;
}

public async Task Invoke(HttpContext context, ISchema schema)
public static T Deserialize<T>(Stream s)
{
if (!IsGraphQLRequest(context))
using (StreamReader reader = new StreamReader(s))
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
await _next(context);
return;
JsonSerializer ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}

await ExecuteAsync(context, schema);
}

private bool IsGraphQLRequest(HttpContext context)
{
return context.Request.Path.StartsWithSegments(_service.Settings.Path)
&& string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase);
}

private async Task ExecuteAsync(HttpContext context, ISchema schema)
[HttpPost]
public async Task<ExecutionResult> Post([FromBody]GraphQLRequest request)
{
GraphQLRequest t = Deserialize<GraphQLRequest>(HttpContext.Request.Body);
DateTime start = DateTime.UtcNow;

GraphQLRequest request = Deserialize<GraphQLRequest>(context.Request.Body);

ExecutionResult result = await _executer.ExecuteAsync(_ =>
{
_.Schema = schema;
_.Schema = _schema;
_.Query = request.Query;
_.OperationName = request.OperationName;
_.Inputs = request.Variables.ToInputs();
_.UserContext = _service.Settings.BuildUserContext?.Invoke(context);
_.UserContext = _service.Settings.BuildUserContext?.Invoke(HttpContext);
_.EnableMetrics = _service.Settings.EnableMetrics;
if (_service.Settings.EnableMetrics)
{
Expand All @@ -82,25 +77,7 @@ private async Task ExecuteAsync(HttpContext context, ISchema schema)
result.EnrichWithApolloTracing(start);
}

await WriteResponseAsync(context, result);
}

private async Task WriteResponseAsync(HttpContext context, ExecutionResult result)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = result.Errors?.Any() == true ? (int)HttpStatusCode.BadRequest : (int)HttpStatusCode.OK;

await _writer.WriteAsync(context.Response.Body, result);
}

public static T Deserialize<T>(Stream s)
{
using (StreamReader reader = new StreamReader(s))
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}
return result;
}
}
}
12 changes: 0 additions & 12 deletions Plugins/RawCMS.Plugins.GraphQL/GraphQLPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ public override void ConfigureServices(IServiceCollection services)
services.AddSingleton<IDependencyResolver>(s => new FuncDependencyResolver(s.GetRequiredService));
services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
services.AddSingleton<IDocumentWriter, DocumentWriter>();
//services.AddSingleton<ICollectionMetadata, CollectionMetadataService>();
services.AddScoped<ISchema, GraphQLSchema>();
services.AddSingleton<GraphQLQuery>();
//services.AddSingleton<GraphQLService>();
services.AddSingleton(x => graphService);
}

Expand All @@ -60,16 +58,6 @@ public override void Configure(IApplicationBuilder app, AppEngine appEngine)

base.Configure(app, appEngine);

app.UseMiddleware<GraphQLMiddleware>();
//app.UseMiddleware<GraphQLMiddleware>(new GraphQLSettings
//{
// BuildUserContext = ctx => new GraphQLUserContext
// {
// User = ctx.User
// },
// EnableMetrics = this.config.EnableMetrics
//});

app.UseGraphiQl(config.GraphiQLPath, config.Path);
}

Expand Down
46 changes: 36 additions & 10 deletions Plugins/RawCMS.Plugins.GraphQL/Types/CollectionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
//******************************************************************************
using GraphQL;
using GraphQL.Types;
using Newtonsoft.Json.Linq;
using RawCMS.Library.Schema;
using RawCMS.Plugins.GraphQL.Classes;
using System;
using System.Collections.Generic;
using System.Linq;

namespace RawCMS.Plugins.GraphQL.Types
{
Expand All @@ -36,7 +39,8 @@ protected IDictionary<FieldBaseType, Type> FieldTypeToSystemType
{ FieldBaseType.Float, typeof(float) },
{ FieldBaseType.ID, typeof(Guid) },
{ FieldBaseType.Int, typeof(int) },
{ FieldBaseType.String, typeof(string) }
{ FieldBaseType.String, typeof(string) },
{ FieldBaseType.Object, typeof(JObject) }
};
}

Expand All @@ -54,26 +58,48 @@ private Type ResolveFieldMetaType(FieldBaseType type)
return typeof(string);
}

public CollectionType(CollectionSchema collectionSchema)
public CollectionType(CollectionSchema collectionSchema, Dictionary<string, CollectionSchema> collections = null, GraphQLService graphQLService = null)
{
Name = collectionSchema.CollectionName;

foreach (Field field in collectionSchema.FieldSettings)
{
InitGraphField(field);
InitGraphField(field, collections, graphQLService);
}
}

private void InitGraphField(Field field)
private void InitGraphField(Field field, Dictionary<string, CollectionSchema> collections = null, GraphQLService graphQLService = null)
{
Type graphQLType = (ResolveFieldMetaType(field.BaseType)).GetGraphTypeFromType(!field.Required);
FieldType columnField = Field(
Type graphQLType;
if (field.BaseType == FieldBaseType.Object)
{
var relatedObject = collections[field.Type];
var relatedCollection = new CollectionType(relatedObject, collections);
var listType = new ListGraphType(relatedCollection);
graphQLType = relatedCollection.GetType();
FieldType columnField = Field(
graphQLType,
field.Name
);
relatedObject.CollectionName);

columnField.Resolver = new NameFieldResolver();
FillArgs(field.Name, graphQLType);
columnField.Resolver = new NameFieldResolver();
columnField.Arguments = new QueryArguments(relatedCollection.TableArgs);
foreach(var arg in columnField.Arguments.Where(x=>!(new string[] { "pageNumber", "pageSize", "rawQuery", "_id" }.Contains(x.Name))).ToList())
{
arg.Name = $"{relatedObject.CollectionName}_{arg.Name}";
TableArgs.Add(arg);
}
}
else
{
//graphQLType = (ResolveFieldMetaType(field.BaseType)).GetGraphTypeFromType(!field.Required);
graphQLType = (ResolveFieldMetaType(field.BaseType)).GetGraphTypeFromType(true);
FieldType columnField = Field(
graphQLType,
field.Name);

columnField.Resolver = new NameFieldResolver();
FillArgs(field.Name, graphQLType);
}
}

private void FillArgs(string name, Type graphType)
Expand Down
4 changes: 2 additions & 2 deletions Plugins/RawCMS.Plugins.GraphQL/Types/JObjectFieldResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ private string BuildMongoQuery(Dictionary<string, object> arguments)
["$regex"] = $"/*{arguments[key]}/*",
["$options"] = "si"
};
dictionary[key.ToPascalCase()] = reg;
dictionary[key.ToPascalCase().Replace("_",".")] = reg;
}
else
{
dictionary[key.ToPascalCase()] = arguments[key];
dictionary[key.ToPascalCase().Replace("_", ".")] = arguments[key];
}
}
query = JsonConvert.SerializeObject(dictionary, jSettings);
Expand Down
41 changes: 41 additions & 0 deletions RawCMS.Client/BLL/Core/ClientConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Minà</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using Microsoft.Extensions.Configuration;
using System.IO;

namespace RawCMS.Client.BLL.Core
{
public class ClientConfig
{
private static IConfigurationBuilder _builder = null;
private static IConfigurationRoot _configuration = null;

public static IConfigurationRoot Config
{
get
{
if (_configuration == null)
{
_builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

_configuration = _builder.Build();
}
return _configuration;
}
set { }
}

public static T GetValue<T>(string key)
{
return Config.GetValue<T>(key);
}
}
}
44 changes: 44 additions & 0 deletions RawCMS.Client/BLL/Core/ConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Minà</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using System;

namespace RawCMS.Client.BLL.Core
{
public class ConfigFile
{
public string Token { get; set; }
public string ServerUrl { get; set; }
public string User { get; set; }
public string CreatedTime { get; set; }

public ConfigFile()
{
}

public ConfigFile(string content)
{
try
{
ConfigFile cf = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigFile>(content);
Token = cf.Token;
ServerUrl = cf.ServerUrl;
User = cf.User;
CreatedTime = cf.CreatedTime;
}
catch (Exception)
{
}
}

public override string ToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
}
}
Loading

0 comments on commit f72102a

Please sign in to comment.