Can HotChocolate GraphQL be configured to convert all enums to their description attribute?
Asked
Active
Viewed 763 times
1 Answers
1
The code below does the trick however I released what I was trying to do wasnt correct. The enum description for NA was 'N/A', 'N/A' isnt a valid enum so HotChocolate throws an Exception
What I should be doing is creating extra fields for the enum descriptions
.AddConvention<INamingConventions, NamingConventions>();
public class NamingConventions : DefaultNamingConventions
{
public override NameString GetEnumValueName(object value)
{
var type = value.GetType();
var memberInfo = type.GetMember(value.ToString());
var descAttribute = memberInfo?.FirstOrDefault()?.GetCustomAttributes(typeof(DescriptionAttribute), false)
?.FirstOrDefault();
return descAttribute == null
? value.ToString()
: ((DescriptionAttribute)descAttribute).Description;
}
}

Shaun Whyte
- 66
- 4