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

问题描述

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
        }
    }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...