How to test a Quarkus GraphQL API

With curl, the GraphQL tool, insomnia and Rest-assured

In the previous article I described the Quarkus GraphQL support and we discussed some GraphQL topics. Now I’ll show you how to test your GraphQL API with curl, the GraphQL tool, insomnia and Rest-assured. As an example I use my superhero project from my last article, which can be found on GitHub for you to run and play around with.

curl
If you would like to try the GraphQL API by yourself, start the Quarkus Project with mvn quarkus:dev and write the following:

    curl --request POST \
    --url http://localhost:8080/graphql \
    --header 'Content-Type: application/json' \
    --data '{"query":"{allCities{name}}"}'

That is quite simple. All we’ll need is to create a JSON payload, e.g. with a single element query with our query as its value. And as a result, you get this.

{"data":{"allCities":[{"name":"Gotham City"},{"name":"New York City"}]}}

GraphQL Tool
Let’s move on to the GraphQL tool. You can do the same query with the Quarkus GraphQL tool. Just enter http://localhost:8080/q/graphql-ui/. On the left side type in the query. Press the button with the triangle, and then you can see the result on the right side.

Insomnia
I think it’s pretty cool that you can test both REST and GraphQL APIs with Insomnia. To create a GraphQL query, you have to switch from Body to GraphQL on the far left. And that’s it.

Rest-assured
If you want to write automated tests, you could use Rest-Assured as we have already discovered. For the sake of completeness, I repeat the example from the last article. The response is plain JSON, so we can assert on the data returned by the API in the exact same manner with REST Assured as with REST APIs.

@QuarkusTest
public class SuperheroTest {
 
    @Test
    void allCities() {
        final Response response = given()
                .contentType(ContentType.JSON)
                .body("{\"query\":\"{\\n allCities{\\n name\\n}\\n}\"}")
                .when()
                .post("/graphql")
                .then()
                    .assertThat()
                    .statusCode(200)
                .and()
                    .extract()
                    .response();
 
        final List<City> allCities = response.jsonPath().getList("data.allCities", City.class);
        assertThat(allCities)
                .isNotEmpty()
                .hasSize(2)
                .extracting(City::getName)
                .contains("Gotham City", "New York City");
    }
 
}

Conclusion
As you have seen in this article, it is quite simple to test a GraphQL API. You can use curl for a quick check or the GraphQL Tool / Insomnia if you need more convenience. It’s even possible to use REST Assured to write automated tests because the response is pure JSON.

Photo by Nguyen Dang Hoang Nhu on Unsplash

Links
Github https://github.com/claudioaltamura/quarkus-graphql-superheroes
Quarkus SmallRye GraphQL https://quarkus.io/guides/smallrye-graphql
Insomnia https://insomnia.rest/
Rest-assured https://rest-assured.io/