Skip to content

Commit d1ccccb

Browse files
committed
Fixed bug with NewExpression
1 parent 91cc6b2 commit d1ccccb

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

src/Linq2GraphQL.Client/QueryNode.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public QueryNode(MemberInfo member, string name = null, List<ArgumentValue> argu
2424
if (!topLevel) {
2525
SetArgumentHashCodeId();
2626
}
27-
2827
}
2928

3029
public bool InterfaceProperty { get; internal set; }

src/Linq2GraphQL.Client/Utilities.cs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace Linq2GraphQL.Client;
66

77
public static class Utilities
88
{
9-
109
public static string GetArgumentsId(IEnumerable<object> objects)
1110
{
1211
if (objects == null) return null;
@@ -15,23 +14,26 @@ public static string GetArgumentsId(IEnumerable<object> objects)
1514

1615
unchecked
1716
{
18-
int hash = 19;
17+
var hash = 19;
1918
foreach (var obj in objs)
2019
{
2120
hash = hash * 31 + obj.GetHashCode();
2221
}
23-
var hashString = hash.ToString().Replace("-", "_");
2422

25-
return hashString;
23+
return hash.ToString().Replace("-", "_");
2624
}
2725
}
2826

29-
public static bool IsSelectOrSelectMany(this MethodCallExpression methodCallExpression)
27+
private static bool IsSelectOrSelectMany(this MethodCallExpression methodCallExpression)
3028
{
31-
if (methodCallExpression.Arguments.Count != 2) { return false; };
29+
if (methodCallExpression.Arguments.Count != 2)
30+
{
31+
return false;
32+
}
33+
34+
;
3235
var methodName = methodCallExpression.Method.Name;
3336
return (methodName == "Select" || methodName == "SelectMany");
34-
3537
}
3638

3739
public static void ParseExpression(Expression body, QueryNode parent)
@@ -44,20 +46,18 @@ public static void ParseExpression(Expression body, QueryNode parent)
4446
{
4547
parent.AddChildNode(childNode);
4648
}
47-
4849
}
4950

5051
private static void ParseExpressionInternal(Expression body, QueryNode parent)
5152
{
5253
if (body.NodeType == ExpressionType.MemberInit)
5354
{
5455
var exp = (MemberInitExpression)body;
55-
56-
foreach (MemberAssignment bining in exp.Bindings.Where(e => e.BindingType == MemberBindingType.Assignment).Cast<MemberAssignment>())
56+
foreach (var binding in exp.Bindings.Where(e => e.BindingType == MemberBindingType.Assignment)
57+
.Cast<MemberAssignment>())
5758
{
58-
ParseExpressionInternal(bining.Expression, parent);
59+
ParseExpressionInternal(binding.Expression, parent);
5960
}
60-
6161
}
6262

6363
switch (body)
@@ -72,36 +72,29 @@ private static void ParseExpressionInternal(Expression body, QueryNode parent)
7272
break;
7373

7474
case MethodCallExpression methodCallExp:
75-
7675
ParseMethodCallExpression(parent, methodCallExp);
77-
7876
break;
79-
case NewExpression newExpression:
80-
81-
var t = newExpression;
8277

78+
case NewExpression newExpression:
8379
foreach (var argument in newExpression.Arguments)
8480
{
85-
ParseExpressionInternal(argument, parent);
81+
ParseExpression(argument, parent);
8682
}
87-
break;
88-
89-
9083

84+
break;
9185
}
9286
}
9387

9488
private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpression methodCallExp)
9589
{
96-
var grapInterfaceAttribute = methodCallExp.Method.GetCustomAttribute<GraphInterfaceAttribute>();
97-
if (grapInterfaceAttribute != null)
90+
var graphInterfaceAttribute = methodCallExp.Method.GetCustomAttribute<GraphInterfaceAttribute>();
91+
if (graphInterfaceAttribute != null)
9892
{
9993
var queryNode = new QueryNode(methodCallExp.Method, methodCallExp.Method.Name, null, true);
10094
parent.AddChildNode(queryNode);
10195
return;
10296
}
10397

104-
10598
var graphMethodAttribute = methodCallExp.Method.GetCustomAttribute<GraphMethodAttribute>();
10699
if (graphMethodAttribute != null)
107100
{
@@ -110,8 +103,8 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
110103
var i = 0;
111104
foreach (var parameter in methodCallExp.Method.GetParameters())
112105
{
113-
var graphAtttribute = parameter.GetCustomAttribute<GraphArgumentAttribute>();
114-
if (graphAtttribute != null)
106+
var graphAttribute = parameter.GetCustomAttribute<GraphArgumentAttribute>();
107+
if (graphAttribute != null)
115108
{
116109
var arg = methodCallExp.Arguments[i];
117110
ConstantExpression argConstant;
@@ -125,7 +118,7 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
125118
argConstant = (ConstantExpression)arg;
126119
}
127120

128-
arguments.Add(new ArgumentValue(parameter.Name, graphAtttribute.GraphType,
121+
arguments.Add(new ArgumentValue(parameter.Name, graphAttribute.GraphType,
129122
argConstant.Value));
130123
}
131124

@@ -134,7 +127,6 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
134127

135128
var queryNode = new QueryNode(methodCallExp.Method, graphMethodAttribute.GraphName, arguments);
136129
parent.AddChildNode(queryNode);
137-
138130
}
139131
else if (methodCallExp.IsSelectOrSelectMany())
140132
{
@@ -151,7 +143,7 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
151143
}
152144
}
153145

154-
public static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expression expression)
146+
private static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expression expression)
155147
{
156148
var members = GetMembers(expression);
157149
if (members == null) return (null, null);
@@ -164,7 +156,6 @@ public static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expr
164156
foreach (var member in members)
165157
{
166158
var newNode = new QueryNode(member);
167-
168159
if (parentNode == null)
169160
{
170161
parentNode = newNode;
@@ -181,7 +172,7 @@ public static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expr
181172
}
182173

183174

184-
public static List<MemberInfo> GetMembers(Expression expression)
175+
private static List<MemberInfo> GetMembers(Expression expression)
185176
{
186177
var members = new List<MemberInfo>();
187178
if (expression.NodeType == ExpressionType.MemberAccess)

test/Linq2GraphQL.TestServer.Shared/Models/Customer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Customer
66
public string CustomerName { get; set; } = "";
77
public CustomerStatus Status { get; set; }
88
public List<Order> Orders { get; set; } = new();
9-
public Address Address { get; set; }
9+
public Address? Address { get; set; }
1010

1111
}
1212

test/Linq2GraphQL.Tests/MutationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task Mutation_Multiple()
3636
CustomerId = id,
3737
CustomerName = "New Customer",
3838
Orders = new List<OrderInput>(),
39-
Status = CustomerStatus.Active
39+
Status = CustomerStatus.Active,
4040
})
4141
.Select(e=> e.CustomerId)
4242
.ExecuteAsync();

0 commit comments

Comments
 (0)