Open PandaBlackAndWhitr opened 2 years ago
The issue lies somehow in the naming convention. You could override the default naming convention if you want to do a quick fix in your project, or override the ToString method of the enumeration.
I will create a test and we can fix that with the next release.
I'm having this same issue with both scalar types (int) and classes like in the OP.
Ive just run into the same/similar issue
a smart enum class like this (note: ToString() will return a non graphql safe name)
public sealed class CultureCode
{
private readonly string _culture;
public static CultureCode EN_GB { get; } = new("en-GB";);
public static CultureCode EN_NZ { get; } = new("en-NZ";);
private CultureCode(string culture)
{
_culture = culture;
}
public override string ToString()
=> _culture;
}
and an enum descriptor like this
protected override void Configure(IEnumTypeDescriptor<CultureCode> descriptor)
{
descriptor
.Value(CultureCode.EN_GB) // will use ToString() to get the name and throw exception as "-" is invalid
.Name(CultureCode.EN_GB.ToString().Replace("-", "_")) // wont run as the exception is thrown earlier in the fluent api
.Description(CultureCode.EN_GB.ToString());
}
My fix is to override the DefaultNamingConventions
like this
public override string GetEnumValueName(object value)
{
var basename = base.GetEnumValueName(value);
if (basename.Contains('-'))
basename = basename.Replace('-', '_');
return basename;
}
but i think the fluent api would use a new overload for .Value()
IEnumValueDescriptor Value(TRuntimeType value);
IEnumValueDescriptor Value(TRuntimeType value, string enumValueName);
which would plumb the name through to
descriptor = EnumValueDescriptor.New(Context, value);
descriptor.Name(enumValueName);
though even this may be too late, it may need to be passed to .New()
Is there an existing issue for this?
Describe the bug
When using Enumeration classes, attempts to bind the classes results in an error.
Given an EnumType definition for enum class
AccountType
:Expected:
IEnumTypeDescriptor.Value()
, and the name provided byIEnumTypeDescriptor.Name()
Actual: For a class
Fufu.Domain.Aggregates.AccountAggregate.AccountType
: the following error results:The error seems to occur during naming:
Steps to reproduce
Relevant log output
Additional Context?
Note that a workaround to the issue is defining custom NamingConventions:
Which implies the issue being in this/around this GetEnumValueName call.
I feel like everything I do concerning HotChocolate entails workarounds though...
Product
Hot Chocolate
Version
12.11.0