-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathMetaDataExtensions.cs
More file actions
112 lines (95 loc) · 3.45 KB
/
MetaDataExtensions.cs
File metadata and controls
112 lines (95 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Linq;
using GraphQL.Builders;
using GraphQL.Types;
namespace Graphql.Extensions.FieldEnums.Types.Extensions
{
public static class MetaDataExtensions
{
public static FieldBuilder<TSourceType, TProperty> WithOriginalName<TSourceType, TProperty>
(
this FieldBuilder<TSourceType, TProperty> fieldBuilder,
string originalName
)
{
fieldBuilder.FieldType.Metadata[SharedConstants.OriginalPropertyName] = originalName;
return fieldBuilder;
}
public static string GetOriginalName(this FieldType field)
{
if (field.Metadata.TryGetValue(SharedConstants.OriginalPropertyName, out var originalName))
{
return originalName.ToString();
}
return field.Name;
}
public static FieldBuilder<TSourceType, TProperty> WithSourceType<TSourceType, TProperty>
(
this FieldBuilder<TSourceType, TProperty> fieldBuilder,
Type type = null
)
{
fieldBuilder.FieldType.Metadata[SharedConstants.SourceType] = type ?? typeof(TSourceType);
return fieldBuilder;
}
public static Type GetSourceType(this FieldType field)
{
if (field.Metadata.TryGetValue(SharedConstants.SourceType, out var sourceTypeRaw) && sourceTypeRaw is Type sourceType)
{
return sourceType;
}
return null;
}
}
public static class GraphQLExtensions
{
/// <summary>
/// Guesses the first type which is not a graphql lib type.
/// WARNING! THIS METHOD IS NOT SAFE
/// Its just for the lazy boyz
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static Type EnsureNoGraphQlCoreType(this Type type)
{
var assembly = typeof(ListGraphType).Assembly;
if (type.Assembly == assembly)
{
return EnsureNoListGraphType(type.GetGenericArguments().First());
}
return type;
}
public static Type EnsureNoListGraphType(this Type type)
{
if (type.BaseType == typeof(ListGraphType))
{
return EnsureNoListGraphType(type.GetGenericArguments().First());
}
return type;
}
public static QueryArguments AddRange(this QueryArguments queryArguments, IEnumerable<QueryArgument> arguments)
{
foreach (var queryArgument in arguments)
{
queryArguments.Add(queryArgument);
}
return queryArguments;
}
public static FieldBuilder<TSourceType, TReturnType> SkipTakeOrderByArguments<TSourceType, TReturnType>
(
this FieldBuilder<TSourceType, TReturnType> source
)
{
var guessedGraphType = source.FieldType.Type.EnsureNoGraphQlCoreType();
var typedArg = typeof(TypeFieldEnumerationWithoutLists<>).MakeGenericType(guessedGraphType);
source.FieldType.Arguments.AddRange(new[] {
DefaultQueryArguments.Skip,
DefaultQueryArguments.Take,
DefaultQueryArguments.GetOrderBy(typedArg),
DefaultQueryArguments.GetOrderByDesc(typedArg),
});
return source;
}
}
}