Skip to content

Commit

Permalink
Fixed argument naming conventions for mutation conventions. (#6705)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Staib <michael@chillicream.com>
  • Loading branch information
PHILLIPS71 and michaelstaib authored Nov 17, 2023
1 parent a7788db commit f90fbaa
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
namespace HotChocolate.Types;

internal sealed class ErrorMiddleware
internal sealed class ErrorMiddleware(FieldDelegate next, IReadOnlyList<CreateError> errorHandlers)
{
private readonly FieldDelegate _next;
private readonly IReadOnlyList<CreateError> _errorHandlers;

public ErrorMiddleware(FieldDelegate next, IReadOnlyList<CreateError> errorHandlers)
{
_next = next ??
throw new ArgumentNullException(nameof(next));
_errorHandlers = errorHandlers ??
throw new ArgumentNullException(nameof(errorHandlers));
}
private readonly FieldDelegate _next = next ??
throw new ArgumentNullException(nameof(next));
private readonly IReadOnlyList<CreateError> _errorHandlers = errorHandlers ??
throw new ArgumentNullException(nameof(errorHandlers));

public async ValueTask InvokeAsync(IMiddlewareContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ namespace HotChocolate.Types;

internal sealed class ErrorNullMiddleware(FieldDelegate next)
{
private readonly FieldDelegate _next = next ?? throw new ArgumentNullException(nameof(next));
private readonly FieldDelegate _next = next ??
throw new ArgumentNullException(nameof(next));

public async ValueTask InvokeAsync(IMiddlewareContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Linq;

#nullable enable

namespace HotChocolate.Types;

internal sealed class ErrorObjectType<T> : ObjectType<T>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

namespace HotChocolate.Types;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using static HotChocolate.Properties.TypeResources;
using static HotChocolate.Types.ErrorContextDataKeys;

#nullable enable

namespace HotChocolate.Types;

/// <summary>
Expand Down
46 changes: 17 additions & 29 deletions src/HotChocolate/Core/src/Types.Mutations/MutationContextData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,31 @@ namespace HotChocolate.Types;
/// This internal data structure is used to store the effective mutation options of a field
/// on the context so that the type interceptor can access them.
/// </summary>
internal sealed class MutationContextData
internal sealed class MutationContextData(
ObjectFieldDefinition definition,
string? inputTypeName,
string? inputArgumentName,
string? payloadTypeName,
string? payloadFieldName,
string? payloadErrorTypeName,
string? payloadErrorsFieldName,
bool enabled)
{
public MutationContextData(
ObjectFieldDefinition definition,
string? inputTypeName,
string? inputArgumentName,
string? payloadTypeName,
string? payloadFieldName,
string? payloadErrorTypeName,
string? payloadErrorsFieldName,
bool enabled)
{
Definition = definition;
InputTypeName = inputTypeName;
InputArgumentName = inputArgumentName;
PayloadTypeName = payloadTypeName;
PayloadFieldName = payloadFieldName;
PayloadPayloadErrorTypeName = payloadErrorTypeName;
PayloadErrorsFieldName = payloadErrorsFieldName;
Enabled = enabled;
}

public string Name => Definition.Name;

public ObjectFieldDefinition Definition { get; }
public ObjectFieldDefinition Definition { get; } = definition;

public string? InputTypeName { get; }
public string? InputTypeName { get; } = inputTypeName;

public string? InputArgumentName { get; }
public string? InputArgumentName { get; } = inputArgumentName;

public string? PayloadFieldName { get; }
public string? PayloadFieldName { get; } = payloadFieldName;

public string? PayloadTypeName { get; }
public string? PayloadTypeName { get; } = payloadTypeName;

public string? PayloadPayloadErrorTypeName { get; }
public string? PayloadPayloadErrorTypeName { get; } = payloadErrorTypeName;

public string? PayloadErrorsFieldName { get; }
public string? PayloadErrorsFieldName { get; } = payloadErrorsFieldName;

public bool Enabled { get; }
public bool Enabled { get; } = enabled;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

namespace HotChocolate.Types;

internal static class MutationContextDataKeys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,17 @@ namespace HotChocolate.Types;
/// This middleware ensures that the rewritten argument structure is remapped so that the
/// resolver can request the arguments in the original structure.
/// </summary>
internal sealed class MutationConventionMiddleware
internal sealed class MutationConventionMiddleware(
FieldDelegate next,
string inputArgumentName,
IReadOnlyList<ResolverArgument> resolverArguments)
{
private readonly FieldDelegate _next;
private readonly string _inputArgumentName;
private readonly IReadOnlyList<ResolverArgument> _resolverArguments;

public MutationConventionMiddleware(
FieldDelegate next,
string inputArgumentName,
IReadOnlyList<ResolverArgument> resolverArguments)
{
_next = next ??
throw new ArgumentNullException(nameof(next));
_inputArgumentName = inputArgumentName ??
throw new ArgumentNullException(nameof(inputArgumentName));
_resolverArguments = resolverArguments ??
throw new ArgumentNullException(nameof(resolverArguments));
}
private readonly FieldDelegate _next = next ??
throw new ArgumentNullException(nameof(next));
private readonly string _inputArgumentName = inputArgumentName ??
throw new ArgumentNullException(nameof(inputArgumentName));
private readonly IReadOnlyList<ResolverArgument> _resolverArguments = resolverArguments ??
throw new ArgumentNullException(nameof(resolverArguments));

public async ValueTask InvokeAsync(IMiddlewareContext context)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using HotChocolate.Types.Helpers;
using static HotChocolate.WellKnownMiddleware;
using static HotChocolate.Types.Descriptors.TypeReference;
using static HotChocolate.Resolvers.FieldClassMiddlewareFactory;
Expand All @@ -8,8 +9,6 @@
using static HotChocolate.Utilities.ThrowHelper;
using static HotChocolate.WellKnownContextData;

#nullable enable

namespace HotChocolate.Types;

internal sealed class MutationConventionTypeInterceptor : TypeInterceptor
Expand Down Expand Up @@ -99,7 +98,7 @@ internal override void OnBeforeCompleteMutation(
// on the mutations.
if (_mutationTypeDef is not null)
{
HashSet<MutationContextData> unprocessed = new(_mutations);
HashSet<MutationContextData> unprocessed = [.._mutations];
var defLookup = _mutations.ToDictionary(t => t.Definition);
var nameLookup = _mutations.ToDictionary(t => t.Name);
var rootOptions = CreateOptions(_context.ContextData);
Expand Down Expand Up @@ -137,7 +136,7 @@ internal override void OnBeforeCompleteMutation(
// if the mutation options indicate that we shall apply the mutation
// conventions we will start rewriting the field.
ApplyResultMiddleware(mutationField);
TryApplyInputConvention(mutationField, mutationOptions);
TryApplyInputConvention(_context.ResolverCompiler, mutationField, mutationOptions);
TryApplyPayloadConvention(mutationField, cd?.PayloadFieldName, mutationOptions);
}
}
Expand Down Expand Up @@ -181,13 +180,38 @@ private static void ApplyResultMiddleware(ObjectFieldDefinition mutation)
mutation.MiddlewareDefinitions.Insert(0, middlewareDef);
}

private void TryApplyInputConvention(ObjectFieldDefinition mutation, Options options)
private void TryApplyInputConvention(
IResolverCompiler resolverCompiler,
ObjectFieldDefinition mutation,
Options options)
{
if (mutation.Arguments.Count is 0)
{
return;
}

if (mutation.Member is not null)
{
var argumentNameMap = TypeMemHelper.RentArgumentNameMap();

foreach (var arg in mutation.Arguments)
{
if (arg.Parameter is not null)
{
argumentNameMap.Add(arg.Parameter, arg.Name);
}
}

mutation.Resolvers =
resolverCompiler.CompileResolve(
mutation.Member,
mutation.SourceType,
mutation.ResolverType,
argumentNameMap);

TypeMemHelper.Return(argumentNameMap);
}

var inputTypeName = options.FormatInputTypeName(mutation.Name);

if (_typeRegistry.NameRefs.ContainsKey(inputTypeName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace HotChocolate.Types;

internal sealed class MutationResultTypeDiscoveryHandler : TypeDiscoveryHandler
internal sealed class MutationResultTypeDiscoveryHandler(ITypeInspector typeInspector) : TypeDiscoveryHandler
{
private readonly ITypeInspector _typeInspector;

public MutationResultTypeDiscoveryHandler(ITypeInspector typeInspector)
{
_typeInspector = typeInspector ?? throw new ArgumentNullException(nameof(typeInspector));
}
private readonly ITypeInspector _typeInspector = typeInspector ??
throw new ArgumentNullException(nameof(typeInspector));

public override bool TryInferType(
TypeReference typeReference,
Expand Down
39 changes: 14 additions & 25 deletions src/HotChocolate/Core/src/Types.Mutations/ResolverArgument.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
#nullable enable

namespace HotChocolate.Types;

internal sealed class ResolverArgument : IInputFieldInfo
internal sealed class ResolverArgument(
string name,
FieldCoordinate coordinate,
IInputType type,
Type runtimeType,
IValueNode? defaultValue,
IInputValueFormatter? formatter)
: IInputFieldInfo
{
public ResolverArgument(
string name,
FieldCoordinate coordinate,
IInputType type,
Type runtimeType,
IValueNode? defaultValue,
IInputValueFormatter? formatter)
{
Name = name;
Coordinate = coordinate;
RuntimeType = runtimeType;
Type = type;
DefaultValue = defaultValue;
Formatter = formatter;
}

public string Name { get; }
public string Name { get; } = name;

public FieldCoordinate Coordinate { get; }
public FieldCoordinate Coordinate { get; } = coordinate;

public IInputType Type { get; }
public IInputType Type { get; } = type;

public Type RuntimeType { get; }
public Type RuntimeType { get; } = runtimeType;

public IValueNode? DefaultValue { get; }
public IValueNode? DefaultValue { get; } = defaultValue;

public IInputValueFormatter? Formatter { get; }
public IInputValueFormatter? Formatter { get; } = formatter;
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Linq;

namespace HotChocolate.Types;

/// <summary>
Expand Down
Loading

0 comments on commit f90fbaa

Please sign in to comment.