You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hot Chocolate GraphQL Resolver中如何对嵌套实体属性做就近取整

核心问题说明

你之前使用的context.ArgumentValue<>是用来获取GraphQL查询输入参数的方法,要获取当前字段所属的父实体数据,需要用context.Parent<T>()方法获取父级实例,再读取属性做格式化处理即可。


方案1:直接在现有TestA的ObjectType中配置

直接修改你现有的TestAExtentions类即可完成需求:

public class TestAExtentions : ObjectType<TestA>
{
    private static double FormatValues(double value) => Math.Round(value / 5, MidpointRounding.AwayFromZero) * 5;
    protected override void Configure(IObjectTypeDescriptor<TestA> descriptor)
    {
        _ = descriptor ?? throw new ArgumentNullException(nameof(descriptor));

        // 配置AValue舍入逻辑
        descriptor.Field(a => a.TestB.TestC.AValue)
            .Type<DecimalType>()
            .Resolve(context => 
            {
                var testA = context.Parent<TestA>();
                // 空值处理,可根据业务需求调整空值返回规则
                if (testA.TestB?.TestC == null)
                    return 0m;
                return (decimal)FormatValues(testA.TestB.TestC.AValue);
            });
        
        // 同理配置BValue舍入逻辑
        descriptor.Field(a => a.TestB.TestC.BValue)
            .Type<DecimalType>()
            .Resolve(context => 
            {
                var testA = context.Parent<TestA>();
                if (testA.TestB?.TestC == null)
                    return 0m;
                return (decimal)FormatValues(testA.TestB.TestC.BValue);
            });
    }
}

方案2:单独为TestC定义ObjectType(更推荐)

因为AValue、BValue是TestC的固有属性,统一配置后所有引用TestC的接口都会自动应用舍入规则,避免重复编码:

  1. 新增TestC的映射类型
public class TestCType : ObjectType<TestC>
{
    private static double FormatValues(double value) => Math.Round(value / 5, MidpointRounding.AwayFromZero) * 5;
    
    protected override void Configure(IObjectTypeDescriptor<TestC> descriptor)
    {
        // 配置AValue舍入
        descriptor.Field(c => c.AValue)
            .Type<DecimalType>()
            .Resolve(context => 
            {
                var testC = context.Parent<TestC>();
                return (decimal)FormatValues(testC.AValue);
            });
        
        // 配置BValue舍入
        descriptor.Field(c => c.BValue)
            .Type<DecimalType>()
            .Resolve(context => 
            {
                var testC = context.Parent<TestC>();
                return (decimal)FormatValues(testC.BValue);
            });
    }
}
  1. 在GraphQL服务注册中添加该类型即可生效,不需要修改原有TestA的配置:
builder.Services.AddGraphQLServer()
    .AddType<TestCType>() // 新增这行注册
    // 你原有其他的GraphQL配置
    ;

补充说明

  • 如果业务需要同时返回原始值和舍入后的值,可以新增自定义字段而不重写原有字段,示例:
descriptor.Field<decimal>("AValueRounded")
    .Resolve(context => 
    {
        var testC = context.Parent<TestC>();
        return (decimal)FormatValues(testC.AValue);
    });
  • Hot Chocolate会自动处理EF Core的嵌套投影,不需要你手动写Include加载TestB、TestC实体,中间件会根据查询字段自动生成最优的SQL语句。

内容的提问来源于stack exchange,提问作者rocky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 19:45:01