Skip to content

Commit

Permalink
Projection left joins now support nullable (#2545)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn committed Nov 9, 2020
1 parent 8c2b695 commit e77073a
Show file tree
Hide file tree
Showing 35 changed files with 299 additions and 250 deletions.
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Execution.Processing;
using HotChocolate.Types;
using static HotChocolate.Data.Projections.Expressions.ProjectionExpressionBuilder;

namespace HotChocolate.Data.Projections.Expressions.Handlers
{
Expand Down Expand Up @@ -81,16 +83,30 @@ public class QueryableProjectionFieldHandler

Queue<MemberAssignment> members = queryableScope.Level.Pop();
MemberInitExpression memberInit =
ProjectionExpressionBuilder.CreateMemberInit(queryableScope.RuntimeType, members);
CreateMemberInit(queryableScope.RuntimeType, members);

if (!context.TryGetQueryableScope(out QueryableProjectionScope? parentScope))
{
throw new InvalidOperationException();
}

Expression nestedProperty;
if (field.Member is PropertyInfo propertyInfo)
{
nestedProperty = Expression.Property(context.GetInstance(), propertyInfo);
}
else if (field.Member is MethodInfo methodInfo)
{
nestedProperty = Expression.Call(context.GetInstance(), methodInfo);
}
else
{
throw new InvalidOperationException();
}

parentScope.Level.Peek()
.Enqueue(
Expression.Bind(field.Member, memberInit));
Expression.Bind(field.Member, NotNullAndAlso(nestedProperty, memberInit)));

action = SelectionVisitor.Continue;
return true;
Expand Down
Expand Up @@ -6,12 +6,28 @@ namespace HotChocolate.Data.Projections.Expressions
{
internal static class ProjectionExpressionBuilder
{
private static readonly ConstantExpression _null =
Expression.Constant(null, typeof(object));

public static MemberInitExpression CreateMemberInit(
Type type,
IEnumerable<MemberBinding> expressions)
{
NewExpression ctor = Expression.New(type);
return Expression.MemberInit(ctor, expressions);
}

public static Expression NotNull(Expression expression)
{
return Expression.NotEqual(expression, _null);
}

public static Expression NotNullAndAlso(Expression property, Expression condition)
{
return Expression.Condition(
NotNull(property),
condition,
Expression.Default(property.Type));
}
}
}

0 comments on commit e77073a

Please sign in to comment.