APIs Unleashed 08:
Handling GraphQL

What is GraphQL?

GraphQL is a query language and server-side runtime for application programming interfaces (APIs) that prioritizes giving clients exactly the data they request and no more.

 

GraphQL was developed by Facebook. The GraphQL specification was open sourced in 2015 which is now overseen by the GraphQL Foundation.

 

GraphQL is designed to make APIs fast, flexible, and developer-friendly. It can even be deployed within an integrated development environment (IDE) known as GraphiQL. As an alternative to REST, GraphQL lets developers construct requests that pull data from multiple data sources in a single API call. Developers can build APIs with whatever methods they prefer, and the GraphQL specification will ensure they function in predictable ways to clients.

 

For more details check here: https://graphql.or

KarateDSL: Handling GraphQL

Let's see how to handle GraphQL using KarateDSL

KarateDSL is appropriate for testing highly dynamic responses from GraphQL APIs due to its built-in text manipulation and JsonPath features. These APIs resemble JSON in appearance, but they are not.

 

To support GraphQL APIs, we’ll need to stop karate’s default behaviour of attempting to parse everything that looks like JSON(or XML). This can be accomplished by using the ‘text’ keyword.

 

The purpose of the ‘text’ keyword is – to treat it as raw text rather than parsing it.

 

In the following part, we’ll look at a few examples:

 

HTTP Method: POST

 

API: https://countries.trevorblades.com

 

Request Body:

{

countries {
code
name
native
emoji
phone
}

}

Example 1: Successful execution of the GraphQL API using Karate.

 

The request-body (variable name – ‘query’) is defined as ‘text’ in the code snippet below, which is then passed with the ‘request’ keyword.

After running the feature file, the desired result is obtained.

Example 2: Removing key from the request body.

 

The entire code is the same in this example; we are only removing “emoji” from the request body to get our desired response.

In the following report, you will observe “emoji” key is eliminated from the API response.

Example 3: Add keys in the request body as per documentation.

 

To acquire more information in the response, we’ll change the request body just as in Example 2 by adding extra keys supported by the API.

According to the Karate report, the defined scenario is successful, and the response contains all of the desired key-value pairs.

Example 4: Country specific information.

 

In this example, we will be applying country-code filter along with desired keys to be obtained in response.

 

Note that we are only modifying the request body, the rest of the code remains same.

In the following karate report, you will notice that only the filter applied country details along with the values of defined keys are populated in the response.

With these examples, we’ve seen how to implement GraphQL APIs in a straightforward and simple manner using KarateDSL.

 

I hope this section was helpful in better understanding this concept.

 

Now, let’s take a look as to how it is implemented in Postman.

POSTMAN: Handling GraphQL

Let's see how to handle GraphQL POSTMAN

Postman provides built-in support for GraphQL from version 7.2

 

There are four options to handle GraphQL in Postman:

 

  1. – Using the JSON body request for the query
  2. – Same as above, but instead of JSON we can go with text mode and just need to add the header “Content-type: application/graphql
  3. – We can import the cURL request representing a GraphQL query into Postman
  4. – Using the in-built support for GraphQL in Postman.

 

For our illustration we will handle with the fourth method here:

 

Let’s see few samples of query formats and how they work.

 

Copy the URL (https://countries.trevorblades.com) and create a POST request

Example 1:

 

 

Select the body as “GraphQL” and paste the below query

{ 
countries
{ code
name
emoji
}
}

Save and Click “Send”

Here you have the option to select the keys to be displayed.

 

Example 2:

 

We can remove the “emoji” from the query and the entire response is filtered based on it.

Example 3:

 

Also you can add the few other fields based on the documentation.

{



countries

{ code

name

native

phone

continent {

       code

      }

     capital

   }



}

This will give the below response:

Example 4:

 

You can filter specific country like below:

{ 

country (code: "IN") {

code

name

native

phone

continent {

      code

      }

      capital

}



}

Response is as,

Quite interesting right? For single endpoint, we can customize the request based on our requirement. Yes, this is why many developers prefer GraphQL nowadays.

 

Since the response is in JSON format, the parsing process is similar to handling REST APIs JSON response.

 

For more details and examples please visit the below pages.

 

https://codetraveler.io/2019/01/12/how-to-use-postman-with-a-graphql-api/

 

https://www.postman.com/devrel/workspace/graphql-examples

 

https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/

Note:

 

Only the Postman v7.2 or later versions support the following:

 

  • – Sending GraphQL queries in request body as POST requests
  • – GraphQL variables
  • – Creating APIs in Postman with GraphQL schema type
  • – Query autocompletion integrated with user-defined GraphQL schemas

We hope you learnt some basics of GraphQL and how to work with GraphQL in Postman and KarateDSL. Try to get your hands dirty and play around with more examples, to get a clear understanding about GraphQL and how it is handled in the API Testing tools.

 

Dear readers, thank you for your time.

Missed Previous Chapters?

Know Our Synapses

Meet the brains behind this learning series!

Let's Together

Follow Us

Stay updated with all the latest contents & announcements

Share this:

Like this:

Like Loading...

Up ↑

Discover more from Synapse QA

Subscribe now to keep reading and get access to the full archive.

Continue reading