Skip to content

Commit 9b6f074

Browse files
committed
Move classes to own files
1 parent 39be2f5 commit 9b6f074

14 files changed

+517
-484
lines changed
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+
}

src/Our.Umbraco.GraphQL/Schema/UmbracoSchema.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static IEnumerable<IGraphType> CreateGraphTypes(
130130
}
131131
};
132132

133-
graphType.Interface<PublishedContentGraphType>();
133+
graphType.Interface<PublishedContentInterfaceGraphType>();
134134
foreach (var composition in contentType.ContentTypeComposition)
135135
{
136136
if (interfaceGraphTypes.TryGetValue(composition.Alias, out IInterfaceGraphType interfaceType))

src/Our.Umbraco.GraphQL/Types/ComplexGraphTypeOfIPublishedContentExtensions.cs

Lines changed: 4 additions & 453 deletions
Large diffs are not rendered by default.
Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using GraphQL;
6+
using GraphQL.Language.AST;
7+
using GraphQL.Types;
8+
using Our.Umbraco.GraphQL.Filters;
9+
10+
namespace Our.Umbraco.GraphQL.Types
11+
{
12+
public class FilterGraphType<T> : InputObjectGraphType where T : GraphType
13+
{
14+
public FilterGraphType(IComplexGraphType graphType)
15+
{
16+
Type = typeof(T);
17+
Name = $"{Type.GraphQLName()}Filter";
18+
19+
AddFilterField(
20+
new ListGraphType(new NonNullGraphType(this)),
21+
graphType,
22+
"AND",
23+
$"Combines all passed `{Name}` objects with logical AND",
24+
subFilters => new AndFilter(subFilters)
25+
);
26+
27+
AddFilterField(
28+
new ListGraphType(new NonNullGraphType(this)),
29+
graphType,
30+
"OR",
31+
$"Combines all passed `{Name}` objects with logical OR",
32+
subFilters => new OrFilter(subFilters)
33+
);
34+
35+
foreach (var field in graphType.Fields)
36+
{
37+
if (field.GetMetadata("disableFiltering", false))
38+
{
39+
continue;
40+
}
41+
42+
FilterField(graphType, field);
43+
}
44+
45+
// relation
46+
// _every
47+
// _some
48+
// _none
49+
}
50+
51+
public void FilterField(IGraphType sourceType, FieldType fieldType)
52+
{
53+
var namedType = fieldType.Type.GetNamedType();
54+
var graphType = new Lazy<IGraphType>(() => namedType.BuildNamedType());
55+
56+
if (namedType == typeof(EnumerationGraphType) ||
57+
namedType.IsGenericType && namedType.GetGenericTypeDefinition() == typeof(EnumerationGraphType<>) ||
58+
namedType == typeof(BooleanGraphType) ||
59+
namedType == typeof(IntGraphType) ||
60+
namedType == typeof(FloatGraphType) ||
61+
namedType == typeof(DateGraphType) ||
62+
namedType == typeof(StringGraphType) ||
63+
namedType == typeof(IdGraphType))
64+
{
65+
AddFilterField(
66+
graphType.Value,
67+
fieldType,
68+
sourceType,
69+
fieldType.Name,
70+
"matches all nodes with exact value",
71+
(resolver, source) => new EqFilter(resolver, source)
72+
);
73+
74+
AddFilterField(
75+
graphType.Value,
76+
fieldType,
77+
sourceType,
78+
$"{fieldType.Name}_not",
79+
"matches all nodes with different value",
80+
(resolver, source) => new NotFilter(new EqFilter(resolver, source))
81+
);
82+
}
83+
84+
if (namedType == typeof(EnumerationGraphType) ||
85+
namedType.IsGenericType && namedType.GetGenericTypeDefinition() == typeof(EnumerationGraphType<>) ||
86+
namedType == typeof(IntGraphType) ||
87+
namedType == typeof(FloatGraphType) ||
88+
namedType == typeof(DateGraphType) ||
89+
namedType == typeof(StringGraphType) ||
90+
namedType == typeof(IdGraphType))
91+
{
92+
AddFilterField(
93+
typeof(ListGraphType<>).MakeGenericType(typeof(NonNullGraphType<>).MakeGenericType(namedType)).BuildNamedType(),
94+
fieldType,
95+
sourceType,
96+
$"{fieldType.Name}_in",
97+
"matches all nodes with value in the passed list",
98+
(resolver, source) => new InFilter(resolver, source)
99+
);
100+
101+
AddFilterField(
102+
typeof(ListGraphType<>).MakeGenericType(typeof(NonNullGraphType<>).MakeGenericType(namedType)).BuildNamedType(),
103+
fieldType,
104+
sourceType,
105+
$"{fieldType.Name}_not_in",
106+
"matches all nodes with value not in the passed list",
107+
(resolver, source) => new EqFilter(resolver, source)
108+
);
109+
}
110+
if (namedType == typeof(IntGraphType) ||
111+
namedType == typeof(FloatGraphType) ||
112+
namedType == typeof(DateGraphType) ||
113+
namedType == typeof(StringGraphType) ||
114+
namedType == typeof(IdGraphType))
115+
{
116+
AddFilterField(
117+
graphType.Value,
118+
fieldType,
119+
sourceType,
120+
$"{fieldType.Name}_lt",
121+
"matches all nodes with lesser value",
122+
(resolver, source) => new LtFilter(resolver, source)
123+
);
124+
125+
AddFilterField(
126+
graphType.Value,
127+
fieldType,
128+
sourceType,
129+
$"{fieldType.Name}_lte",
130+
"matches all nodes with lesser or equal value",
131+
(resolver, source) => new LteFilter(resolver, source)
132+
);
133+
134+
AddFilterField(
135+
graphType.Value,
136+
fieldType,
137+
sourceType,
138+
$"{fieldType.Name}_gt",
139+
"matches all nodes with greater value",
140+
(resolver, source) => new GtFilter(resolver, source)
141+
);
142+
143+
AddFilterField(
144+
graphType.Value,
145+
fieldType,
146+
sourceType,
147+
$"{fieldType.Name}_gte",
148+
"matches all nodes with greater or equal value",
149+
(resolver, source) => new GteFilter(resolver, source)
150+
);
151+
}
152+
153+
if (namedType == typeof(StringGraphType) || namedType == typeof(IdGraphType))
154+
{
155+
AddFilterField(
156+
graphType.Value,
157+
fieldType,
158+
sourceType,
159+
$"{fieldType.Name}_contains",
160+
"matches all nodes with a value that contains given substring",
161+
(resolver, source) => new ContainsFilter(resolver, source)
162+
);
163+
164+
AddFilterField(
165+
graphType.Value,
166+
fieldType,
167+
sourceType,
168+
$"{fieldType.Name}_not_contains",
169+
"matches all nodes with a value that does not contain given substring",
170+
(resolver, source) => new NotFilter(new ContainsFilter(resolver, source))
171+
);
172+
173+
AddFilterField(
174+
graphType.Value,
175+
fieldType,
176+
sourceType,
177+
$"{fieldType.Name}_starts_with",
178+
"matches all nodes with a value that starts with given substring",
179+
(resolver, source) => new StartsWithFilter(resolver, source)
180+
);
181+
182+
AddFilterField(
183+
graphType.Value,
184+
fieldType,
185+
sourceType,
186+
$"{fieldType.Name}_not_starts_with",
187+
"matches all nodes with a value that does not start with given substring",
188+
(resolver, source) => new NotFilter(new StartsWithFilter(resolver, source))
189+
);
190+
191+
AddFilterField(
192+
graphType.Value,
193+
fieldType,
194+
sourceType,
195+
$"{fieldType.Name}_ends_with",
196+
"matches all nodes with a value that ends with given substring",
197+
(resolver, source) => new EndsWithFilter(resolver, source)
198+
);
199+
200+
AddFilterField(
201+
graphType.Value,
202+
fieldType,
203+
sourceType,
204+
$"{fieldType.Name}_not_ends_with",
205+
"matches all nodes with a value that does not end with given substring",
206+
(resolver, source) => new NotFilter(new EndsWithFilter(resolver, source))
207+
);
208+
}
209+
}
210+
211+
protected void AddFilterField(
212+
IGraphType field,
213+
FieldType fieldType,
214+
IGraphType parentType,
215+
string name,
216+
string description,
217+
Func<Func<object, object>, object, IFilter> resolveFilter)
218+
{
219+
Field(field.GetType(),
220+
name,
221+
description,
222+
deprecationReason: fieldType.DeprecationReason,
223+
resolve: context =>
224+
{
225+
object ResolveValue(object source)
226+
{
227+
var path = (context.Path ?? new [] { "filter" }).Concat(new[] { fieldType.Name });
228+
var value = fieldType.Resolver.Resolve(
229+
new ResolveFieldContext
230+
{
231+
FieldName = fieldType.Name,
232+
FieldAst = new Field(null, new NameNode(fieldType.Name)),
233+
FieldDefinition = fieldType,
234+
ParentType = (IObjectGraphType)parentType,
235+
Source = source,
236+
Schema = context.Schema,
237+
Document = context.Document,
238+
Fragments = context.Fragments,
239+
RootValue = context.RootValue,
240+
UserContext = context.UserContext,
241+
Operation = context.Operation,
242+
Variables = context.Variables,
243+
CancellationToken = context.CancellationToken,
244+
Metrics = context.Metrics,
245+
Errors = context.Errors,
246+
Path = path
247+
}
248+
);
249+
250+
if (value is Task<object> task)
251+
{
252+
return task.Result;
253+
}
254+
255+
return value;
256+
}
257+
258+
return resolveFilter(ResolveValue, context.Source);
259+
}).ResolvedType = field;
260+
}
261+
262+
protected void AddFilterField(
263+
IGraphType field,
264+
IGraphType parentType,
265+
string name,
266+
string description,
267+
Func<IEnumerable<IFilter>, IFilter> resolveFilter)
268+
{
269+
Field(field.GetType(),
270+
name,
271+
description,
272+
resolve: context =>
273+
{
274+
var value = (IEnumerable<object>)context.Source;
275+
var subFilters = new List<IFilter>();
276+
foreach (var obj in value)
277+
{
278+
var dict = (IDictionary<string, object>)obj;
279+
foreach (KeyValuePair<string, object> pair in dict)
280+
{
281+
subFilters.Add(this.ResolveFilter(pair, context));
282+
}
283+
}
284+
return resolveFilter(subFilters);
285+
}).ResolvedType = field;
286+
}
287+
288+
public Type Type { get; }
289+
290+
public override string CollectTypes(TypeCollectionContext context)
291+
{
292+
var innerType = context.ResolveType(Type);
293+
var name = innerType.CollectTypes(context);
294+
context.AddType(name, innerType, context);
295+
return Name;
296+
}
297+
}
298+
}

0 commit comments

Comments
 (0)