2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Reflection ;
5
+ using System . Threading ;
5
6
using System . Threading . Tasks ;
6
7
using GraphQL ;
7
8
using GraphQL . Types ;
8
9
using Our . Umbraco . GraphQL . Adapters . Resolvers ;
10
+ using Our . Umbraco . GraphQL . Adapters . Types ;
9
11
using Our . Umbraco . GraphQL . Adapters . Types . Relay ;
10
12
using Our . Umbraco . GraphQL . Adapters . Types . Resolution ;
11
13
using Our . Umbraco . GraphQL . Adapters . Visitors ;
12
14
using Our . Umbraco . GraphQL . Attributes ;
15
+ using Our . Umbraco . GraphQL . Reflection ;
16
+ using Our . Umbraco . GraphQL . Types ;
13
17
using Our . Umbraco . GraphQL . Types . Relay ;
14
18
15
19
namespace Our . Umbraco . GraphQL . Adapters
@@ -38,7 +42,7 @@ public IGraphType Adapt(TypeInfo typeInfo)
38
42
{
39
43
if ( typeInfo == null ) throw new ArgumentNullException ( nameof ( typeInfo ) ) ;
40
44
41
- var unwrappedTypeInfo = UnwrapTypeInfo ( typeInfo ) ;
45
+ var unwrappedTypeInfo = typeInfo . Unwrap ( ) ;
42
46
var graphType = TryGetFromCache ( unwrappedTypeInfo , typeInfo ) ;
43
47
if ( graphType != null ) return graphType ;
44
48
@@ -65,7 +69,7 @@ public IGraphType Adapt(TypeInfo typeInfo)
65
69
66
70
private IGraphType AdaptInput ( TypeInfo typeInfo )
67
71
{
68
- var unwrappedTypeInfo = UnwrapTypeInfo ( typeInfo ) ;
72
+ var unwrappedTypeInfo = typeInfo . Unwrap ( ) ;
69
73
var graphType = TryGetFromCache ( unwrappedTypeInfo , typeInfo ) ;
70
74
if ( graphType != null ) return graphType ;
71
75
if ( unwrappedTypeInfo . IsEnum )
@@ -110,7 +114,7 @@ private FieldType CreateField(MemberInfo memberInfo)
110
114
{
111
115
var returnType = GetReturnType ( memberInfo ) ;
112
116
113
- var unwrappedReturnType = UnwrapTypeInfo ( returnType ) ;
117
+ var unwrappedReturnType = returnType . Unwrap ( ) ;
114
118
115
119
var foundType = _typeRegistry . Get ( unwrappedReturnType ) ;
116
120
@@ -160,7 +164,7 @@ private FieldType CreateField(MemberInfo memberInfo)
160
164
Metadata = { { nameof ( MemberInfo ) , memberInfo } } ,
161
165
Name = memberInfo . GetCustomAttribute < NameAttribute > ( ) ? . Name ?? memberInfo . Name ,
162
166
ResolvedType = resolvedType ,
163
- Type = WrapTypeInfo ( foundType , returnType , isNonNull , isNonNullItem )
167
+ Type = foundType . Wrap ( returnType , isNonNull , isNonNullItem )
164
168
} ;
165
169
166
170
fieldType . Resolver = new FieldResolver ( fieldType , _dependencyResolver ) ;
@@ -188,7 +192,40 @@ private QueryArgument CreateArgument(ParameterInfo parameterInfo)
188
192
parameterInfo . GetCustomAttribute < DefaultValueAttribute > ( ) != null ||
189
193
parameterType . IsValueType && parameterType . IsNullable ( ) ;
190
194
191
- var inputType = AdaptInput ( parameterType . GetTypeInfo ( ) ) ;
195
+ var unwrappedType = parameterType . GetTypeInfo ( ) . Unwrap ( ) ;
196
+
197
+ IGraphType inputType ;
198
+ if ( unwrappedType == typeof ( OrderBy ) )
199
+ {
200
+ var returnType = GetReturnType ( parameterInfo . Member ) ;
201
+ if ( returnType . IsGenericType && returnType . GetGenericTypeDefinition ( ) == typeof ( Connection < > ) )
202
+ returnType = returnType . GenericTypeArguments [ 0 ] . GetTypeInfo ( ) ;
203
+
204
+ var foundType = _typeRegistry . Get ( returnType ) ;
205
+
206
+ IGraphType graphType ;
207
+
208
+ if ( foundType != null )
209
+ {
210
+ graphType = Activator . CreateInstance ( foundType ) as IComplexGraphType ;
211
+ }
212
+ else
213
+ {
214
+ graphType = Adapt ( returnType ) ;
215
+ }
216
+
217
+ inputType = ( IGraphType ) Activator . CreateInstance ( typeof ( OrderByGraphType ) , graphType ) ;
218
+
219
+ if ( parameterType . IsArray )
220
+ {
221
+ inputType = WrapList ( WrapNonNull ( inputType ) ) ;
222
+ }
223
+ }
224
+ else
225
+ {
226
+ inputType = AdaptInput ( parameterType . GetTypeInfo ( ) ) ;
227
+ }
228
+
192
229
if ( hasDefaultValue == false && ! ( inputType is NonNullGraphType ) )
193
230
{
194
231
inputType = WrapNonNull ( inputType ) ;
@@ -299,45 +336,9 @@ private IGraphType TryGetFromCache(TypeInfo typeInfo, TypeInfo unwrappedTypeInfo
299
336
return Wrap ( unwrappedTypeInfo , ( IGraphType ) Activator . CreateInstance ( foundType ) ) ;
300
337
}
301
338
302
- private static TypeInfo UnwrapTypeInfo ( TypeInfo typeInfo )
303
- {
304
- if ( typeInfo . IsGenericType && typeInfo . GetGenericTypeDefinition ( ) == typeof ( Task < > ) )
305
- typeInfo = typeInfo . GenericTypeArguments [ 0 ] . GetTypeInfo ( ) ;
306
-
307
- var isNullable = typeInfo . IsNullable ( ) ;
308
- if ( isNullable )
309
- return typeInfo . GenericTypeArguments [ 0 ] . GetTypeInfo ( ) ;
310
-
311
- var enumerableArgument = GetEnumerableArgument ( typeInfo ) ;
312
- if ( enumerableArgument != null )
313
- return enumerableArgument . GetTypeInfo ( ) ;
314
-
315
- return typeInfo ;
316
- }
317
-
318
- private TypeInfo WrapTypeInfo ( TypeInfo graphType , TypeInfo typeInfo , bool isNonNull , bool isNonNullItem )
319
- {
320
- if ( graphType == null )
321
- return null ;
322
-
323
- var enumerableArgument = GetEnumerableArgument ( typeInfo ) ;
324
-
325
- if ( typeInfo . IsValueType && typeInfo . IsNullable ( ) == false || enumerableArgument != null &&
326
- ( enumerableArgument . IsValueType && enumerableArgument . IsNullable ( ) == false || isNonNullItem ) )
327
- graphType = typeof ( NonNullGraphType < > ) . MakeGenericType ( graphType ) . GetTypeInfo ( ) ;
328
-
329
- if ( enumerableArgument != null )
330
- graphType = typeof ( ListGraphType < > ) . MakeGenericType ( graphType ) . GetTypeInfo ( ) ;
331
-
332
- if ( isNonNull && typeof ( NonNullGraphType ) . IsAssignableFrom ( graphType ) == false )
333
- graphType = typeof ( NonNullGraphType < > ) . MakeGenericType ( graphType ) . GetTypeInfo ( ) ;
334
-
335
- return graphType ;
336
- }
337
-
338
339
private IGraphType Wrap ( TypeInfo typeInfo , IGraphType graphType )
339
340
{
340
- var enumerableArgument = GetEnumerableArgument ( typeInfo ) ;
341
+ var enumerableArgument = typeInfo . GetEnumerableArgument ( ) ;
341
342
342
343
if ( typeInfo . IsValueType && typeInfo . IsNullable ( ) == false || enumerableArgument != null &&
343
344
enumerableArgument . IsValueType && enumerableArgument . IsNullable ( ) == false )
@@ -366,23 +367,6 @@ private static IGraphType WrapNonNull(IGraphType graphType)
366
367
return nonNullGraphType ;
367
368
}
368
369
369
- private static Type GetEnumerableArgument ( TypeInfo typeInfo )
370
- {
371
- if ( typeInfo . IsGenericType && typeInfo . GetGenericTypeDefinition ( ) == typeof ( Task < > ) )
372
- typeInfo = typeInfo . GenericTypeArguments [ 0 ] . GetTypeInfo ( ) ;
373
-
374
- if ( typeInfo == typeof ( string ) )
375
- return null ;
376
-
377
- if ( typeInfo . IsGenericType && typeInfo . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) )
378
- return typeInfo . GenericTypeArguments [ 0 ] ;
379
-
380
- var enumerableInterface = typeInfo . ImplementedInterfaces . FirstOrDefault ( x =>
381
- x . IsGenericType && x . GetGenericTypeDefinition ( ) == typeof ( IEnumerable < > ) ) ;
382
-
383
- return enumerableInterface ? . GenericTypeArguments [ 0 ] ;
384
- }
385
-
386
370
private bool IsValidReturnType ( Type type ) =>
387
371
type != typeof ( void ) && type != typeof ( object ) && type != typeof ( Task ) ;
388
372
}
0 commit comments