Skip to content

Commit

Permalink
Merge pull request #31 from Linq2GraphQL/fix-multilevel-select
Browse files Browse the repository at this point in the history
Fix multilevel select
  • Loading branch information
magahl authored Feb 8, 2024
2 parents ee426c8 + d1ccccb commit c4d0fed
Show file tree
Hide file tree
Showing 35 changed files with 264 additions and 264 deletions.
1 change: 0 additions & 1 deletion src/Linq2GraphQL.Client/QueryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public QueryNode(MemberInfo member, string name = null, List<ArgumentValue> argu
if (!topLevel) {
SetArgumentHashCodeId();
}

}

public bool InterfaceProperty { get; internal set; }
Expand Down
53 changes: 22 additions & 31 deletions src/Linq2GraphQL.Client/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Linq2GraphQL.Client;

public static class Utilities
{

public static string GetArgumentsId(IEnumerable<object> objects)
{
if (objects == null) return null;
Expand All @@ -15,23 +14,26 @@ public static string GetArgumentsId(IEnumerable<object> objects)

unchecked
{
int hash = 19;
var hash = 19;
foreach (var obj in objs)
{
hash = hash * 31 + obj.GetHashCode();
}
var hashString = hash.ToString().Replace("-", "_");

return hashString;
return hash.ToString().Replace("-", "_");
}
}

public static bool IsSelectOrSelectMany(this MethodCallExpression methodCallExpression)
private static bool IsSelectOrSelectMany(this MethodCallExpression methodCallExpression)
{
if (methodCallExpression.Arguments.Count != 2) { return false; };
if (methodCallExpression.Arguments.Count != 2)
{
return false;
}

;
var methodName = methodCallExpression.Method.Name;
return (methodName == "Select" || methodName == "SelectMany");

}

public static void ParseExpression(Expression body, QueryNode parent)
Expand All @@ -44,20 +46,18 @@ public static void ParseExpression(Expression body, QueryNode parent)
{
parent.AddChildNode(childNode);
}

}

private static void ParseExpressionInternal(Expression body, QueryNode parent)
{
if (body.NodeType == ExpressionType.MemberInit)
{
var exp = (MemberInitExpression)body;

foreach (MemberAssignment bining in exp.Bindings.Where(e => e.BindingType == MemberBindingType.Assignment).Cast<MemberAssignment>())
foreach (var binding in exp.Bindings.Where(e => e.BindingType == MemberBindingType.Assignment)
.Cast<MemberAssignment>())
{
ParseExpressionInternal(bining.Expression, parent);
ParseExpressionInternal(binding.Expression, parent);
}

}

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

case MethodCallExpression methodCallExp:

ParseMethodCallExpression(parent, methodCallExp);

break;
case NewExpression newExpression:

var t = newExpression;

case NewExpression newExpression:
foreach (var argument in newExpression.Arguments)
{
ParseExpressionInternal(argument, parent);
ParseExpression(argument, parent);
}
break;



break;
}
}

