3

In our company, we are creating APIs using 'GraphQL'.

I want to test and automate this API for CI/CD. I have tried 'REST assured' but it doesn't have proper support for 'GraphQL' queries yet, please suggest which tools we can use and automate these APIs for CI/CD.

2
  • Can anyone share a full sample example which worked successfully with correct graphql query rest-assured. It will be helpful
    – mtesting
    Commented Mar 26, 2019 at 17:23
  • There is a full sample in my answer. What problem are you facing with that?
    – Alexey R.
    Commented Mar 26, 2019 at 20:27

3 Answers 3

4

You should approach with REST Assured one more time. Actually it should perfectly fit your needs. When you run GraphQL service you basically run a REST end-point where you send some query and receive JSON as a response.

So nothing prevents you from using REST Assured since you can send whatever as a requests and at the same time it provides good tool set for parsing JSON response.

UPD:

Below you can find my example for your case (Rest Assured + TestNg):

@Test
public void testGraphQL() throws MalformedURLException {
    String actual = RestAssured.
                                given()
                                    .header(new Header("Content-type", "application/json"))
                                    .body("{\"query\":\"{\\n Country(id: \\\"us\\\") {\\n name\\n situation\\n }\\n}\\n \"}")
                                .post(new URL("https://portal.ehri-project.eu/api/graphql"))
                                .jsonPath().getString("data.Country.name");
    Assert.assertEquals(actual, "United Sttates");
}

With the following output:

java.lang.AssertionError: expected [United Sttates] but found [United States]
Expected :United Sttates
Actual   :United States
 <Click to see difference>

P.S. - This is the query that you have provided in your comment, however you can omit \n with no impact to your test.

4
  • Hi Alexey, Yes REST assured is a perfect tool for the API automation. Please, can you elaborate more on how we can send a get request with graphQL query in the payload? please also refer to this link github.com/rest-assured/rest-assured/issues/958 Commented Oct 30, 2018 at 9:19
  • Give me actionable GraphQL endpoint and data model to query and I'll try to help.
    – Alexey R.
    Commented Oct 30, 2018 at 10:33
  • Hi Alexey, sharing a cURL call, I want to use similar kind of request in REST assured automation curl -X POST \ https://portal.ehri-project.eu/api/graphql \ -H 'Cache-Control: no-cache' \ -H 'Content-type: application/json' \ -d '{"query":"{\n Country(id: \"us\") {\n name\n situation\n }\n}\n "}' Commented Oct 31, 2018 at 5:34
  • @swapnil.s check my update
    – Alexey R.
    Commented Oct 31, 2018 at 12:04
0

I am using SoapUI 5.4.0 (community edition) for testing GraphQL requests. Treat them as a Rest request and add a header, e.g. Content-Type: application/graphql

see image for all details

1
0

You can try webtau framework me and my friends are developing: https://testingisdocumenting.org/webtau/GraphQL/queries-and-mutations

def taskByIdQuery = '''
query taskById($id: ID!) {
    taskById(id: $id) {
        id
        description
        completed
    }
}
'''
graphql.execute(taskByIdQuery, [id: "a"]) {
  errors.should == null
  taskById.id.should == "a"
  taskById.completed.should == true
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.