How to test a GraphQL

Kalpanab
TestVagrant
Published in
4 min readJun 23, 2020

--

GraphQL is a language for APIs that enables a client to query and manipulate data easily through an intuitive and flexible syntax. GraphQL provides a syntax to describe data requirements and interactions, allowing the client to ask for exactly what it needs and get back predictable results from the server. Queries and mutations that are available for clients to execute against data graph are specified in the schema of GraphQL server. Read more on GraphQL fundamentals on this intuitive blog from TestVagrant.

Testing through postman

To send a GraphQL query through postman select the request body type as GraphQL and pass the query in the query section. The method would be POST. In below example, a simple query is fetching details of the category from GraphQL server with category id “2”.

Passing query to get id and name of Category with Id “2” in postman

In the query above our argument is inside the query string. But in most applications, the arguments to fields will be dynamic hence to factor dynamic values out of the query and pass them as separate dictionary variables are used.

Factoring dynamic values out of the query with the help of GraphQL Variables
Postman log for above request

Similarly to update or add resource mutation is called.

Sending GraphQL Mutation request through postman to update Category
Postman log for above mutation request

Automating GraphQL API

As evident from postman logs above, GraphQL APIs are called like any other Rest API except the fact that we need to pass query(or mutation) as request body with the method POST.

Prerequisite

  1. A library to serialise Java Objects to JSON (Jackson, Gson)
  2. Any Rest client (Rest Assured/ HTTP Client or Retrofit etc.)

However, if you observe logs carefully, these queries are written in GraphQL data language though it appears JSON format at first glance. To pass them in request body these need to be converted to JSON format irrespective of the client being used. To accomplish this Jackson is used in the example, which is a Java-based library to serialize or map java objects to JSON and vice versa. Jackson would convert the query String read to the corresponding JSON string. Jackson library can be used after adding following Gradle dependency :

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'

Steps

  1. Add dependencies for Rest Client and library for Serialisation of Java objects to JSON Object (Rest assured and Jackson respectively used here).
  2. Write Query (or mutation) in a text file. For standardization .graphql extension can be used to name it.
  3. Read this query using FileUtils class in Java.
  4. Initialise an ObjectNode of Jackson and set above-read query string as the value for “query” parameter. Put method of ObjectNode can be used for this purpose.
  5. In case GraphQL variables are used in the query, variables are added to the same node using setmethod of ObjectNode.
  6. Convert the ObjectNode to JSON String using writeValueAsString.
  7. Pass the JSON String to Rest client to fetch/modify required data from GraphQL Server.

Below are some code snippets to understand above-mentioned steps for automating GraphQL APIs.

Mutation Query in .graphql file

To send any GraphQL query we just need to add a complete query in a separate text file preferable with .graphql extension.

Add each query(or mutation) in a separate text file like above.
Converting query String to JSON String using Jackson library

A typical test would look like:

Junit test for GraphQL Mutation Query

where parseGraphql method reads query from .graphql file using FileUtils class method. It calls another method convertToGraphqlString internally that converts this file content into JSON String.

Contents of parseGraphQL method

Conclusion

GraphQL provides a complete and understandable description of the data in your API. It gives clients the power to ask for exactly what they need and nothing more. To send the GraphQL query either through postman or any other client we need to pass the query as the request body of the API.

References

https://dev.to/email2vimalraj/introducing-test-graphql-java-4i7h

https://graphql.org/

Thanks for Reading !!!

Keep following TestVagrant for more such interesting and valuable learnings !!

--

--