Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing in a naming convention for generic arguments #6547

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace HotChocolate;

public interface IGenericTypeArgumentNamingConvention
{
string GetGenericTypeArgumentName(Type type);
}
19 changes: 15 additions & 4 deletions src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ internal static class NameFormattingHelpers
private const string _async = "Async";
private const string _typePostfix = "`1";

public static string GetGraphQLName(this Type type)
public static string GetGraphQLName(
this Type type,
IGenericTypeArgumentNamingConvention? namingConventionForGenericTypeArguments = null)
{
if (type is null)
{
Expand All @@ -29,7 +31,7 @@ public static string GetGraphQLName(this Type type)
var typeInfo = type.GetTypeInfo();
var name = typeInfo.IsDefined(typeof(GraphQLNameAttribute), false)
? typeInfo.GetCustomAttribute<GraphQLNameAttribute>()!.Name
: GetFromType(typeInfo);
: GetFromType(typeInfo, namingConventionForGenericTypeArguments);

return NameUtils.MakeValidGraphQLName(name)!;
}
Expand Down Expand Up @@ -182,7 +184,9 @@ private static bool IsAsyncMethod(Type returnType)
return false;
}

private static string GetFromType(Type type)
public static string GetFromType(
Type type,
IGenericTypeArgumentNamingConvention? namingConventionForGenericTypeArguments)
{
if (type.GetTypeInfo().IsGenericType)
{
Expand All @@ -194,7 +198,14 @@ private static string GetFromType(Type type)

var arguments = type
.GetTypeInfo().GenericTypeArguments
.Select(GetFromType);
.Select(type1 =>
{
if (namingConventionForGenericTypeArguments is not null)
{
return namingConventionForGenericTypeArguments.GetGenericTypeArgumentName(type1);
}
return GetFromType(type1, namingConventionForGenericTypeArguments: null);
});

return $"{name}Of{string.Join("And", arguments)}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace HotChocolate.Types.Descriptors;
public class DefaultNamingConventions
: Convention
, INamingConventions
, IGenericTypeArgumentNamingConvention
{
private const string _inputPostfix = "Input";
private const string _inputTypePostfix = "InputType";
Expand Down Expand Up @@ -39,6 +40,12 @@ protected internal override void Initialize(IConventionContext context)
_formatInterfaceName = context.DescriptorContext.Options.StripLeadingIFromInterface;
}

/// <inheritdoc />
public virtual string GetGenericTypeArgumentName(Type type)
{
return NameFormattingHelpers.GetFromType(type, namingConventionForGenericTypeArguments: null);
}

/// <inheritdoc />
public virtual string GetTypeName(Type type)
{
Expand All @@ -52,7 +59,7 @@ public virtual string GetTypeName(Type type)
return Schema.DefaultName;
}

return type.GetGraphQLName();
return type.GetGraphQLName(namingConventionForGenericTypeArguments: this);
}

/// <inheritdoc />
Expand All @@ -63,7 +70,7 @@ public virtual string GetTypeName(Type type, TypeKind kind)
throw new ArgumentNullException(nameof(type));
}

var name = type.GetGraphQLName();
var name = type.GetGraphQLName(namingConventionForGenericTypeArguments: this);

if (_formatInterfaceName &&
kind == TypeKind.Interface &&
Expand Down
Loading