Skip to content

Commit

Permalink
Fixed List Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jan 22, 2024
1 parent 2c8ba0d commit 85498d6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private void TryApplyPayloadConvention(

var type = CreatePayloadType(
payloadTypeName,
new(payloadFieldName, EnsureNullable(mutation.Type!)),
new(payloadFieldName, EnsureNullable(NormalizeTypeRef(mutation.Type!))),
errorField);
RegisterType(type);

Expand Down Expand Up @@ -826,7 +826,7 @@ private TypeReference EnsureNonNull(TypeReference typeRef)
return Create(CreateTypeNode(new NonNullType(type)));
}

private ITypeNode CreateTypeNode(IType type)
private static ITypeNode CreateTypeNode(IType type)
=> type switch
{
NonNullType nnt => new NonNullTypeNode((INullableTypeNode) CreateTypeNode(nnt.Type)),
Expand All @@ -835,41 +835,42 @@ private ITypeNode CreateTypeNode(IType type)
_ => throw new NotSupportedException("Type is not supported."),
};

private readonly ref struct Options
private static TypeReference NormalizeTypeRef(TypeReference typeRef)
{
public Options(
string? inputTypeNamePattern,
string? inputArgumentName,
string? payloadTypeNamePattern,
string? payloadErrorTypeNamePattern,
string? payloadErrorsFieldName,
bool? apply)
if (typeRef is ExtendedTypeReference { Type.IsGeneric: true } extendedTypeRef &&
typeof(IMutationResult).IsAssignableFrom(extendedTypeRef.Type.Type))
{
InputTypeNamePattern = inputTypeNamePattern ??
MutationConventionOptionDefaults.InputTypeNamePattern;
InputArgumentName = inputArgumentName ??
MutationConventionOptionDefaults.InputArgumentName;
PayloadTypeNamePattern = payloadTypeNamePattern ??
MutationConventionOptionDefaults.PayloadTypeNamePattern;
PayloadErrorsFieldName = payloadErrorsFieldName ??
MutationConventionOptionDefaults.PayloadErrorsFieldName;
PayloadErrorTypeNamePattern = payloadErrorTypeNamePattern ??
MutationConventionOptionDefaults.ErrorTypeNamePattern;
Apply = apply ??
MutationConventionOptionDefaults.ApplyToAllMutations;
return extendedTypeRef.WithType(extendedTypeRef.Type.TypeArguments[0]);
}

public string InputTypeNamePattern { get; }
return typeRef;
}

private readonly ref struct Options(
string? inputTypeNamePattern,
string? inputArgumentName,
string? payloadTypeNamePattern,
string? payloadErrorTypeNamePattern,
string? payloadErrorsFieldName,
bool? apply)
{
public string InputTypeNamePattern { get; } = inputTypeNamePattern ??
MutationConventionOptionDefaults.InputTypeNamePattern;

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

public string PayloadTypeNamePattern { get; }
public string PayloadTypeNamePattern { get; } = payloadTypeNamePattern ??
MutationConventionOptionDefaults.PayloadTypeNamePattern;

public string PayloadErrorTypeNamePattern { get; }
public string PayloadErrorTypeNamePattern { get; } = payloadErrorTypeNamePattern ??
MutationConventionOptionDefaults.ErrorTypeNamePattern;

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

public bool Apply { get; }
public bool Apply { get; } = apply ??
MutationConventionOptionDefaults.ApplyToAllMutations;

public string FormatInputTypeName(string mutationName)
=> InputTypeNamePattern.Replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace HotChocolate.Types;

internal sealed class MutationResultTypeDiscoveryHandler(ITypeInspector typeInspector) : TypeDiscoveryHandler
{
private readonly ITypeInspector _typeInspector = typeInspector ??
throw new ArgumentNullException(nameof(typeInspector));

public override bool TryInferType(
TypeReference typeReference,
TypeDiscoveryInfo typeInfo,
Expand All @@ -18,15 +15,15 @@ public override bool TryInferType(
typeof(IMutationResult).IsAssignableFrom(runtimeType) &&
typeReference is ExtendedTypeReference typeRef)
{
var type = _typeInspector.GetType(runtimeType.GenericTypeArguments[0]);
var type = GetNamedType(typeInspector.GetType(runtimeType.GenericTypeArguments[0]));
schemaTypeRefs = new TypeReference[runtimeType.GenericTypeArguments.Length];
schemaTypeRefs[0] = typeRef.WithType(type);

for (var i = 1; i < runtimeType.GenericTypeArguments.Length; i++)
{
var errorType = runtimeType.GenericTypeArguments[i];

type = _typeInspector.GetType(
type = typeInspector.GetType(
typeof(Exception).IsAssignableFrom(errorType)
? typeof(ExceptionObjectType<>).MakeGenericType(errorType)
: typeof(ErrorObjectType<>).MakeGenericType(errorType));
Expand All @@ -40,4 +37,10 @@ public override bool TryInferType(
schemaTypeRefs = null;
return false;
}

private IExtendedType GetNamedType(IExtendedType extendedType)
{
var typeInfo = typeInspector.CreateTypeInfo(extendedType);
return typeInspector.GetType(typeInfo.NamedType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,6 @@ public async Task List_Return_Type()
.AddGraphQL()
.AddQueryType(d => d.Field("abc").Resolve("def"))
.AddMutationType<ListReturnMutation>()
.AddType<ListReturnMutation.ResultItem>()
.AddMutationConventions()
.BuildSchemaAsync();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
schema {
query: Query
mutation: ListReturnMutation
}

type AddItemPayload {
resultItem: [ResultItem!]
}

type ListReturnMutation {
addItem(input: AddItemInput!): AddItemPayload!
}

type Query {
abc: String
}

type ResultItem {
name: String!
}

input AddItemInput {
count: Int!
}

0 comments on commit 85498d6

Please sign in to comment.