-
-
Notifications
You must be signed in to change notification settings - Fork 749
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed StateAttribute support in resolver compilers. (#887)
- Loading branch information
1 parent
c7e0cc8
commit 3b95fd0
Showing
5 changed files
with
289 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/Core/Types/Resolvers/Expressions/Arguments/GetCustomContextCompiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace HotChocolate.Resolvers.Expressions.Parameters | ||
{ | ||
internal sealed class GetCustomContextCompiler<T> | ||
: ResolverParameterCompilerBase<T> | ||
where T : IResolverContext | ||
{ | ||
private static readonly MethodInfo _resolveContextData = | ||
typeof(ExpressionHelper).GetMethod("ResolveContextData"); | ||
private static readonly MethodInfo _resolveScopedContextData = | ||
typeof(ExpressionHelper).GetMethod("ResolveScopedContextData"); | ||
|
||
private readonly PropertyInfo _contextData; | ||
private readonly PropertyInfo _scopedContextData; | ||
|
||
public GetCustomContextCompiler() | ||
{ | ||
_contextData = typeof(IHasContextData) | ||
.GetTypeInfo().GetDeclaredProperty( | ||
nameof(IResolverContext.ContextData)); | ||
_scopedContextData = ContextTypeInfo.GetDeclaredProperty( | ||
nameof(IResolverContext.ScopedContextData)); | ||
} | ||
|
||
public override bool CanHandle( | ||
ParameterInfo parameter, | ||
Type sourceType) => | ||
parameter.IsDefined(typeof(StateAttribute)); | ||
|
||
public override Expression Compile( | ||
Expression context, | ||
ParameterInfo parameter, | ||
Type sourceType) | ||
{ | ||
StateAttribute attribute = | ||
parameter.GetCustomAttribute<StateAttribute>(); | ||
|
||
ConstantExpression key = | ||
Expression.Constant(attribute.Key); | ||
|
||
ConstantExpression defaultIfNotExists = | ||
Expression.Constant(attribute.DefaultIfNotExists); | ||
|
||
MemberExpression contextData = attribute.IsScoped | ||
? Expression.Property(context, _scopedContextData) | ||
: Expression.Property(context, _contextData); | ||
|
||
MethodInfo resolveContextData = attribute.IsScoped | ||
? _resolveScopedContextData.MakeGenericMethod( | ||
parameter.ParameterType) | ||
: _resolveContextData.MakeGenericMethod( | ||
parameter.ParameterType); | ||
|
||
return Expression.Call( | ||
resolveContextData, | ||
contextData, | ||
key, | ||
defaultIfNotExists); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters