GQL/ GraphQL/ Graph Query Language

Hai guys, i need to know does katalon already support GraphQL? if no, then i’ll create the Keyword by myself. I just dont want my work being redundant if i create the keyword without knowing the fact that katalon support GraphQL and has their built-in functions. Thank you very much

But…i can’t found graphql on the documentation…

okay okay im sorry if said something wrong, i didnt mean that. Thank you by the way.

Hi , did you create a keyword for the GraphQL, if yes can you show what you did ? I am trying to use Katalon with GraphQL, but got some problem with some request. The Same GraphQL request are working for me in Postman.

Thanks for saving my time??

image

Hai, i figured out how to execute gql using katalon. First i need to import okhttp jar, then create a custom keyword like this

class GQL {
public String url, query
Response response

def setUrl(String url){
	this.url = url
}

def setQuery(String query){
	this.query = query
}

def sendRequest(){
	OkHttpClient client = new OkHttpClient().newBuilder().followRedirects(false).followSslRedirects(false).build()
	MediaType mediaType = MediaType.parse("application/graphql")
	RequestBody body = RequestBody.create(mediaType, this.query)
	Request request = new Request.Builder()
			.url(this.url)
			.post(body)
			.addHeader("cookie", cookie)
			.addHeader("content-type", "application/json")
			.build()
	response = client.newCall(request).execute()
	return response
}

def verifyStatusCode(int statusCode){
	assert response.code == statusCode
}

//optional
def verifyPayloadContainsData(){
	def responseText = response.body().string()
	println responseText
	JsonSlurper parser = new JsonSlurper()
	def payload = parser.parseText(responseText)
	assert payload.data != null
	return payload
}}

and this is how i implement it on test case

GQL gql = new GQL()
gql.setUrl("gql.com") //ur url here
// set query by generating the query into string
// still working on this keyword, so this function able to accept gql query as an input
gql.setQuery("{\"query\":\"query getPerson {\\n  profile: user {\\n    name\\n    address\\n    dob\\n    gender\\n    isactive\\n  }\\n}\\n\"}")
def response = gql.sendRequest()

i dont know tho if this still acceptable or if there is any new feature to support gql execution, because i havent been using katalon since dec 2019.

1 Like

I used your keyword and tried to implement it on a test case using the spotify sample from https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/

When using the format you defined :arrow_down:

the response i got was always like :arrow_down:

Response{protocol=http/1.1, code=400, message=Bad Request, url=https://spotify-graphql-server.herokuapp.com/graphql}

Katalon seems to have issues with the graphQL mediaType. So I changed it to json:
MediaType mediaType = MediaType.parse(“application/json”)
and removed the no longer needed line:
.addHeader(“content-type”, “application/json”)

In my case I also didn’t need the line: .addHeader(“cookie”, cookie)

For copy paste:

public class GQL {

	public String url, query
	Response response

	def setUrl(String url){
		this.url = url
	}

	def setQuery(String query){
		this.query = query
	}

	def sendRequest(){
		OkHttpClient client = new OkHttpClient()
		MediaType mediaType = MediaType.parse("application/json")
		RequestBody body = RequestBody.create(mediaType, this.query)

		println(this.query)

		Request request = new Request.Builder()
				.url(this.url)
				.post(body)
				.build()
		response = client.newCall(request).execute()
		return response
	}

	def verifyStatusCode(int statusCode){
		assert response.code == statusCode
	}

	//optional
	def verifyPayloadContainsData(){
		def responseText = response.body().string()
		println responseText
		JsonSlurper parser = new JsonSlurper()
		def payload = parser.parseText(responseText)
		assert payload.data != null
		return payload
	}
}

In a test case my query looks like this: gql.setQuery("{\“query\”:\“query getByArtist{queryArtists{name image albums{name}}}\”}")

Hope this helps anyone who stumbles upon this.

2 Likes

Hi All,

We’re excited to announce the release of Katalon Studio version 8.4.0.beta, which provides you with a proof-of-concept for GraphQL testing. Go for it here. Any early feedback is much appreciated.

GraphQL is well-known among API consumers due to its popularity and accessibility. Wonder how GraphQL significantly improves the efficiency and performance of API calls in Katalon Studio. Follow our detailed instructions at [API Testing] GraphQL (PoC).

We gratefully await your responses!
Shin

P/s: Kindly note that this PoC is not ready for production use. We recommend using this PoC for evaluation purposes only.