Skip to content

Commit

Permalink
Inputs will infer only arguments. (ChilliCream#4511)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Dec 1, 2021
1 parent d43c700 commit b557ebf
Show file tree
Hide file tree
Showing 34 changed files with 178 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class MutationRequestExecutorBuilderExtensions
/// <exception cref="ArgumentNullException">
/// The <paramref name="builder"/> is null.
/// </exception>
public static IRequestExecutorBuilder EnableMutationConvention(
public static IRequestExecutorBuilder EnableMutationConventions(
this IRequestExecutorBuilder builder)
{
if (builder is null)
Expand All @@ -29,7 +29,8 @@ public static IRequestExecutorBuilder EnableMutationConvention(
.TryAddTypeInterceptor<InputArgumentTypeInterceptor>()
.TryAddTypeInterceptor<PayloadTypeInterceptor>()
.Services
.AddSingleton<IParameterExpressionBuilder, InputParameterExpressionBuilder>();
.AddSingleton<IParameterExpressionBuilder, InputParameterExpressionBuilder>()
.AddSingleton<IParameterExpressionBuilder, InputArgumentParameterExpressionBuilder>();

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HotChocolate.Types;

internal class InputArgumentParameterExpressionBuilder : InputParameterExpressionBuilder
{
public override bool IsDefaultHandler => true;

public override bool CanHandle(ParameterInfo parameter)
=> parameter.Member.IsDefined(typeof(InputAttribute));
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;

#nullable enable

Expand All @@ -26,8 +22,7 @@ public override void OnBeforeRegisterDependencies(

string? fieldInputName = null;
string? fieldInputTypeName = null;
if (
field.ContextData.TryGetValue(InputContextData.Input, out object? contextObj) &&
if (field.ContextData.TryGetValue(InputContextData.Input, out var contextObj) &&
contextObj is InputContextData context)
{
fieldInputName = context.ArgumentName;
Expand Down Expand Up @@ -57,7 +52,7 @@ public override void OnBeforeRegisterDependencies(
if (argumentInputName is not null)
{
arguments ??= new Dictionary<string, ArgumentReference>();
if (!arguments.TryGetValue(argumentInputName, out var reference))
if (!arguments.TryGetValue(argumentInputName, out ArgumentReference? reference))
{
reference = new ArgumentReference(
fieldInputTypeName,
Expand Down Expand Up @@ -90,7 +85,7 @@ public override void OnBeforeRegisterDependencies(
continue;
}

foreach (var argument in arguments)
foreach (KeyValuePair<string, ArgumentReference> argument in arguments)
{
if (field.Type is null)
{
Expand All @@ -111,7 +106,8 @@ TypeSystemObjectBase CreateType(IDescriptorContext _) =>
new InputObjectType(x =>
{
x.Name(typeName);
foreach (var argumentDefinition in argument.Value.ArgumentDefinitions)
foreach (ArgumentDefinition argumentDefinition in
argument.Value.ArgumentDefinitions)
{
MergeFieldWithArgument(
x.Field(argumentDefinition.Name),
Expand All @@ -133,7 +129,7 @@ private static void MergeFieldWithArgument(
definition.DefaultValue = argumentDefinition.DefaultValue;
definition.Ignore = argumentDefinition.Ignore;
definition.RuntimeDefaultValue = argumentDefinition.RuntimeDefaultValue;
definition.RuntimeType = argumentDefinition.Parameter.ParameterType;
definition.RuntimeType = argumentDefinition.Parameter?.ParameterType;
definition.ContextData.AddRange(argumentDefinition.ContextData);
definition.Formatters.AddRange(argumentDefinition.Formatters);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Internal;
using HotChocolate.Resolvers;
using HotChocolate.Utilities;
using HotChocolate.Types.Properties;
using static System.Linq.Expressions.Expression;

#nullable enable
Expand All @@ -18,7 +13,8 @@ internal class InputParameterExpressionBuilder : IParameterExpressionBuilder
x => x.ArgumentValue<Dictionary<string, object>>(default));

private static readonly MethodInfo _containsKey =
ReflectionUtils.ExtractMethod<Dictionary<string, object>>(x => x.ContainsKey(default!));
ReflectionUtils.ExtractMethod<Dictionary<string, object>>(
x => x.ContainsKey(default!));

private static readonly PropertyInfo _getValue =
typeof(Dictionary<string, object>).GetProperty("Item")!;
Expand All @@ -29,12 +25,10 @@ internal class InputParameterExpressionBuilder : IParameterExpressionBuilder

public bool IsPure => true;

public bool CanHandle(ParameterInfo parameter)
{
InputAttribute? attribute = parameter.GetCustomAttribute<InputAttribute>() ??
parameter.Member.GetCustomAttribute<InputAttribute>();
return attribute is not null;
}
public virtual bool IsDefaultHandler => false;

public virtual bool CanHandle(ParameterInfo parameter)
=> parameter.IsDefined(typeof(InputAttribute));

public Expression Build(ParameterInfo parameter, Expression context)
{
Expand All @@ -43,7 +37,9 @@ public Expression Build(ParameterInfo parameter, Expression context)

if (attribute is null)
{
throw new ArgumentException("Could not find the InputAttribute", nameof(parameter));
throw new ArgumentException(
MutationResources.InputParameterExpressionBuilder_Build_NoAttribute,
nameof(parameter));
}

ParameterExpression variable =
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@
<value>Could not find a unique type name for input type of the argument {0} on field {1}. The type name {2} collides with {3}.
Check the attribute `[Input(TypeName= "{2}")]` or the method `.Input(typeName: "{2}")` on type {4} for naming collisions.</value>
</data>
<data name="InputParameterExpressionBuilder_Build_NoAttribute" xml:space="preserve">
<value>Could not find the InputAttribute</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public abstract class CustomParameterExpressionBuilder : IParameterExpressionBui

bool IParameterExpressionBuilder.IsPure => false;

bool IParameterExpressionBuilder.IsDefaultHandler => false;

/// <summary>
/// Checks if this expression builder can handle the following parameter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public CustomServiceParameterExpressionBuilder(ServiceKind kind = ServiceKind.De
ArgumentKind IParameterExpressionBuilder.Kind
=> ArgumentKind.Service;

bool IParameterExpressionBuilder.IsDefaultHandler => false;

bool IParameterExpressionBuilder.IsPure
=> _kind is ServiceKind.Default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public interface IParameterExpressionBuilder : IParameterHandler
/// </summary>
bool IsPure { get; }

/// <summary>
/// Specifies that this handler is run after all non-default handlers.
/// </summary>
bool IsDefaultHandler { get; }

/// <summary>
/// Builds an expression that resolves a resolver parameter.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ internal sealed class DefaultResolverCompiler : IResolverCompiler
public DefaultResolverCompiler(
IEnumerable<IParameterExpressionBuilder>? customParameterExpressionBuilders)
{
var custom = customParameterExpressionBuilders is not null
? new List<IParameterExpressionBuilder>(customParameterExpressionBuilders)
: new List<IParameterExpressionBuilder>();

// explicit internal expression builders will be added first.
var parameterExpressionBuilders = new List<IParameterExpressionBuilder>
{
Expand All @@ -59,9 +63,12 @@ public DefaultResolverCompiler(
{
// then we will add custom parameter expression builder and
// give the user a chance to override our implicit expression builder.
foreach (IParameterExpressionBuilder builder in customParameterExpressionBuilders)
foreach (IParameterExpressionBuilder builder in custom)
{
parameterExpressionBuilders.Add(builder);
if (!builder.IsDefaultHandler)
{
parameterExpressionBuilders.Add(builder);
}
}
}

Expand All @@ -79,6 +86,19 @@ public DefaultResolverCompiler(
parameterExpressionBuilders.Add(new ClaimsPrincipalParameterExpressionBuilder());
parameterExpressionBuilders.Add(new PathParameterExpressionBuilder());

if (customParameterExpressionBuilders is not null)
{
// last we will add all custom default handlers. This will give these handlers a chance
// to apply logic only on arguments.
foreach (IParameterExpressionBuilder builder in custom)
{
if (builder.IsDefaultHandler)
{
parameterExpressionBuilders.Add(builder);
}
}
}

var parameterFieldConfigurations = new List<IParameterFieldConfiguration>();

foreach (IParameterExpressionBuilder builder in parameterExpressionBuilders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static bool IsArgumentOptionalMethod(MethodInfo method)

public bool IsPure => true;

public bool IsDefaultHandler => true;

public virtual bool CanHandle(ParameterInfo parameter)
=> parameter.IsDefined(typeof(ArgumentAttribute));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static CancellationTokenParameterExpressionBuilder()

public bool IsPure => false;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> typeof(CancellationToken) == parameter.ParameterType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ internal sealed class ClaimsPrincipalParameterExpressionBuilder : IParameterExpr

public bool IsPure => true;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> parameter.ParameterType == typeof(ClaimsPrincipal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static DocumentParameterExpressionBuilder()

public bool IsPure => false;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> typeof(DocumentNode) == parameter.ParameterType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal sealed class GlobalStateParameterExpressionBuilder : IParameterExpressi

public bool IsPure => true;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> parameter.IsDefined(typeof(GlobalStateAttribute));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ protected LambdaParameterExpressionBuilder(Expression<Func<TContext, TValue>> ex

public bool IsPure { get; }

public bool IsDefaultHandler => false;

public abstract bool CanHandle(ParameterInfo parameter);

public virtual Expression Build(ParameterInfo parameter, Expression context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal class LegacyScopedServiceParameterExpressionBuilder : IParameterExpress

public bool IsPure => false;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> parameter.IsDefined(typeof(ScopedServiceAttribute));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static bool IsParentMethod(MethodInfo method)

public bool IsPure => true;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> parameter.IsDefined(typeof(ParentAttribute));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ internal sealed class PureResolverContextParameterExpressionBuilder

public bool IsPure => true;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> typeof(IPureResolverContext) == parameter.ParameterType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal sealed class ResolverContextParameterExpressionBuilder : IParameterExpr

public bool IsPure => false;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> typeof(IResolverContext) == parameter.ParameterType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ static SchemaParameterExpressionBuilder()

public bool IsPure => true;

public bool IsDefaultHandler => false;

public bool CanHandle(ParameterInfo parameter)
=> typeof(ISchema) == parameter.ParameterType ||
typeof(Schema) == parameter.ParameterType;
Expand Down
Loading

0 comments on commit b557ebf

Please sign in to comment.