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

GraphQL传递Int数组至Mutation报错求助

解决GraphQL Mutation传递Int数组时的类型匹配错误

问题根源

你遇到的Unknown Collection element type错误,核心是GraphQL输入定义与Java DTO的类型不兼容:

  • GraphQL里的conditionIds: [Int]对应Java的Integer类型(或其集合/数组)
  • 但你的DTO中用了Long[] conditionIds,类型不匹配导致解析失败,进而抛出rejected value [null]的异常。

解决方案

方案1:统一类型为Integer(推荐)

修改Java DTO的conditionIds类型,与GraphQL定义保持一致:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class HepatitisLabConditionGroupInput {
    Long groupId;
    String groupName;
    // 替换Long[]为Integer[],或用List<Integer>更灵活
    Integer[] conditionIds;
    // 可选:List<Integer> conditionIds;
}

使用List<Integer>的话,Spring GraphQL会自动处理集合解析,比数组更不容易出现类型适配问题。

方案2:自定义GraphQL标量适配Long类型

如果业务必须用Long,需要先在GraphQL中自定义Long标量(标准GraphQL无Long类型),再修改输入定义:

scalar Long

input HepatitisLabConditionGroupInput{
    groupId: Long
    groupName: String!
    conditionIds: [Long]
}

同时在Spring中配置该自定义标量的解析器,不过这种方式会增加复杂度,非必要不推荐。

验证修改

调整后重新测试Mutation:

mutation{
  saveHepatitisLabConditionGroup(hepatitisLabConditionGroupInput:{
    groupId: 100
    groupName: "Test 1"
    conditionIds: [77,78]
  })
}

此时类型匹配正常,数组参数能正确解析到DTO中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 16:51:01