swapnil shirke
swapnil shirke

Reputation: 91

How to test and automate APIs implemented in GraphQL

In our company, we are creating an application by implementing graphQL. I want to test and automate this APIs for CI/CD.

I have tried REST-assured but since graphQL queries are different than Json, REST-assured doesn't have proper support for graphQL queries as discussed here.

Upvotes: 1

Views: 2862

Answers (2)

SIGODEN
SIGODEN

Reputation: 11

You can test it with apitest

{
  vars: { @describe("share variables") @client("echo")
    req: {
      v1: 10,
    }
  },
  test1: { @describe("test graphql")
    req: {
      url: "https://api.spacex.land/graphql/",
      body: {
        query: `\`query {
  launchesPast(limit: ${vars.req.v1}) {
    mission_name
    launch_date_local
    launch_site {
      site_name_long
    }
  }
}\`` @eval
      }
    },
    res: {
      body: {
        data: {
          launchesPast: [ @partial
            {
              "mission_name": "", @type
              "launch_date_local": "", @type
              "launch_site": {
                "site_name_long": "", @type
              }
            }
          ]
        }
      }
    }
  }
}

Apitest is declarative api testing tool with JSON-like DSL.

See https://github.com/sigoden/apitest

Upvotes: 0

vitor_binsfeld
vitor_binsfeld

Reputation: 21

So I had the same issue and I was able to make it work on a very simple way.

So I've been strugling for a while trying to make this graphQL request with Restassured in order to validate the response (amazing how scarce is the info about this) and since yesterday I was able to make it work, thought sharing here might help someone else.

What was wrong? By purely copying and pasting my Graphql request (that is not json format) on the request was not working. I kept getting error "Unexpected token t in JSON at position". So I thought it was because graphql is not JSON or some validation of restassured. That said I tried to convert the request to JSON, imported library and lot of other things but none of them worked.

My grahql query request:

String reqString = "{ trade  { orders { ticker } }}\n";

How did I fixed it? By using postman to format my request. Yes, I just pasted on the QUERY window of postman and then clicked on code button on the right side (fig. 1). That allowed my to see my request on a different formatt, a formatt that works on restassured (fig. 2). PS: Just remeber to configure postman, which I've pointed with red arrows.

My grahql query request FORMATTED:

String reqString = {"query":"{ trade  { orders { ticker } }}\r\n","variables":{}}

Fig 1.

Fig 2.

Hope it helps you out, take care!

Upvotes: 2

Related Questions