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

使用HttpGraphQlTester时ArgumentValue与UpdateImageInput不兼容报错

问题:HttpGraphQlTester使用ArgumentValue时请求报错

相关GraphQL定义

更新图片的Mutation

mutation updateImage($input: UpdateImageInput!) {
    updateImage(input: $input) {
        ...Image
    }
}

UpdateImageInput输入类型

input UpdateImageInput {
    id: ID!
    version: Int!
    title: String
}

报错信息

{message=Variable 'input' has an invalid value: Expected a String input, but it was a 'LinkedHashMap', locations=[{line=1, column=22}]}

客户端请求代码(Kotlin)

val input = new MyInput(testImage.getId(), testImage.getVersion(), 
                ArgumentValue.ofNullable("foo"));

val actual = this.gql
    .documentName("updateImage")
    .fragmentName("fragments/Image")
    .fragmentName("fragments/Address")
    .fragmentName("fragments/LinkedPlanningObject")
    .variable("input", input)
    .execute()
    .errors()
    .verify()
    .path("updateImage")
    .entity(Image.class)
    .get();

问题背景

移除ArgumentValue直接传入字符串时,请求能正常执行;GraphiQL等其他客户端也能正常运行该请求。想知道是不是漏了配置,或者怎么配置客户端才能支持ArgumentValue。


解决方案

问题核心:ArgumentValue是Spring GraphQL服务器端用于参数解析的工具类,不是给客户端(比如HttpGraphQlTester)传参用的。客户端传参直接用原始值即可,不需要用它包装。

具体调整:

  1. 修改MyInput构造参数,把ArgumentValue.ofNullable("foo")替换为直接传字符串"foo";如果允许title为空,直接传null即可
  2. 确保MyInput类能被正确序列化为JSON——比如要有对应的getter/setter,或使用Kotlin的data class

调整后的代码示例:

// 传非空title
val input = MyInput(testImage.getId(), testImage.getVersion(), "foo");
// 或者传空title
// val input = MyInput(testImage.getId(), testImage.getVersion(), null);

val actual = this.gql
    .documentName("updateImage")
    .fragmentName("fragments/Image")
    .fragmentName("fragments/Address")
    .fragmentName("fragments/LinkedPlanningObject")
    .variable("input", input)
    .execute()
    .errors()
    .verify()
    .path("updateImage")
    .entity(Image::class.java)
    .get();

原因说明:
HttpGraphQlTester会将传入的变量对象序列化为JSON,而ArgumentValue是服务器端用来处理参数逻辑(比如nullable判断、默认值填充)的封装类,客户端传递它会导致序列化后变成LinkedHashMap结构,和GraphQL期望的String类型不匹配,从而触发报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 22:46:11