Search code examples
scalagraphqlgatling

Gatling post request sending GraphQl mutation in body not working


// I tried sending mutation as 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))
            )

Also tried sending it using an ElFileBody, keeping mutation in a text file.

Just need to know if there is any way I can send graphQl mutation in gatling body

I checked in logs, Request is properly going on graphql but it is giving me 400, I think there is some format issue please guide me


Solution

  • Several things wrong in your code.

    1. body path

    RawFileBody("./src/gatling/resources/graphql/sample.json") is very brittle as it depends on where you launch the process and would break if you don't have an exploded project on the filesystem (for example with Gatling Enterprise).

    Prefer RawFileBody("graphql/sample.json")

    1. value parameter instead of function parameter

    From the documentation, StringBody takes an Expression, meaning that you can pass a value, or a function.

    In your code, you're passing the former, so getMutation() and getVariables() get only called once when passing the value.

    Instead, you want to pass a function so they get called every time a virtual user tries to build and send this request:

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