Skip to content

Commit 5af879b

Browse files
authored
Merge pull request #38 from rasmusjp/feature/refactoring
Refactoring
2 parents 94f1273 + c72fbc0 commit 5af879b

30 files changed

+665
-627
lines changed

samples/Website/Web.config.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@
354354
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
355355
<dependentAssembly>
356356
<assemblyIdentity name="System.Web.Cors" publicKeyToken="31BF3856AD364E35" culture="neutral" />
357-
<bindingRedirect oldVersion="0.0.0.0-5.2.6.0" newVersion="5.2.6.0" />
357+
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
358358
</dependentAssembly>
359359
<dependentAssembly>
360360
<assemblyIdentity name="System.Threading" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
@@ -678,6 +678,10 @@
678678
<assemblyIdentity name="Microsoft.AspNet.SignalR.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
679679
<bindingRedirect oldVersion="0.0.0.0-2.2.3.0" newVersion="2.2.3.0" />
680680
</dependentAssembly>
681+
<dependentAssembly>
682+
<assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" culture="neutral" />
683+
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
684+
</dependentAssembly>
681685
</assemblyBinding>
682686
</runtime>
683687

samples/Website/Website.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
121121
<Reference Include="Microsoft.Owin, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
122122
<HintPath>..\..\packages\Microsoft.Owin.3.1.0\lib\net45\Microsoft.Owin.dll</HintPath>
123123
</Reference>
124+
<Reference Include="Microsoft.Owin.Cors, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
125+
<HintPath>..\..\packages\Microsoft.Owin.Cors.3.1.0\lib\net45\Microsoft.Owin.Cors.dll</HintPath>
126+
</Reference>
124127
<Reference Include="Microsoft.Owin.Host.SystemWeb, Version=3.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
125128
<HintPath>..\..\packages\Microsoft.Owin.Host.SystemWeb.3.1.0\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
126129
</Reference>
@@ -262,6 +265,9 @@
262265
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
263266
<HintPath>..\..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
264267
</Reference>
268+
<Reference Include="System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
269+
<HintPath>..\..\packages\Microsoft.AspNet.Cors.5.2.3\lib\net45\System.Web.Cors.dll</HintPath>
270+
</Reference>
265271
<Reference Include="System.Web.DynamicData" />
266272
<Reference Include="System.Web.Entity" />
267273
<Reference Include="System.Web.ApplicationServices" />

samples/Website/packages.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<package id="Log4Net.Async" version="2.0.4" targetFramework="net462" />
1313
<package id="Lucene.Net" version="2.9.4.1" targetFramework="net462" />
1414
<package id="Markdown" version="1.14.7" targetFramework="net462" />
15+
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net462" />
1516
<package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net462" />
1617
<package id="Microsoft.AspNet.Identity.Owin" version="2.2.1" targetFramework="net462" />
1718
<package id="Microsoft.AspNet.Mvc" version="5.2.6" targetFramework="net462" />
@@ -29,6 +30,7 @@
2930
<package id="Microsoft.IO.RecyclableMemoryStream" version="1.2.2" targetFramework="net462" />
3031
<package id="Microsoft.Net.Compilers" version="2.8.2" targetFramework="net462" developmentDependency="true" />
3132
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net462" />
33+
<package id="Microsoft.Owin.Cors" version="3.1.0" targetFramework="net462" />
3234
<package id="Microsoft.Owin.Host.SystemWeb" version="3.1.0" targetFramework="net462" />
3335
<package id="Microsoft.Owin.Security" version="3.1.0" targetFramework="net462" />
3436
<package id="Microsoft.Owin.Security.Cookies" version="3.1.0" targetFramework="net462" />

src/Our.Umbraco.GraphQL/Constants.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Our.Umbraco.GraphQL
2+
{
3+
public class Constants
4+
{
5+
public const string ProductName = "Our.Umbraco.GraphQL";
6+
}
7+
}

src/Our.Umbraco.GraphQL/Events/MigrationEventHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class MigrationEventHandler : ApplicationEventHandler
2020
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
2121
{
2222
// Update the version number as we deploy new DB changes to run any new Migrations
23-
ApplyMigrations(applicationContext, "GraphQL", new SemVersion(1, 0, 0));
23+
ApplyMigrations(applicationContext, Constants.ProductName, new SemVersion(0, 1, 0));
2424
}
2525

