-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueryNode.cs
More file actions
245 lines (200 loc) · 6.75 KB
/
QueryNode.cs
File metadata and controls
245 lines (200 loc) · 6.75 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Linq2GraphQL.Client;
using Linq2GraphQL.Client.Schema;
namespace Linq2GraphQL.Client;
public class QueryNode
{
private readonly bool mustHaveChildren;
private readonly Type underlyingMemberType;
private string argumentHashCodeId;
public QueryNode(MemberInfo member, string name = null, List<ArgumentValue> arguments = null, bool interfaceProperty = false, bool topLevel = false)
{
Name = name ?? member.GetCustomAttribute<GraphQLMemberAttribute>()?.GraphQLName ?? member.Name.ToCamelCase();
Member = member;
Arguments = arguments ?? new List<ArgumentValue>();
underlyingMemberType = member.GetUnderlyingType();
mustHaveChildren = MustHaveChildren(underlyingMemberType);
InterfaceProperty = interfaceProperty;
if (!topLevel) {
SetArgumentHashCodeId();
}
}
public bool InterfaceProperty { get; internal set; }
public string Name { get; internal set; }
public string Alias { get; internal set; }
public MemberInfo Member { get; internal set; }
public List<QueryNode> ChildNodes { get; internal set; } = new();
public List<ArgumentValue> Arguments { get; internal set; } = new();
public bool IncludePrimitive { get; internal set; }
public QueryNode Parent { get; private set; }
internal void IncludePrimitiveIfNoChildPrimitive()
{
if (!ChildNodes.Any(e => e.underlyingMemberType.IsValueTypeOrString()))
{
IncludePrimitive = true;
}
}
private void SetArgumentHashCodeId()
{
argumentHashCodeId = Utilities.GetArgumentsId(Arguments?.Select(e=> e.Value));
if (argumentHashCodeId != null)
{
Alias = Name + argumentHashCodeId;
}
}
private static bool MustHaveChildren(Type type)
{
return !type.IsValueTypeOrString() &&
!type.IsListOfPrimitiveTypeOrString();
}
public void AddChildNode(MemberInfo member, string name = null)
{
AddChildNode(new QueryNode(member, name));
}
public int Level => Parent?.Level + 1 ?? 1;
public int Leaf { get; internal set; } = 1;
public QueryNode AddChildNode(QueryNode childNode)
{
var currentNode = ChildNodes.FirstOrDefault(e => e.Name == childNode.Name && e.argumentHashCodeId == childNode.argumentHashCodeId);
if (currentNode == null)
{
childNode.Parent = this;
childNode.Leaf = ChildNodes.Count + 1;
ChildNodes.Add(childNode);
return childNode;
}
else if (childNode.IncludePrimitive)
{
currentNode.IncludePrimitive = true;
}
foreach (var child in childNode.ChildNodes)
{
currentNode.AddChildNode(child);
}
return currentNode;
}
public void SetArgumentValue(string graphName, object value)
{
var argument = Arguments.FirstOrDefault(e => e.GraphName == graphName);
if (argument != null)
{
argument.Value = value;
}
}
public void AddPrimitiveChildren(bool recursive, GraphQLSchema schema)
{
if (recursive)
{
foreach (var child in ChildNodes.Where(e => e.mustHaveChildren))
{
child.AddPrimitiveChildren(recursive, schema);
}
}
if (mustHaveChildren && (!ChildNodes.Any() || IncludePrimitive))
{
var typeOrListType = underlyingMemberType.GetTypeOrListType();
foreach (var propertyInfo in typeOrListType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (!propertyInfo.PropertyType.IsValueTypeOrString())
{
continue;
}
var memberAttribute = propertyInfo.GetCustomAttribute<GraphQLMemberAttribute>();
if (memberAttribute == null)
{
continue;
}
if (schema != null)
{
if (schema.TypePropertyExists(typeOrListType.Name, memberAttribute.GraphQLName))
{
AddChildNode(propertyInfo, memberAttribute.GraphQLName);
}
else
{
Console.WriteLine(
$"Property: {propertyInfo.Name} Type: {Member.Name} was not present in active schema");
}
}
else
{
AddChildNode(propertyInfo);
}
}
}
}
internal void SetAllUniqueVariableNames()
{
foreach (var argument in Arguments)
{
argument.VariableName += $"_{Level}_{Leaf}";
}
foreach (var child in ChildNodes)
{
child.SetAllUniqueVariableNames();
}
}
public List<ArgumentValue> GetActiveArguments()
{
return Arguments.Where(e => e.Value != null).ToList();
}
public List<ArgumentValue> GetAllActiveArguments()
{
var allArguments = GetActiveArguments();
foreach (var childNode in ChildNodes)
{
allArguments.AddRange(childNode.GetAllActiveArguments());
}
return allArguments;
}
private string GetArgumentString()
{
var args = GetActiveArguments(); ;
if (!args.Any())
{
return "";
}
var argString = "(";
foreach (var arg in args)
{
argString += arg.GraphName + ":$" + arg.VariableName + " ";
}
argString.TrimEnd();
argString += ")";
return argString;
}
internal string GetQueryString(string indent = "")
{
string query;
var memberType = Member.GetUnderlyingType();
if (InterfaceProperty)
{
query = "... on " + Name + GetArgumentString() + Environment.NewLine;
}
else if (!string.IsNullOrWhiteSpace(Alias))
{
query = Alias + ":" + Name + GetArgumentString() + Environment.NewLine;
}
else
{
query = Name + GetArgumentString() + Environment.NewLine;
}
if (memberType.IsListOfPrimitiveTypeOrString())
{
return indent + query;
}
query += indent + "{" + Environment.NewLine;
if (ChildNodes.Any())
{
var newIndent = " " + indent;
foreach (var childNode in ChildNodes)
{
query += childNode.GetQueryString(newIndent);
}
}
query += indent + "}" + Environment.NewLine;
return query;
}
}