This article covers creating a GraphQL Query and validating the response using Scriptworks.
As we covered what GraphQL is and how it works in our previous article, let’s try an exercise to validate the response received from a GraphQL API.
First, I will demonstrate the exercise we need to perform to automate the Validation of a Single response received from a GraphQL API using Scriptworks, followed by practically demonstrating the Automation through Drag and Drop of the relevant Blocks present in Scriptworks for achieving it.
Demonstrating Drag and Drop of GraphQL Query Block into Canvas
Now Enter the Graph QL query inside the GraphQL query block, for this exercise we are using the following query:
Scriptworks provides you the great feature of adding a multiline block that can be expanded to show the query in a formatted way. Let’s see that as well:
Click on Block symbol → Click on Input Values Link → Choose Multiline block → Drag and drop to Canvas.
Demonstrating Drag and Drop of get response Block into Canvas
5) Let’s give it a dry run and see what we are receiving in the response after the run.
In the above screenshot, we can see what request we have sent to GraphQL API through GraphQL query with the kind of request headers that have been passed.
In the above screenshot, we can observe responses from the GraphQL API in the Response Body, plus we also get information on the Response Time of the API with some other parameters.
We are done with GraphQL Request and Response Blocks. What about Automatically Validating the Response? Let’s look at it from Step 6 Onwards.
6) Drag comment to let others know what we are going to do now. And for that, follow Step 3 Above.
Search → Comment → Drag and Drop to Canvas
7) Now, to validate the response, or we can say in technical terms, i.e., to Assert the API response received. Drag the Assert block multiple times for the number of Assertions required, put it in Canvas, and fill it with information you wish to assert from the GraphQL API response.
Search → Assert → Drag and Drop to Canvas
As GraphQL API response is in JSON, we need to drag a block that can handle JSON response readily available under the API - JSON/XML Handling category in the left side panel.
JSON/XML Handling → Extract Block → Drag and Drop to Canvas
We need what part of the response? Of course, we need a response body. So search for "response" in the search box and pick the response body block and drag it into the canvas.
Search → Response → Drag and Drop Response Body into Canvas
8) Now, the final thing is a Test Run. Make sure you have followed the prerequisite mentioned above before the Test Run. Make sure you can see Green Symbol before clicking on the play icon.
That’s how we can validate a GraphQL API response in Scriptworks.