2626
private void ApplyMigrations(ApplicationContext applicationContext, string productName, SemVersion targetVersion)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using GraphQL.Language.AST;
2+
using GraphQL.Types;
3+
using Our.Umbraco.GraphQL.Filters;
4+
using Our.Umbraco.GraphQL.Models;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
10+
namespace Our.Umbraco.GraphQL
11+
{
12+
internal static class FilterUtils
13+
{
14+
public static IEnumerable<TSource> Filter<TSource, TParent>(
15+
this IEnumerable<TSource> source,
16+
ResolveFieldContext<TParent> context)
17+
{
18+
var orderByArg = context.GetArgument<IEnumerable<object>>("orderBy")?.OfType<OrderBy>();
19+
20+
if (context.Arguments.TryGetValue("filter", out object filterArg) && filterArg != null)
21+
{
22+
var rootFilter = ((IDictionary<string, object>)filterArg).First();
23+
var filterType = (IComplexGraphType)context.FieldDefinition.Arguments.Find("filter").ResolvedType;
24+
var filter = ResolveFilter(filterType, rootFilter, context);
25+
26+
source = source.Where(x => filter.IsSatisfiedBy(x));
27+
}
28+
if (orderByArg != null)
29+
{
30+
foreach (var order in orderByArg)
31+
{
32+
FieldType fieldType = context.ParentType.Fields.Single(x => x.Name == order.Field);
33+
34+
object KeySelector(TSource item)
35+
{
36+
var path = (context.Path ?? new[] { "order" }).Concat(new[] { fieldType.Name });
37+
var value = fieldType.Resolver.Resolve(new ResolveFieldContext
38+
{
39+
FieldName = order.Field,
40+
FieldAst = new Field(null, new NameNode(order.Field)),
41+
FieldDefinition = fieldType,
42+
ParentType = context.ParentType,
43+
Source = item,
44+
Schema = context.Schema,
45+
Document = context.Document,
46+
Fragments = context.Fragments,
47+
RootValue = context.RootValue,
48+
UserContext = context.UserContext,
49+
Operation = context.Operation,
50+
Variables = context.Variables,
51+
CancellationToken = context.CancellationToken,
52+
Metrics = context.Metrics,
53+
Errors = context.Errors,
54+
Path = path
55+
});
56+
if (value is Task<object> task)
57+
{
58+
return task.Result;
59+
}
60+
return value;
61+
}
62+
63+
if (source is IOrderedEnumerable<TSource> ordered)
64+
{
65+
source = order.Order == SortOrder.Ascending
66+
? ordered.ThenBy(KeySelector)
67+
: ordered.ThenByDescending(KeySelector);
68+
}
69+
else
70+
{
71+
source = order.Order == SortOrder.Ascending
72+
? source.OrderBy(KeySelector)
73+
: source.OrderByDescending(KeySelector);
74+
}
75+
}
76+
77+
}
78+
return source;
79+
}
80+
81+
internal static IFilter ResolveFilter<TParentType>(
82+
this IComplexGraphType filterType,
83+
KeyValuePair<string, object> value,
84+
ResolveFieldContext<TParentType> context)
85+
{
86+
var path = (context.Path ?? new[] { "filter" }).Concat(new[] { value.Key });
87+
var field = filterType.Fields.First(x => x.Name == value.Key);
88+
89+
return (IFilter)field.Resolver.Resolve(
90+
new ResolveFieldContext
91+
{
92+
FieldName = field.Name,
93+
FieldAst = new Field(null, new NameNode(field.Name)),
94+
FieldDefinition = field,
95+
ParentType = context.ParentType,
96+
Source = value.Value,
97+
Schema = context.Schema,
98+
Document = context.Document,
99+
Fragments = context.Fragments,
100+
RootValue = context.RootValue,
101+
UserContext = context.UserContext,
102+
Operation = context.Operation,
103+
Variables = context.Variables,
104+
CancellationToken = context.CancellationToken,
105+
Metrics = context.Metrics,
106+
Errors = context.Errors,
107+
Path = path,
108+
}
109+
);
110+
}
111+
}
112+
}

src/Our.Umbraco.GraphQL/GraphQLAuthenticationExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public static void SetDoctypeMetadata(this FieldType type, string doctypeAlias)
9494
}
9595
}
9696

97+
public static FieldBuilder<TSourceType, TReturnType> SetDoctypeMetadata<TSourceType, TReturnType>(
98+
this FieldBuilder<TSourceType, TReturnType> builder, string doctypeAlias)
99+
{
100+
SetDoctypeMetadata(builder.FieldType, doctypeAlias);
101+
return builder;
102+
}
103+
97104
public static FieldBuilder<TSourceType, TReturnType> RequirePermission<TSourceType, TReturnType>(
98105
this FieldBuilder<TSourceType, TReturnType> builder, string permission)
99106
{

src/Our.Umbraco.GraphQL/Migrations/CreateInitialTables.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Our.Umbraco.GraphQL.Migrations
1111
{
12-
[Migration("1.0.0", 1, "GraphQL")]
12+
[Migration("0.1.0", 1, Constants.ProductName)]
1313
public class CreateInitialTables : MigrationBase
1414
{
1515
public const string accountsTableName = "Accounts";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Our.Umbraco.GraphQL.Models
2+
{
3+
public class OrderBy
4+
{
5+
public string Field { get; }
6+
public SortOrder Order { get; }
7+
8+
public OrderBy(string field, SortOrder order)
9+
{
10+
Field = field;
11+
Order = order;
12+
}
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Our.Umbraco.GraphQL.Models
2+
{
3+
4+
public enum SortOrder
5+
{
6+
Ascending,
7+
Descending
8+
}
9+
}

0 commit comments

Comments
 (0)