From 64f8039ad042b5a6b7166a15128b9584ead0f119 Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:36:25 +0300 Subject: [PATCH 1/7] Allow passing in a naming convention for generic arguments --- .../src/Abstractions/ITypeNamingConvention.cs | 8 ++++++++ .../src/Abstractions/NameFormattingHelpers.cs | 19 +++++++++++++++---- .../Conventions/DefaultNamingConventions.cs | 4 ++-- .../Conventions/INamingConventions.cs | 4 +--- .../QueryableSpatialBooleanMethodHandler.cs | 1 + 5 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs diff --git a/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs b/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs new file mode 100644 index 00000000000..9d571e8d8f4 --- /dev/null +++ b/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs @@ -0,0 +1,8 @@ +using System; + +namespace HotChocolate; + +public interface ITypeNamingConvention +{ + string GetTypeName(Type type); +} diff --git a/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs b/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs index 929dbfd7760..b53b408b637 100644 --- a/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs +++ b/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs @@ -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, + ITypeNamingConvention? namingConventionForGenericArguments = null) { if (type is null) { @@ -29,7 +31,7 @@ public static string GetGraphQLName(this Type type) var typeInfo = type.GetTypeInfo(); var name = typeInfo.IsDefined(typeof(GraphQLNameAttribute), false) ? typeInfo.GetCustomAttribute()!.Name - : GetFromType(typeInfo); + : GetFromType(typeInfo, namingConventionForGenericArguments); return NameUtils.MakeValidGraphQLName(name)!; } @@ -182,7 +184,9 @@ public static bool IsDeprecated( return false; } - private static string GetFromType(Type type) + private static string GetFromType( + Type type, + ITypeNamingConvention? namingConventionForGenericArguments) { if (type.GetTypeInfo().IsGenericType) { @@ -194,7 +198,14 @@ private static string GetFromType(Type type) var arguments = type .GetTypeInfo().GenericTypeArguments - .Select(GetFromType); + .Select(type1 => + { + if (namingConventionForGenericArguments is not null) + { + return namingConventionForGenericArguments.GetTypeName(type1); + } + return GetFromType(type1, namingConventionForGenericArguments: null); + }); return $"{name}Of{string.Join("And", arguments)}"; } diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs index 67624ab5a9f..23a478f7f7a 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs @@ -52,7 +52,7 @@ public virtual string GetTypeName(Type type) return Schema.DefaultName; } - return type.GetGraphQLName(); + return type.GetGraphQLName(namingConventionForGenericArguments: this); } /// @@ -63,7 +63,7 @@ public virtual string GetTypeName(Type type, TypeKind kind) throw new ArgumentNullException(nameof(type)); } - var name = type.GetGraphQLName(); + var name = type.GetGraphQLName(namingConventionForGenericArguments: this); if (_formatInterfaceName && kind == TypeKind.Interface && diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs index 5a3b34ed444..dec4f9ad11f 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs @@ -5,10 +5,8 @@ namespace HotChocolate.Types.Descriptors; -public interface INamingConventions : IConvention +public interface INamingConventions : IConvention, ITypeNamingConvention { - string GetTypeName(Type type); - string GetTypeName(Type type, TypeKind kind); string? GetTypeDescription(Type type, TypeKind kind); diff --git a/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs b/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs index a137e6eb0cf..d7bb2eae6e7 100644 --- a/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs +++ b/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs @@ -11,6 +11,7 @@ namespace HotChocolate.Data.Filters.Spatial; + public abstract class QueryableSpatialBooleanMethodHandler : FilterFieldHandler { From 3b2ee130559a908f89e0ab750e5902313ab16f58 Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:45:46 +0300 Subject: [PATCH 2/7] Make the implementation non-breaking (almost) --- .../Core/src/Abstractions/ITypeNamingConvention.cs | 4 ++-- .../Core/src/Abstractions/NameFormattingHelpers.cs | 12 ++++++------ .../Conventions/DefaultNamingConventions.cs | 10 ++++++++-- .../Descriptors/Conventions/INamingConventions.cs | 6 +++++- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs b/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs index 9d571e8d8f4..08bd782ba56 100644 --- a/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs +++ b/src/HotChocolate/Core/src/Abstractions/ITypeNamingConvention.cs @@ -2,7 +2,7 @@ namespace HotChocolate; -public interface ITypeNamingConvention +public interface IGenericTypeArgumentNamingConvention { - string GetTypeName(Type type); + string GetGenericTypeArgumentName(Type type); } diff --git a/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs b/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs index b53b408b637..87365b1902f 100644 --- a/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs +++ b/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs @@ -21,7 +21,7 @@ internal static class NameFormattingHelpers public static string GetGraphQLName( this Type type, - ITypeNamingConvention? namingConventionForGenericArguments = null) + IGenericTypeArgumentNamingConvention? namingConventionForGenericTypeArguments = null) { if (type is null) { @@ -31,7 +31,7 @@ public static string GetGraphQLName( var typeInfo = type.GetTypeInfo(); var name = typeInfo.IsDefined(typeof(GraphQLNameAttribute), false) ? typeInfo.GetCustomAttribute()!.Name - : GetFromType(typeInfo, namingConventionForGenericArguments); + : GetFromType(typeInfo, namingConventionForGenericTypeArguments); return NameUtils.MakeValidGraphQLName(name)!; } @@ -186,7 +186,7 @@ public static bool IsDeprecated( private static string GetFromType( Type type, - ITypeNamingConvention? namingConventionForGenericArguments) + IGenericTypeArgumentNamingConvention? namingConventionForGenericTypeArguments) { if (type.GetTypeInfo().IsGenericType) { @@ -200,11 +200,11 @@ private static string GetFromType( .GetTypeInfo().GenericTypeArguments .Select(type1 => { - if (namingConventionForGenericArguments is not null) + if (namingConventionForGenericTypeArguments is not null) { - return namingConventionForGenericArguments.GetTypeName(type1); + return namingConventionForGenericTypeArguments.GetGenericTypeArgumentName(type1); } - return GetFromType(type1, namingConventionForGenericArguments: null); + return GetFromType(type1, namingConventionForGenericTypeArguments: null); }); return $"{name}Of{string.Join("And", arguments)}"; diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs index 23a478f7f7a..813cc384f82 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs @@ -39,6 +39,12 @@ protected internal override void Initialize(IConventionContext context) _formatInterfaceName = context.DescriptorContext.Options.StripLeadingIFromInterface; } + /// + public virtual string GetGenericTypeArgumentName(Type type) + { + return type.Name; + } + /// public virtual string GetTypeName(Type type) { @@ -52,7 +58,7 @@ public virtual string GetTypeName(Type type) return Schema.DefaultName; } - return type.GetGraphQLName(namingConventionForGenericArguments: this); + return type.GetGraphQLName(namingConventionForGenericTypeArguments: this); } /// @@ -63,7 +69,7 @@ public virtual string GetTypeName(Type type, TypeKind kind) throw new ArgumentNullException(nameof(type)); } - var name = type.GetGraphQLName(namingConventionForGenericArguments: this); + var name = type.GetGraphQLName(namingConventionForGenericTypeArguments: this); if (_formatInterfaceName && kind == TypeKind.Interface && diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs index dec4f9ad11f..c5ea3db84c4 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs @@ -5,8 +5,12 @@ namespace HotChocolate.Types.Descriptors; -public interface INamingConventions : IConvention, ITypeNamingConvention +public interface INamingConventions + : IConvention + , IGenericTypeArgumentNamingConvention { + string GetTypeName(Type type); + string GetTypeName(Type type, TypeKind kind); string? GetTypeDescription(Type type, TypeKind kind); From ab2705c6c0634d8f3544a19585ea8c9fa1b831a8 Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:46:53 +0300 Subject: [PATCH 3/7] Ah, actually, I can be totally non-breaking --- .../Types/Descriptors/Conventions/DefaultNamingConventions.cs | 1 + .../Types/Types/Descriptors/Conventions/INamingConventions.cs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs index 813cc384f82..d77d8c6bbdb 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs @@ -11,6 +11,7 @@ namespace HotChocolate.Types.Descriptors; public class DefaultNamingConventions : Convention , INamingConventions + , IGenericTypeArgumentNamingConvention { private const string _inputPostfix = "Input"; private const string _inputTypePostfix = "InputType"; diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs index c5ea3db84c4..b12458cfd3e 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs @@ -7,7 +7,6 @@ namespace HotChocolate.Types.Descriptors; public interface INamingConventions : IConvention - , IGenericTypeArgumentNamingConvention { string GetTypeName(Type type); From b05db0142f4ae30a36d47e9cf9ffc21838dfebad Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:50:20 +0300 Subject: [PATCH 4/7] Roll back accidental whitespace change --- .../Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs b/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs index d7bb2eae6e7..a137e6eb0cf 100644 --- a/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs +++ b/src/HotChocolate/Spatial/src/Data/Filters/Expressions/Handlers/QueryableSpatialBooleanMethodHandler.cs @@ -11,7 +11,6 @@ namespace HotChocolate.Data.Filters.Spatial; - public abstract class QueryableSpatialBooleanMethodHandler : FilterFieldHandler { From c20b6a16175ce021b664372716721dd6a84a338f Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:55:59 +0300 Subject: [PATCH 5/7] Oops the default should recurse --- .../Types/Descriptors/Conventions/DefaultNamingConventions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs index d77d8c6bbdb..d8e2ee1cc21 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs @@ -43,7 +43,7 @@ protected internal override void Initialize(IConventionContext context) /// public virtual string GetGenericTypeArgumentName(Type type) { - return type.Name; + return type.GetGraphQLName(namingConventionForGenericTypeArguments: this); } /// From f2c57b3b8ccdc3f7901b472781fbe8fd1397b601 Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:59:02 +0300 Subject: [PATCH 6/7] The recursive call needs to call GetFromType to match the previous behavior. --- src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs | 2 +- .../Types/Descriptors/Conventions/DefaultNamingConventions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs b/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs index 87365b1902f..b0f1e0a18a9 100644 --- a/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs +++ b/src/HotChocolate/Core/src/Abstractions/NameFormattingHelpers.cs @@ -184,7 +184,7 @@ public static bool IsDeprecated( return false; } - private static string GetFromType( + public static string GetFromType( Type type, IGenericTypeArgumentNamingConvention? namingConventionForGenericTypeArguments) { diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs index d8e2ee1cc21..254c8d78704 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DefaultNamingConventions.cs @@ -43,7 +43,7 @@ protected internal override void Initialize(IConventionContext context) /// public virtual string GetGenericTypeArgumentName(Type type) { - return type.GetGraphQLName(namingConventionForGenericTypeArguments: this); + return NameFormattingHelpers.GetFromType(type, namingConventionForGenericTypeArguments: null); } /// From da6a01daefa231265276c721f8011b44b22e64a0 Mon Sep 17 00:00:00 2001 From: AntonC9018 Date: Wed, 20 Sep 2023 21:59:49 +0300 Subject: [PATCH 7/7] Restore formatting on INamingConventions --- .../Types/Types/Descriptors/Conventions/INamingConventions.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs index b12458cfd3e..5a3b34ed444 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/INamingConventions.cs @@ -5,8 +5,7 @@ namespace HotChocolate.Types.Descriptors; -public interface INamingConventions - : IConvention +public interface INamingConventions : IConvention { string GetTypeName(Type type);