Table of contents
1.
Introduction
2.
GraphQL
3.
Sending GraphQL queries using Query Parameters​
3.1.
Result 
4.
Sending GraphQL queries and mutations in the HTTP Body​
4.1.
Result
5.
Add a GraphQL request to a test case​
6.
Use GraphQL variables​
7.
Validate GraphQL requests and responses against schemas​
7.1.
Result
8.
Frequently Asked Questions
8.1.
Does Katalon support GraphQL?
8.2.
Is GraphQL a backend?
8.3.
Can GraphQL replace REST?
8.4.
What is an alternative to GraphQL?
8.5.
How many types of APIs are there?
9.
Conclusion
Last Updated: Mar 27, 2024

GraphQL in Katalon

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Readers!!

Have you ever thought about when a client wants to fetch the API? What should he do?

There when GraphQL comes into play. It is a query language that helps the client to use the programming interface to fetch the APIs.

Let’s begin to learn more about it.

GraphQL in Katalon

GraphQL

GraphQL is a query language which specifies how a client should use an application programming interface to seek information (API). Developers can use the GraphQL syntax to request specific data and receive it back from various sources. The exact structure of data is returned from the server once the client has specified the structure of the data that is required.

Sending GraphQL queries using Query Parameters

Now let’s learn about how to send GraphQL queries using Query Parameters:

  • Create a Web Service Request object and select RESTful as the Request Type.
  • Select the GET request method in the Web Service Request object editor.
  • In the URL, add this:
https://countries.trevorblades.com/graphql?query=query Query { country(code: "VN") { name native capital emoji currency languages { code name } }}
  • Name: query
  • Value: query Query { country(code: "VN") { name native capital emoji currency languages { code name } }}
Sending GraphQL queries using Query Parameters​
  • Save and click on Test Request and Verify.

Result 

The country information is displayed as JSON responses in the Response tab.

Result

Sending GraphQL queries and mutations in the HTTP Body

Follow these steps to send a GraphQL query in the HTTP Body:

  1. Make a Web Service Request object and select RESTful as the Request Type.
  2. Select the POST request method in the Web Service Request object editor.
  3. In the URL, add this https://katalon-sample-graphql-aut.herokuapp.com/graphql
  4. Enter your GraphQL mutation or query in the Query textbox.
  • GraphQL Query :
query {
findAllBooks {
    id
    title
    isbn
    pageCount
    author {
        id
        firstName
        lastName
    }
  }
}
  • GraphQL mutation 
mutation {
   deleteBook(id:3)
}

 

Query
  1. Then click Test Request and Verify after saving your request.
  2. Look out for all the outcomes on the Response tab.

Result

  • Use GraphQL to search for all books.
Result
  • Use a GraphQL mutation to create a new book.
GraphQL mutation

 

  • Modify a book's name with a GraphQL mutation.
GraphQL mutation.
  • Using a GraphQL mutation, delete a book.
Delete

Add a GraphQL request to a test case

A GraphQL request can be included in a test case.

Use GraphQL variables

A GraphQL request can be included in a test case.

  1. Select the GraphQL body type under the HTTP Body tab.
  2. Put your GraphQL query with defined variables into the Query textbox.
Use GraphQL variables​
  • Put the values of your GraphQL variables in the Query Variables textbox.
Example :
{
   "id": "AU"
}

Validate GraphQL requests and responses against schemas

Do the following to check a GraphQL request body and the response returns against schemas:

  • Make a web service request object in GraphQL.
  • Select the Verification tab in the web service request object editor.
  • Use the WS.validateGraphQLBodyAgainstSchema method in the Verification snippets, a schema, and the request body.

Example:

import static org.assertj.core.api.Assertions.*
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webservice.verification.WSResponseManager
import groovy.json.JsonSlurper
import internal.GlobalVariable as GlobalVariable
RequestObject request = WSResponseManager.getInstance().getCurrentRequest()
ResponseObject response = WSResponseManager.getInstance().getCurrentResponse()
WS.validateGraphQLBodyAgainstSchema(request, "Schema/Country.qls", FailureHandling.CONTINUE_ON_FAILURE)
WS.validateJsonAgainstSchema(response, "Schema/JSON/CountrySchema.json", FailureHandling.STOP_ON_FAILURE)
  • Click Save, Test Request, and Verify.

Result

The validation result is displayed in the Response section:

Result

Frequently Asked Questions

Does Katalon support GraphQL?

In Katalon Studio, the GET and POST methods can be used to submit a GraphQL request.

Is GraphQL a backend?

GraphQL is either frontend or backend. GraphQL is the language used to exchange information between the front and back end.

Can GraphQL replace REST?

REST is the replacement of GraphQL, a query language, and runtime for APIs. Scaling and creating APIs can be done more quickly and easily with GraphQL.

What is an alternative to GraphQL?

The most common alternatives and competitors to graphql are Cube. js, Apollo, Oracle PL/SQL, and Oracle PL/SQL.

How many types of APIs are there?

Four web APIs are: internal, partner, open, and composite.

Conclusion

In this article, we have extensively discussed GraphQL in Katalon Studio. Sending GraphQL queries using query parameters and sending GraphQl queries in the HTTP body in katalon studio. In the end, we saw Validating GraphQL requests and responses against schemas.

If you want to learn more deeply, check out the excellent content on the Coding Ninjas Website:

Import RESTful from Postman in KatalonManage Mobile Test Objects in Katalon studioSample iOS mobile tests project in Katalon studio

For more content, Refer to our guided paths on Coding Ninjas Studio to upskill yourself. If you want to explore more, feel free to see our coursesinterview experiences, problems to solvetest serieslibraries, and resources

Thank You

Do upvote our blogs if you find them helpful!

Live masterclass