-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
CustomParameterExpressionBuilder.cs
112 lines (99 loc) · 3.85 KB
/
CustomParameterExpressionBuilder.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Resolvers;
#nullable enable
namespace HotChocolate.Internal;
/// <summary>
/// A custom parameter expression builder allows to implement custom resolver parameter
/// injection logic.
/// </summary>
public abstract class CustomParameterExpressionBuilder : IParameterExpressionBuilder
{
ArgumentKind IParameterExpressionBuilder.Kind => ArgumentKind.Custom;
bool IParameterExpressionBuilder.IsPure => false;
bool IParameterExpressionBuilder.IsDefaultHandler => false;
/// <summary>
/// Checks if this expression builder can handle the following parameter.
/// </summary>
/// <param name="parameter">
/// The parameter that needs to be resolved.
/// </param>
/// <returns>
/// <c>true</c> if the parameter can be handled by this expression builder;
/// otherwise <c>false</c>.
/// </returns>
public abstract bool CanHandle(ParameterInfo parameter);
/// <summary>
/// Builds an expression that resolves a resolver parameter.
/// </summary>
/// <param name="context">
/// The parameter expression builder context.
/// </param>
/// <returns>
/// Returns an expression the handles the value injection into the parameter specified by
/// <see cref="ParameterExpressionBuilderContext.Parameter"/>.
/// </returns>
public abstract Expression Build(ParameterExpressionBuilderContext context);
}
/// <summary>
/// A custom parameter expression builder that allows to specify the expressions by
/// passing them into the constructor.
/// </summary>
public sealed class CustomParameterExpressionBuilder<TArg> : CustomParameterExpressionBuilder
{
private readonly Func<ParameterInfo, bool> _canHandle;
private readonly Expression<Func<IResolverContext, TArg>> _expression;
/// <summary>
/// Initializes a new instance of <see cref="CustomParameterExpressionBuilder"/>.
/// </summary>
/// <param name="expression">
/// The expression that shall be used to resolve the parameter value.
/// </param>
public CustomParameterExpressionBuilder(
Expression<Func<IResolverContext, TArg>> expression)
{
_canHandle = p => p.ParameterType == typeof(TArg);
_expression = expression;
}
/// <summary>
/// Initializes a new instance of <see cref="CustomParameterExpressionBuilder"/>.
/// </summary>
/// <param name="canHandle">
/// A func that defines if a parameter can be handled by this expression builder.
/// </param>
/// <param name="expression">
/// The expression that shall be used to resolve the parameter value.
/// </param>
public CustomParameterExpressionBuilder(
Expression<Func<IResolverContext, TArg>> expression,
Func<ParameterInfo, bool> canHandle)
{
_expression = expression;
_canHandle = canHandle;
}
/// <summary>
/// Checks if this expression builder can handle the following parameter.
/// </summary>
/// <param name="parameter">
/// The parameter that needs to be resolved.
/// </param>
/// <returns>
/// <c>true</c> if the parameter can be handled by this expression builder;
/// otherwise <c>false</c>.
/// </returns>
public override bool CanHandle(ParameterInfo parameter)
=> _canHandle(parameter);
/// <summary>
/// Builds an expression that resolves a resolver parameter.
/// </summary>
/// <param name="context">
/// The parameter expression builder context.
/// </param>
/// <returns>
/// Returns an expression the handles the value injection into the parameter specified by
/// <see cref="ParameterExpressionBuilderContext.Parameter"/>.
/// </returns>
public override Expression Build(ParameterExpressionBuilderContext context)
=> Expression.Invoke(_expression, context.ResolverContext);
}