【问题标题】:Gatling post request sending GraphQl mutation in body not workingGatling 发布请求在正文中发送 GraphQl 突变不起作用
【发布时间】:2021-10-24 15:03:38
【问题描述】:

// 我尝试将突变作为 json 发送

val testAPIScenario = scenario("Sample test")
            .exec(http("graph ql sample test")
                .post("https://demo.com/")
                .body(RawFileBody("./src/gatling/resources/graphql/sample.json")).asJson
                .header("content-type",value = "application/json")
                .check(status.is(200))
            )

val testAPIScenario = scenario("Sample test")
            .exec(http("graph ql sample test")
                .post("https://demo.com/")
                .body(StringBody("\"query\":\""+getMutation()+"\",\"variables\":"+getVariables()+"}")).asJson
                .header("content-type",value = "application/json")
                .check(status.is(200))
            )

还尝试使用 ElFileBody 发送它,将突变保存在文本文件中。

只需要知道是否有任何方法可以在加特林体内发送 graphQl 突变

我检查了日志,请求在 graphql 上正常进行,但它给了我 400,我认为有一些格式问题,请指导我

【问题讨论】:

  • 不熟悉 gatling 但有 2 件事,1 仔细检查您的端点,大多数 graphql 服务器使用 '/graphql' 2,使用 curl 或 postman 获取 graphql 内省架构 hasura.io/learn/graphql/intro-graphql/introspection 到仔细检查部署的架构是否符合您的要求
  • 我在这里添加了一个虚拟端点,实际端点不同。
  • 那么我建议下一步是通过自省获取 graphql 模式,这样您就知道您的端点是正确的,并且您可以看到后端期望的突变形状,请参阅stackoverflow.com/questions/37397886/…

标签: scala graphql gatling


【解决方案1】:

您的代码中有几处错误。

  1. 身体路径

RawFileBody("./src/gatling/resources/graphql/sample.json") 非常脆弱,因为它取决于您启动进程的位置,并且如果您在文件系统上没有展开的项目(例如 Gatling Enterprise)会中断。

首选RawFileBody("graphql/sample.json")

  1. 值参数而不是函数参数

documentationStringBody 接受Expression,这意味着您可以传递一个值或一个函数。

在您的代码中,您传递的是前者,因此 getMutation()getVariables() 在传递值时只被调用一次。

相反,您希望传递一个函数,以便在每次虚拟用户尝试构建和发送此请求时调用它们:

StringBody(session => "\"query\":\""+getMutation()+"\",\"variables\":"+getVariables()+"}")

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2021-08-29
    • 2021-05-09
    • 2020-09-28
    相关资源
    最近更新 更多