如何控制 HotChocolate 中枚举值的序列化?

How can you control the serialization of Enum values in HotChocolate?

HotChocolate 在所有大蜗牛案例中序列化枚举值,这导致 作为枚举值 FooBar 被 Hot Chocolate 推断为 FOO_BAR,但是 value.ToString()Enum.GetName(value) 给出了 FooBar,而 Hot Chocolate 似乎忽略了 [EnumMember(Value = "FooBar")].

如何将序列化更改为我喜欢的任何方式?

HotChocolate 服务器 v11 遵循规范建议,默认将枚举值序列化为 UPPER_SNAIL_CASE。

您可以这样更改:

    builder
        .AddConvention<INamingConventions>(new YourNamingConvention())

    public class YourNamingConvention
        : DefaultNamingConventions
    {
        public override NameString GetEnumValueName(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            return value.ToString().ToUpperInvariant(); // change this to whatever you like
        }
    }