-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
QueryableListOperationHandlerBase.cs
98 lines (84 loc) · 3.11 KB
/
QueryableListOperationHandlerBase.cs
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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using HotChocolate.Configuration;
using HotChocolate.Internal;
using HotChocolate.Language;
using HotChocolate.Language.Visitors;
namespace HotChocolate.Data.Filters.Expressions
{
public abstract class QueryableListOperationHandlerBase
: FilterFieldHandler<QueryableFilterContext, Expression>
{
protected abstract int Operation { get; }
public override bool CanHandle(
ITypeCompletionContext context,
IFilterInputTypeDefinition typeDefinition,
IFilterFieldDefinition fieldDefinition)
{
return context.Type is IListFilterInput &&
fieldDefinition is FilterOperationFieldDefinition operationField &&
operationField.Id == Operation;
}
public override bool TryHandleEnter(
QueryableFilterContext context,
IFilterField field,
ObjectFieldNode node,
[NotNullWhen(true)] out ISyntaxVisitorAction? action)
{
if (node.Value.IsNull())
{
context.ReportError(
ErrorHelper.CreateNonNullError(field, node.Value, context));
action = SyntaxVisitor.Skip;
return true;
}
if (context.RuntimeTypes.Count > 0 &&
context.RuntimeTypes.Peek().TypeArguments is { Count: > 0 } args)
{
Expression nestedProperty = context.GetInstance();
context.PushInstance(nestedProperty);
IExtendedType element = args[0];
context.RuntimeTypes.Push(element);
context.AddScope();
action = SyntaxVisitor.Continue;
return true;
}
action = null;
return false;
}
public override bool TryHandleLeave(
QueryableFilterContext context,
IFilterField field,
ObjectFieldNode node,
[NotNullWhen(true)] out ISyntaxVisitorAction? action)
{
IExtendedType runtimeType = context.RuntimeTypes.Pop();
if (context.TryCreateLambda(out LambdaExpression? lambda))
{
context.Scopes.Pop();
Expression instance = context.PopInstance();
Expression expression = HandleListOperation(
context,
field,
node,
runtimeType.Source,
lambda);
if (context.InMemory)
{
expression = FilterExpressionBuilder.NotNullAndAlso(
instance, expression);
}
context.GetLevel().Enqueue(expression);
}
action = SyntaxVisitor.Continue;
return true;
}
protected abstract Expression HandleListOperation(
QueryableFilterContext context,
IFilterField field,
ObjectFieldNode node,
Type closureType,
LambdaExpression lambda);
}
}