How to test a GraphQL
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”.
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.
Similarly to update or add resource mutation is called.
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
- A library to serialise Java Objects to JSON (Jackson, Gson)
- 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
- Add dependencies for Rest Client and library for Serialisation of Java objects to JSON Object (Rest assured and Jackson respectively used here).
- Write Query (or mutation) in a text file. For standardization .graphql extension can be used to name it.
- Read this query using FileUtils class in Java.
- 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.
- In case GraphQL variables are used in the query, variables are added to the same node using
set
method of ObjectNode. - Convert the ObjectNode to JSON String using writeValueAsString.
- 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.
To send any GraphQL query we just need to add a complete query in a separate text file preferable with .graphql extension.
A typical test would look like:
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.
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
Thanks for Reading !!!
Keep following TestVagrant for more such interesting and valuable learnings !!