Repository navigation
Expand file tree
/
Copy pathParameterReplacer.cs
More file actions
191 lines (177 loc) · 8.74 KB
/
Copy pathParameterReplacer.cs
File metadata and controls
191 lines (177 loc) · 8.74 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using EntityGraphQL.Extensions;
using EntityGraphQL.Schema;
namespace EntityGraphQL.Compiler.Util
{
/// <summary>
/// As people build schema fields they may be against different parameters, this visitor lets us change it to the one used in compiling the EQL.
/// I.e. in (p_0) => p_0.blah `p_0` needs to be the same object otherwise it expects 2 values passed in.
/// </summary>
public class ParameterReplacer : ExpressionVisitor
{
private Expression? newParam;
private Type? toReplaceType;
private ParameterExpression? toReplace;
private bool finished;
private bool replaceWholeExpression;
private string? newFieldNameForType;
/// <summary>
/// Rebuilds the expression by replacing toReplace with newParam. Optionally looks for newFieldName as it is rebuilding.
/// Used when rebuilding expressions to happen from the sans-services results
/// </summary>
/// <param name="node"></param>
/// <param name="toReplace"></param>
/// <param name="newParam"></param>
/// <param name="newFieldName"></param>
/// <returns></returns>
public Expression Replace(Expression node, ParameterExpression toReplace, Expression newParam, bool replaceWholeExpression = false)
{
this.newParam = newParam;
this.toReplace = toReplace;
this.toReplaceType = null;
this.newFieldNameForType = null;
finished = false;
this.replaceWholeExpression = replaceWholeExpression;
return Visit(node);
}
public Expression ReplaceByType(Expression node, Type toReplaceType, Expression newParam, string? newContextFieldName = null)
{
this.newParam = newParam;
this.toReplaceType = toReplaceType;
this.toReplace = null;
finished = false;
this.newFieldNameForType = newContextFieldName;
replaceWholeExpression = false;
return Visit(node);
}
protected override Expression VisitParameter(ParameterExpression node)
{
if (toReplace != null && toReplace == node)
//leave param type so we can do conversions later,
//but if namespace is null then do conversion as it's not an entity type it's a generated type
//(need better way to detect this)
if(newParam!.Type.Namespace == null || newParam!.Type == node.Type)
return newParam!;
if (toReplaceType != null && node.NodeType == ExpressionType.Parameter && toReplaceType == node.Type)
return newParam!;
return node;
}
protected override Expression VisitLambda<T>(Expression<T> node)
{
var p = node.Parameters.Select(base.Visit).Cast<ParameterExpression>();
var body = base.Visit(node.Body);
return Expression.Lambda(body, p);
}
protected override Expression VisitUnary(UnaryExpression node)
{
// RequiredField<> causes a convert
if (node.Operand != null)
{
var newNode = base.Visit(node.Operand);
if (node.NodeType == ExpressionType.Convert && node.Type == newNode.Type)
return newNode;
return Expression.MakeUnary(node.NodeType, newNode, node.Type);
}
return base.VisitUnary(node);
}
protected override Expression VisitMember(MemberExpression node)
{
// returned expression may have been modified and we need to rebuild
if (node.Expression != null)
{
if (replaceWholeExpression)
{
// RequiredField<> causes a ctx.Field.Value
if (node.Expression == toReplace || (node.Expression.NodeType == ExpressionType.MemberAccess && ((MemberExpression)node.Expression).Expression == toReplace))
{
// var dotnetType = node.Expression.Type;
// if (dotnetType != newParam!.Type)
// return Expression.Convert(newParam, node.Expression.Type.GetGenericArguments().FirstOrDefault() ?? dotnetType);
return newParam!;
}
}
Expression nodeExp;
var fieldName = node.Member.Name;
if (node.Expression.Type == toReplaceType)
{
nodeExp = newParam!;
if (!string.IsNullOrEmpty(newFieldNameForType))
fieldName = newFieldNameForType;
}
else
nodeExp = base.Visit(node.Expression);
if (finished)
return nodeExp;
var field = nodeExp.Type.GetField(fieldName);
if (field != null)
nodeExp = Expression.Field(nodeExp, field);
else
{
var prop = nodeExp.Type.GetProperty(fieldName);
if (prop != null)
nodeExp = Expression.Property(nodeExp, prop);
else
nodeExp = Expression.PropertyOrField(nodeExp, node.Member.Name);
}
return nodeExp;
}
return base.VisitMember(node);
}
protected override Expression VisitConstant(ConstantExpression node)
{
// we do not want to replace constant ParameterExpressions in a nullwrap
return node;
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
bool baseCallIsEnumerable = node.Object == null && node.Arguments[0].Type.IsEnumerableOrArray();
if (node.Object == null && node.Arguments.Count > 1 && node.Method.IsGenericMethod)
{
// Replace expression that are inside method calls that might need parameters updated (.Where() etc.)
var callBase = base.Visit(node.Arguments[0]);
var callBaseType = callBase.Type.IsEnumerableOrArray() ? callBase.Type.GetEnumerableOrArrayType()! : callBase.Type;
var oldCallBaseType = baseCallIsEnumerable ? node.Arguments[0].Type.GetEnumerableOrArrayType()! : node.Arguments[0].Type;
if (callBaseType != oldCallBaseType)
{
var replaceAgain = new ParameterReplacer();
var newTypeArgs = new List<Type> { callBaseType };
var newArgs = new List<Expression>();
var oldTypeArgs = node.Method.GetGenericArguments();
foreach (var oldArg in node.Arguments.Skip(1))
{
var newArg = replaceAgain.ReplaceByType(oldArg, oldCallBaseType, Expression.Parameter(callBaseType));
newArgs.Add(newArg);
if (oldTypeArgs.Contains(oldArg.Type))
newTypeArgs.Add(newArg.Type);
else if (newArg.NodeType == ExpressionType.Lambda && oldTypeArgs.Contains(((LambdaExpression)newArg).ReturnType))
newTypeArgs.Add(((LambdaExpression)newArg).ReturnType);
}
if (oldTypeArgs.Length != newTypeArgs.Count)
throw new EntityGraphQLCompilerException($"Post service object selection contains a method call with mismatched generic type arguments.");
var newCall = Expression.Call(node.Method.DeclaringType, node.Method.Name, newTypeArgs.ToArray(), (new[] { callBase }).Concat(newArgs).ToArray());
return newCall;
}
}
else if (baseCallIsEnumerable && !node.Type.IsEnumerableOrArray() && node.Arguments.Count == 1 && !string.IsNullOrEmpty(newFieldNameForType))
{
// field is going from collection to a single - if execution is split over non service fields and then with
// the next context doesn't have the collection to single. It only has the single
var newField = base.Visit(node.Arguments[0]);
if (newField != null)
return newField;
}
var callOn = node.Object;
if (callOn != null)
{
if (callOn.Type == toReplaceType)
callOn = newParam!;
else
callOn = base.Visit(callOn);
}
return Expression.Call(callOn, node.Method, node.Arguments.Select(base.Visit).ToArray());
}
}
}