How to test and automate APIs implemented in GraphQL



  • 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.



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2