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

Karate API GraphQL POST示例:创建用户后查询参数传递问题求助

问题根因

  • 第二个GraphQL查询语句中的变量占位符存在语法错误:你写的#(userID'缺少了占位符要求的右闭合括号),非法的查询语句直接触发GraphQL解析报错。
  • 额外注意:如果你的服务端getUser接口的id参数定义为数值类型而非字符串类型,需要删除占位符外层的双引号,避免后续出现参数类型不匹配的错误。

修正后的测试代码

Scenario: Create a User
Given path 'query'
And header Accept = 'application/json'
Given text query = 
"""
mutation {
  createUser(username: "CreateUser16"
, name: "Auto CreateUser", isPrivate: true) {
        id
        username
        lastLoginAt
        createdAt
      }
    }

"""

And request { query: '#(query)' }
When method POST
Then status 200
And print response
And match response.data.createUser.id !=null
* def userID = response.data.createUser.id
And print userID
Given path 'query'
And header Accept = 'application/json'
Given text query = 
"""
{
  getUser(id: '#(userID)') {
    bio
    birthDate
    createdAt
    id
  }
}
"""

And request { query: '#(query)' }
When method POST
Then status 200
And print response

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 06:24:03