private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpression methodCallExp)
{
var grapInterfaceAttribute = methodCallExp.Method.GetCustomAttribute<GraphInterfaceAttribute>();
if (grapInterfaceAttribute != null)
var graphInterfaceAttribute = methodCallExp.Method.GetCustomAttribute<GraphInterfaceAttribute>();
if (graphInterfaceAttribute != null)
{
var queryNode = new QueryNode(methodCallExp.Method, methodCallExp.Method.Name, null, true);
parent.AddChildNode(queryNode);
return;
}


var graphMethodAttribute = methodCallExp.Method.GetCustomAttribute<GraphMethodAttribute>();
if (graphMethodAttribute != null)
{
Expand All @@ -110,8 +103,8 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
var i = 0;
foreach (var parameter in methodCallExp.Method.GetParameters())
{
var graphAtttribute = parameter.GetCustomAttribute<GraphArgumentAttribute>();
if (graphAtttribute != null)
var graphAttribute = parameter.GetCustomAttribute<GraphArgumentAttribute>();
if (graphAttribute != null)
{
var arg = methodCallExp.Arguments[i];
ConstantExpression argConstant;
Expand All @@ -125,7 +118,7 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
argConstant = (ConstantExpression)arg;
}

arguments.Add(new ArgumentValue(parameter.Name, graphAtttribute.GraphType,
arguments.Add(new ArgumentValue(parameter.Name, graphAttribute.GraphType,
argConstant.Value));
}

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

var queryNode = new QueryNode(methodCallExp.Method, graphMethodAttribute.GraphName, arguments);
parent.AddChildNode(queryNode);

}
else if (methodCallExp.IsSelectOrSelectMany())
{
Expand All @@ -151,7 +143,7 @@ private static void ParseMethodCallExpression(QueryNode parent, MethodCallExpres
}
}

public static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expression expression)
private static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expression expression)
{
var members = GetMembers(expression);
if (members == null) return (null, null);
Expand All @@ -164,7 +156,6 @@ public static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expr
foreach (var member in members)
{
var newNode = new QueryNode(member);

if (parentNode == null)
{
parentNode = newNode;
Expand All @@ -181,7 +172,7 @@ public static (QueryNode ParentNode, QueryNode LastNode) GetMemberQueryNode(Expr
}


public static List<MemberInfo> GetMembers(Expression expression)
private static List<MemberInfo> GetMembers(Expression expression)
{
var members = new List<MemberInfo>();
if (expression.NodeType == ExpressionType.MemberAccess)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ public ListFilterInputTypeOfOrderFilterInput Orders
set => SetValue("orders", value);
}

[JsonPropertyName("address")]
public AddressFilterInput Address
{
get => GetValue<AddressFilterInput>("address");
set => SetValue("address", value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ public List<OrderInput> Orders
set => SetValue("orders", value);
}

[JsonPropertyName("address")]
public AddressInput Address
{
get => GetValue<AddressInput>("address");
set => SetValue("address", value);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public SortEnumType? Status
set => SetValue("status", value);
}

[JsonPropertyName("address")]
public AddressSortInput Address
{
get => GetValue<AddressSortInput>("address");
set => SetValue("address", value);
}

}
27 changes: 27 additions & 0 deletions test/Linq2GraphQL.TestClient/Generated/Inputs/InputFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ public static CustomerFilterInput Orders(this CustomerFilterInput input, Action<
return input;
}

public static CustomerFilterInput Address(this CustomerFilterInput input, Action<AddressFilterInput> mod)
{
var filter = new AddressFilterInput();
mod ??= _ => { };
mod(filter);
input.Address = filter;
return input;
}

}

public static class CustomerInputExtensions
Expand Down Expand Up @@ -296,6 +305,15 @@ public static CustomerInput Orders(this CustomerInput input, Action<List<OrderIn
return input;
}

public static CustomerInput Address(this CustomerInput input, Action<AddressInput> mod)
{
var filter = new AddressInput();
mod ??= _ => { };
mod(filter);
input.Address = filter;
return input;
}

}

public static class CustomerSortInputExtensions
Expand All @@ -321,6 +339,15 @@ public static CustomerSortInput Status(this CustomerSortInput input, SortEnumTyp
return input;
}

public static CustomerSortInput Address(this CustomerSortInput input, Action<AddressSortInput> mod)
{
var filter = new AddressSortInput();
mod ??= _ => { };
mod(filter);
input.Address = filter;
return input;
}

}

public static class CustomerStatusOperationFilterInputExtensions
Expand Down
7 changes: 0 additions & 7 deletions test/Linq2GraphQL.TestClient/Generated/Types/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ public partial class Address : GraphQLTypeBase
[JsonPropertyName("name")]
public string Name { get; set; }


[JsonPropertyName("street")]
public string Street { get; set; }


[JsonPropertyName("postalCode")]
public string PostalCode { get; set; }






}
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,13 @@ public partial class AnimalsConnection : GraphQLTypeBase, Linq2GraphQL.Client.Co
[JsonPropertyName("pageInfo")]
public Linq2GraphQL.Client.Common.PageInfo PageInfo { get; set; }


[JsonPropertyName("edges")]
public List<AnimalsEdge> Edges { get; set; }


[JsonPropertyName("nodes")]
public List<IAnimal> Nodes { get; set; }


[JsonPropertyName("totalCount")]
public int TotalCount { get; set; }






}
6 changes: 0 additions & 6 deletions test/Linq2GraphQL.TestClient/Generated/Types/AnimalsEdge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public partial class AnimalsEdge : GraphQLTypeBase
[JsonPropertyName("cursor")]
public string Cursor { get; set; }


[JsonPropertyName("node")]
public IAnimal Node { get; set; }






}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public partial class CollectionSegmentInfo : GraphQLTypeBase
[JsonPropertyName("hasNextPage")]
public bool HasNextPage { get; set; }


[JsonPropertyName("hasPreviousPage")]
public bool HasPreviousPage { get; set; }






}
9 changes: 2 additions & 7 deletions test/Linq2GraphQL.TestClient/Generated/Types/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ public partial class Customer : GraphQLTypeBase
[JsonPropertyName("customerId")]
public Guid CustomerId { get; set; }


[JsonPropertyName("customerName")]
public string CustomerName { get; set; }


[JsonPropertyName("status")]
public CustomerStatus Status { get; set; }


[JsonPropertyName("orders")]
public List<Order> Orders { get; set; }





[JsonPropertyName("address")]
public Address Address { get; set; }

}
6 changes: 0 additions & 6 deletions test/Linq2GraphQL.TestClient/Generated/Types/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ public partial class Item : GraphQLTypeBase
[JsonPropertyName("itemId")]
public string ItemId { get; set; }


[JsonPropertyName("itemName")]
public string ItemName { get; set; }






}
Loading

0 comments on commit c4d0fed

Please sign in to comment.